Cómo crear Dibujable de recursos
Tengo una imagen res/drawable/test.png
(R. drawable.prueba).
Quiero pasar esta imagen a una función que acepte Drawable
.
(por ejemplo, mButton.setCompoundDrawables())
Entonces, ¿cómo convertir un recurso de imagen a un Drawable
?
6 answers
Su actividad debe tener el método getResources. Do:
Drawable myIcon = getResources().getDrawable( R.drawable.icon );
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2011-01-27 15:18:09
Este código está obsoleto.
Drawable drawable = getResources().getDrawable( R.drawable.icon );
Utilice este instad.
Drawable drawable = ContextCompat.getDrawable(getApplicationContext(),R.drawable.icon);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2015-08-20 13:09:56
El método getDrawable (int id)
se depreciará a partir de API 22.
En su lugar, debe usar getDrawable (int id, Resources.Theme theme)
para API 21 +
El código se vería algo como esto.
Drawable myDrawable;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
myDrawable = context.getResources().getDrawable(id, context.getTheme());
} else {
myDrawable = context.getResources().getDrawable(id);
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2016-07-14 14:31:05
Me gustaría añadir que si está recibiendo el mensaje "obsoleto" al usar getDrawable(...) debe usar el siguiente método de la biblioteca de soporte en su lugar.
ContextCompat.getDrawable(getContext(),R.drawable.[name])
No tienes que usar getResources() cuando usas este método.
Esto es equivalente a hacer algo como
Drawable mDrawable;
if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP){
mDrawable = ContextCompat.getDrawable(getContext(),R.drawable.[name]);
} else {
mDrawable = getResources().getDrawable(R.id.[name]);
}
Esto funciona tanto en versiones pre y post Lollipop.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-07-25 18:43:23
Si está tratando de obtener el elemento de diseño de la vista donde se establece la imagen como,
ivshowing.setBackgroundResource(R.drawable.one);
Entonces el elemento de diseño devolverá solo el valor null con el siguiente código...
Drawable drawable = (Drawable) ivshowing.getDrawable();
Por lo tanto, es mejor establecer la imagen con el siguiente código, si desea recuperar el elemento de diseño de una vista en particular.
ivshowing.setImageResource(R.drawable.one);
Solo entonces convertiremos exactamente el elemento de diseño.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2013-07-24 10:51:09
Obtenga el elemento de diseño del recurso vectorial independientemente de, si su vector o no:
AppCompatResources.getDrawable(context, R.drawable.icon);
Nota:ContextCompat.getDrawable(context, R.drawable.icon);
producirá android.content.res.Resources$NotFoundException
para el recurso vectorial.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-10-03 04:45:30