简体   繁体   English

如何使用R.Drawable获取图像列表

[英]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. 为了创建瓷砖管理系统,我在res / drawable目录中有一堆图像。 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") F_16.0_0_112.jpg. ("F_"+zoom"+"._"+XCoord+"_"+YCoord".jpg")

Is there a fonction like getDrawable(String) ? 是否有类似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) 您可以使用反射来实现(不要忘记导入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. 不,为此使用android asset文件夹。

Please see more details here . 在此处查看更多详细信息。

Unfortunatly no, you can't load resources by name. 不幸的是,您不能按名称加载资源。 The id's are variables in the generated R class. id是生成的R类中的变量。 In java, you can't build dynamic variable names. 在Java中,您无法建立动态变量名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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