*更新 - 解决** 我在寻找的是如何获取资源ID。当你不知道你不知道什么时,很难提出一个问题,但是在几分钟内回答它的两个人似乎都明白了。谢谢,伙计们。
这是我最终能够使用的代码,无需加载所有32位图,同时使用String来识别我所追踪的特定位图。
StartCalc session = new StartCalc(context);
int findDate = session.findDate();
for (int i = 0; i < daysTil.length; i++) {
String numbers = "numbers" + i;
int resId = getResources().getIdentifier(numbers, "drawable", getPackageName());
if (i == findDate) {
ivNumbers.setImageBitmap(BitmapFactory.decodeResource(getResources(), resId));
}
}
*原始问题** 我正在尝试使用for循环来填充位图数组,从0到32,但我无法弄清楚如何使用字符串“数字”(在第3行标识)来填充BitmapFactory所需的地址(在第5行引用)。这是我填充数组两种方式的代码。漫长的方式会占用太多内存,因为我只需要基于日期的其中一个图像,我希望有一种方法可以做到这一点。
private void setNumbers() {
for (int i = 0; i < 33; i++) {
String numbers = "R.drawable.numbers" + i;
Log.e(TAG, numbers);
daysTil[i] = BitmapFactory.decodeResource(getResources(), numbers);
}
daysTil[0] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers0);
daysTil[1] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers1);
daysTil[2] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers2);
daysTil[3] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers3);
daysTil[4] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers4);
daysTil[5] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers5);
daysTil[6] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers6);
daysTil[7] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers7);
daysTil[8] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers8);
daysTil[9] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers9);
daysTil[10] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers10);
daysTil[11] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers11);
daysTil[12] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers12);
daysTil[13] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers13);
daysTil[14] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers14);
daysTil[15] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers15);
daysTil[16] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers16);
daysTil[17] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers17);
daysTil[18] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers18);
daysTil[19] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers19);
daysTil[20] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers20);
daysTil[21] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers21);
daysTil[22] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers22);
daysTil[23] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers23);
daysTil[24] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers24);
daysTil[25] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers25);
daysTil[26] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers26);
daysTil[27] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers27);
daysTil[28] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers28);
daysTil[29] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers29);
daysTil[30] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers30);
daysTil[31] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers31);
daysTil[32] = BitmapFactory.decodeResource(getResources(), R.drawable.numbers32);
}
答案 0 :(得分:2)
您应该使用此方法来查找drawable的资源ID:
String numbers = "numbers" + i;
int resId = getResources().getIdentifier(numbers, "drawable", getPackageName());
答案 1 :(得分:1)
Resources类有这个方法:
public int getIdentifier (String name, String defType, String defPackage)
所以完整的陈述看起来有点像这样:
for (int i = 0; i < 33; i++) {
String numbers = "R.drawable.numbers" + i;
Log.e(TAG, numbers);
Resources resources = <context>.getResources();
daysTil[i] = BitmapFactory.decodeResource(getResources(),
resources.getIdentifier ("numbers" +i, "drawable",
<context>.getPackageName());
}
(注意:此代码未经测试 - 仅用于演示)