从Resources对象检索所有Drawable资源

Mat*_*ins 22 collections resources android drawable

在我的Android项目中,我想循环遍历整个Drawable资源集合.通常,您只能使用以下内容通过其ID检索特定资源:

InputStream is = Resources.getSystem().openRawResource(resourceId)
Run Code Online (Sandbox Code Playgroud)

但是,我希望得到所有Drawable资源,我不会事先知道他们的ID.是否有一个我可以循环的集合,或者可能是一种获取资源ID列表的方法,它给出了我项目中的资源?

或者,Java中有没有办法从R.drawable静态类中提取所有属性值?

Mat*_*ins 33

好吧,这感觉有点黑客,但这是我通过Reflection提出的.(注意,这resources是一个类的实例android.content.res.Resources.)

final R.drawable drawableResources = new R.drawable();
final Class<R.drawable> c = R.drawable.class;
final Field[] fields = c.getDeclaredFields();

for (int i = 0, max = fields.length; i < max; i++) {
    final int resourceId;
    try {
        resourceId = fields[i].getInt(drawableResources);
    } catch (Exception e) {
        continue;
    }
    /* make use of resourceId for accessing Drawables here */
}
Run Code Online (Sandbox Code Playgroud)

如果有人有更好的解决方案,可以更好地利用我可能没有注意到的Android调用,我肯定希望看到它们!

  • 这是获取资源(而不是资产)的唯一方法.请注意,您不需要drawableResources.由于R的所有字段都是静态的,因此getInt()可以为null. (5认同)

Jan*_*sen 8

我已经对Matt Huggins给出了很好的答案并重构了它以使其更通用:

public static void loadDrawables(Class<?> clz){
    final Field[] fields = clz.getDeclaredFields();
    for (Field field : fields) {
        final int drawableId;
        try {
            drawableId = field.getInt(clz);
        } catch (Exception e) {
            continue;
        }
        /* make use of drawableId for accessing Drawables here */
    }   
}
Run Code Online (Sandbox Code Playgroud)

用法:

loadDrawables(R.drawable.class);
Run Code Online (Sandbox Code Playgroud)


mis*_*kin 7

我使用getResources().getIdentifier来扫描资源文件夹中的顺序命名图像.为了安全起见,我决定在第一次创建活动时缓存图像ID:

    private void getImagesIdentifiers() {

    int resID=0;        
    int imgnum=1;
    images = new ArrayList<Integer>();

    do {            
        resID=getResources().getIdentifier("img_"+imgnum, "drawable", "InsertappPackageNameHere");
        if (resID!=0)
            images.add(resID);
        imgnum++;
    }
    while (resID!=0);

    imageMaxNumber=images.size();
}
Run Code Online (Sandbox Code Playgroud)


D.S*_*nap 6

您应该使用 Raw 文件夹和 AssetManager,但如果您想使用可绘制对象,因为为什么不,这里是如何...

假设我们有一个很长的 JPG 可绘制文件列表,并且我们希望获得所有资源 ID,而无需一一检索(R.drawable.pic1、R.drawable.pic2 等)

//first we create an array list to hold all the resources ids
ArrayList<Integer> imageListId = new ArrayList<Integer>();

//we iterate through all the items in the drawable folder
Field[] drawables = R.drawable.class.getFields();
for (Field f : drawables) {
    //if the drawable name contains "pic" in the filename...
    if (f.getName().contains("image"))
        imageListId.add(getResources().getIdentifier(f.getName(), "drawable", getPackageName()));
}

//now the ArrayList "imageListId" holds all ours image resource ids
for (int imgResourceId : imageListId) {
     //do whatever you want here
}
Run Code Online (Sandbox Code Playgroud)


ada*_*amp 5

如果您发现自己想要这样做,那么您可能会滥用资源系统.看看资产,AssetManager如果你想迭代你的.apk中包含的文件.


Mik*_*yer 5

添加名为aaaa的图片和另一个名为zzzz的图片,然后迭代以下内容:

public static void loadDrawables() {
  for(long identifier = (R.drawable.aaaa + 1);
      identifier <= (R.drawable.zzzz - 1);
      identifier++) {
    String name = getResources().getResourceEntryName(identifier);
    //name is the file name without the extension, indentifier is the resource ID
  }
}
Run Code Online (Sandbox Code Playgroud)

这对我有用.

  • 我喜欢这个黑客背后的创造力.;) (3认同)