简体   繁体   中英

How to get list of image with R.Drawable

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;
}

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM