In order to create a tile management system I have a pack of image in my res/drawable directory. How to load that ressource dynamically ?
like : F_16.0_0_112.jpg. ("F_"+zoom"+"._"+XCoord+"_"+YCoord".jpg")
F_16.0_0_112.jpg. ("F_"+zoom"+"._"+XCoord+"_"+YCoord".jpg")
Is there a fonction like getDrawable(String) ?
您可以通过以下方式使用其名称获取可绘制对象:
int id = getResources().getIdentifier("name_of_resource", "id", getPackageName());
You can get this using reflection (dont forget to import java.lang.reflect.Field)
/**
* For example calling <code>getDrawableId("ic_launcher")</code> will return the same value as <code>R.drawable.ic_launcher;</code>
*
* @param name the name of the field
* @return the drawable id
*/
public int getDrawableId(String name) {
Class<?> c = R.drawable.class;
Field f = null;
int id = 0;
try {
f = R.drawable.class.getField(name);
id = f.getInt(null);
} catch (NoSuchFieldException e) {
Log.i("Reflection", "Missing drawable " + name);
} catch (IllegalAccessException e) {
Log.i("Reflection", "Illegal access to field " + name);
}
return id;
}
On this link you will find two ways of doing this:
http://daniel-codes.blogspot.com/2009/12/dynamically-retrieving-resources-in.html
No, for this use android assets folder.
Please see more details here .
Unfortunatly no, you can't load resources by name. The id's are variables in the generated R class. In java, you can't build dynamic variable names.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.