2016-04-03 48 views
0
ArrayList<Bitmap> images = new ArrayList<Bitmap>(); 
images.add(BitmapFactory.decodeResource(getResources(), R.drawable.uzayli_1)); 
images.add(BitmapFactory.decodeResource(getResources(), R.drawable.uzayli_2)); 

mForeground = images.get(0); 
mBackground = images.get(1); 

我可以將圖片添加到ArrayList,但我想在其他類中使用該類,所以我必須爲它編寫一個方法。使用方法將圖像添加到位圖ArrayList?

public void addImages(ArrayList<Bitmap> images){ 
    for (int i = 0; i < images.size(); i++) { 
     images.get(i); 
     mPages.add(BitmapFactory.decodeResource(getResources(), R.drawable.)); 
    } 
} 

我想要類似這樣的東西,但是我怎樣才能定義可繪製的圖像之後,R.drawable。在一個方法?

+0

什麼是mPages? –

回答

0

你已經解碼了一次,所以使用來自ArrayList之一:

Bitmap bmp = images.get(i); 
mPages.add(bmp); //assuming that's legal 

或保留資源的ID在Integer列表:

ArrayList<Integer> images = new ArrayList<Integer>(); 
images.add(R.drawable.uzayli_1); 
images.add(R.drawable.uzayli_2); 

後來解碼:

public void addImages(ArrayList<Integer> imageIds){ 
    for (int i = 0; i < imageIds.size(); i++) { 
     Integer resourceId = imageIds.get(i); 
     mPages.add(BitmapFactory.decodeResource(getResources(), resourceId)); 
    } 
} 

但你不能這樣做。