【问题标题】:Declare a Bitmap with the name of the resource in a string用字符串中的资源名称声明一个位图
【发布时间】:2015-06-05 15:02:07
【问题描述】:

我想声明一个新的位图,但我有一些图像名称,R.raw.XXXXXXX, 而且我不想创建一个具有所有可能性的糟糕开关......有什么想法吗?

例如: 位图bitAnimal = BitmapFactory.decodeResource(getResources(), myString);

谢谢!

【问题讨论】:

    标签: android string bitmap declare


    【解决方案1】:

    我想你可以这样做:

    Bitmap bm = BitmapFactory.decodeResource(getResources(), getId(myString, myClass);
    
    public static int getId(String resourceName, Class<?> c) {
        try {
            Field idField = c.getDeclaredField(resourceName);
            return idField.getInt(idField);
        } catch (Exception e) {
            throw new RuntimeException("No resource ID found for: "
                    + resourceName + " / " + c, e);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-02-22
      • 1970-01-01
      • 1970-01-01
      • 2020-12-16
      • 1970-01-01
      相关资源
      最近更新 更多