Android的 - 我怎么能动态加载可绘(图片)?不知道,直到运行时的名字加载、名字、动态、图片

2023-09-06 03:25:47 作者:心死了怎么暖都没用

我已经搜索谷歌和这个网站很多次,但无法找到解决我的问题。

首先,我明白了,你可以加载使用类似的图像:

  INT图像= R.drawable.icon; (假设有一个名为icon.png在文件)
 

和我已经知道了,并尝试使用:

  getResources()则getIdentifier(resname,restype,com.example.whatever)。
 

不过,这里就是我不能这样做: 我希望能够在/ RES /绘制文件夹(我相信这是正确的文件夹)的照片,不知道任何文件的名称,并动态加载它们 - 在运行时

一(的许多)事情我已经试过是(像):

  INT []的图像=新INT [numberOfImages]
为(0〜numberOfImages)
{
    图像[I] =
        getResources()则getIdentifier(resname,restype,com.example.whatever)。
}
 
审美的变迁 回顾Android 系统进化史

这将返回0每一次,所以它不是去上班

我想获得每一个画面的名称和整数标识符在/ RES /可绘制文件夹。可以这样做不知道任何文件的名称吗?

----------------------------------------------- -----------------------

[添加此之后,问题得到了解决,以帮助任何人都可能会遇到在未来同样的问题。这只是表明,它实际上是工作。]

 类资源= R.drawable.class;
    现场[]字段= resources.getFields();
    的String [] imageName =新的String [fields.length]
    INT索引= 0;
    对于(场域:域)
    {
        imageName [指数] = field.getName();
        指数++;
    }

    INT结果=
            。getResources()则getIdentifier(imageName [10],可拉伸,com.example.name);
 

解决方案

好吧,如果你必须这样做,使用反射:)

一流的资源= R.drawable.class;
现场[]字段= resources.getFields();
对于(场场:场){
    的System.out.println(field.getName());
}

然而,在某种程度上,你依然是硬编码的东西,因为你只会把一组固定的可绘制在您的应用程序。 :)

I've searched Google and this website many times, but can not find a solution to my question.

First, I understand, you can load images using something like:

int image = R.drawable.icon; (assuming there's a file called 'icon.png')

and I've read about, and tried using:

getResources().getIdentifier("resname", "restype", com.example.whatever);

But, here's what I'm unable to do: I want to be able to have pictures in the /res/drawable folder (I believe that's the correct folder), WITHOUT KNOWING ANY OF THE NAMES OF THE FILES, and load them dynamically - at run-time.

One (of the many) things I've tried is (something like):

int[] images = new int[numberOfImages];
for( 0 to numberOfImages )
{
    images[i] = 
        getResources().getIdentifier("resname", "restype", com.example.whatever);
}

This returns 0 EVERY TIME, so it's not going to work

I'd like to get the name and integer identifier for every picture in the /res/drawables folder. Can this be done WITHOUT KNOWING ANY FILE NAMES?

----------------------------------------------------------------------

[adding this after the question was resolved to help anyone that may run into the same issue in the future. It just shows that it does in fact work.]

    Class resources = R.drawable.class;
    Field[] fields = resources.getFields();
    String[] imageName = new String[fields.length];     
    int index = 0;
    for( Field field : fields )
    {
        imageName[index] = field.getName();
        index++;
    }

    int result = 
            getResources().getIdentifier(imageName[10], "drawable", "com.example.name");

解决方案

Well, if you have to do it, use reflection :)

Class resources = R.drawable.class;
Field[] fields = resources.getFields();
for (Field field : fields) {
    System.out.println(field.getName());
}

However, in a way, you are still hard-coding stuff, since you will only be putting a fixed set of drawables in your app. :)

 
精彩推荐
图片推荐