【问题标题】:Android accessing drawable folderAndroid访问drawable文件夹
【发布时间】:2014-09-02 21:52:17
【问题描述】:

我在 drawable 文件夹中放置了 10 张图像,并创建了一个自定义类,该类的实例持有一个 id Integer,它应该将其引用到 draw 文件夹中的图像。现在我想知道如何一次设置这些id,而不是一个一个。而是:

object[0].setImage(R.drawable.image1);
object[1].setImage(R.drawable.image2);

我想用循环来做这个,但是怎么做呢?

【问题讨论】:

  • 我认为没有什么好的方法...我使用了int[] array = new int[]{ R.drawable.image1, R.drawable.image2, etc... };

标签: android image drawable


【解决方案1】:

Drawable 已知的 ID 是在 final class R 的静态类中生成的。所以只需使用反射来读取这些静态属性的值。如果您想获取 Int id,请使用以下代码:

    for(Field f : R.drawable.class.getDeclaredFields()){
        if(f.getType() == int.class){
            try {
                   int id = f.getInt(null);
                   // read the image from the id
            } catch (IllegalAccessException e) {
               e.printStackTrace();
            } catch (IllegalArgumentException e){
                e.printStackTrace();
            }
        }
    }

或者,如果您想直接按图片名称搜索,您可能会对以下字段的名称属性感兴趣:

    for(Field f : R.drawable.class.getDeclaredFields()){
        if(f.getType() == int.class){
            try {
                String name = f.getName();
                // rest of the code handling file loading
                } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (IllegalArgumentException e) {
                e.printStackTrace();
            }
        }
    }

【讨论】:

    【解决方案2】:

    没有我想的那么好。

    有代码可以通过其名称获取可绘制对象。例如:"image"+myNumber"

    也许:how to access the drawable resources by name in android

    【讨论】:

      【解决方案3】:

      请注意,视图列表的大小应与包含 id 的列表的大小相匹配:

      public class HolderIDs {
      
      public List<Integer> _ids;
      
      public HolderIDs() {
          _ids = new ArrayList<Integer>(Arrays.asList(R.drawable.one, R.drawable.two, R.ldrawable.three, R.drawable.four));
      }
      
      public List<Integer> getIDs() {
          return _ids;
      }
      
      }
      

      //用法

      List<ImageView> views = getYourViews();
      
      List<Integer> ids = new HolderIDs().getIDs();
      
      for (int i = 0; i < ids.size(); i++) {
          View view = views.get(i);
          if (view == null) return;
          int id = ids.get(i);
          view.setBackgroundResource(id);
      }
      

      【讨论】:

      • 它对我不起作用,因为我将拥有 100 多张图像。还有其他地方可以放置我的图像,然后将它们连接到正确的对象吗?
      • 使用反射,正如@eldjon 向你建议的那样。
      • 100 张图片会使您的 APK 文件变得非常大。也许您可以从某个服务器通过互联网为它们提供服务。
      【解决方案4】:

      您可以为资源创建一个整数数组:

      int 资源 = [ R.drawable.image1, R.drawable.image2, ...]

      并使用 bucle 将资源设置到您的对象数组中。

      【讨论】:

      • 如果他必须在数组中一个一个地手动写入它们有什么帮助?
      • 所以你可以使用下面的代码:int elements = 10; for (int i = 0; i
      • 那没有任何帮助。如果他将图像添加到文件夹中,他将需要再次更改数组。问题的重点是获取可绘制列表,而不必一一进行
      • 是的,但如果他需要一个过滤器,它可能会起作用,也许他想要一些格式没有图标应用程序和其他格式的图像。
      猜你喜欢
      • 2011-11-24
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2021-06-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-30
      相关资源
      最近更新 更多