遍历res/drawable-mdpi中的某些图像 [英] Iterate through certain images in res/drawable-mdpi

查看:126
本文介绍了遍历res/drawable-mdpi中的某些图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

res/drawable-mdpi 文件夹中有26个字母图像,分别命名为 big_25.png .

In the res/drawable-mdpi folder I have 26 letter images, named big_0.png to big_25.png.

我想将它们全部添加(并忽略该文件夹中的任何其他图像)到哈希图中:

I would like to add them all (and ignore any other images in the folder) to a hash map:

private static HashMap<Character, Drawable> sHash = 
    new HashMap<Character, Drawable>();

最初,我正在计划类似的事情:

Initially I was planning something like:

private static final CharacterIterator ABC = 
    new StringCharacterIterator("ABCDEFGHIJKLMNOPQRSTUVWXYZ");

int i = 0;
for (char c = ABC.first(); 
    c != CharacterIterator.DONE; 
    c = ABC.next(), i++) {

        String fileName = "R.drawable.big_" + i;
        Drawable image = context.getResources().getDrawable(fileName);
        sHash.put(c, image);
}

但是后来我意识到R.drawable.big_0R.drawable.big_25的类型是 int 而不是 String .

But then I've realized that R.drawable.big_0 to R.drawable.big_25 are of type int and not String.

所以请告诉我,如何正确遍历26张图像?

So please advise me, how to iterate through the 26 images correctly?

推荐答案

使用getResources().getIdentifier()"R.drawable.big_" + i转换为将与getDrawable()一起使用的资源ID值.

Use getResources().getIdentifier() to convert "R.drawable.big_" + i into the resource ID value that you would use with getDrawable().

这篇关于遍历res/drawable-mdpi中的某些图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆