2016-08-12 93 views
-1

我想創建一個動態decodeResource在對象中使用非常圖像。如何創建動態decodeResource?

這是我的代碼:

for(int i=42; i<55; i++) { 
      bitmap = BitmapFactory.decodeResource(context.getResources(), 
        Integer.parseInt("R.drawable.a"+i)); 
     } 

我想要得到的文件

R.drawable.a43 to a54 

其可能創造decodeResource一個循環?

+0

任何人都可以幫助我? –

+0

有什麼不對?你爲什麼不接受? – oldrinb

回答

2

來檢索資源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 */