來檢索資源ID爲「R.drawable.a ##」動態,我們可以使用Resources.getIdentifier
如下:
final String pkg = context.getPackageName();
final Resources resources = context.getResources();
...
int num = ...; /* between 43 and 54 */
final int id = resources.getIdentifier("a" + num, "drawable", pkg);
您可以將這些在List
使用循環就像你現在有一個存儲,只有稍加修改的邊界:
final String pkg = context.getPackageName();
final Resources resources = context.getResources();
final List<Bitmap> bitmaps = new ArrayList<Bitmap>();
for (int i = 43; i <= 54; ++i) {
/* decode bitmap with id R.drawable.a{i} */
final Bitmap bitmap = BitmapFactory.decodeResource(resources,
resources.getIdentifier("a" + i, "drawable", pkg));
bitmaps.add(bitmap);
}
/* now bitmaps contains the Bitmaps */
任何人都可以幫助我? –
有什麼不對?你爲什麼不接受? – oldrinb