Iterate through certain images in res/drawable-mdpi

走远了吗. 提交于 2019-12-23 04:35:34

问题


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);
}

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

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


回答1:


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



来源:https://stackoverflow.com/questions/26947392/iterate-through-certain-images-in-res-drawable-mdpi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!