2010-08-31 117 views
2

我被困在一個奇怪的場景,而開發Android應用程序。動態加載圖像

我的應用程序,從互聯網下載圖像並在應用程序中以縮略圖格式填充它。

由於我不知道在設計時圖片/縮略圖的數量,我使用下面提到的代碼動態創建圖像視圖控件。

ArrayList<String> parsedPhoto=oPhotoList.getPhoto(); 
RelativeLayout media = (RelativeLayout)findViewById(R.id.media); 
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
    RelativeLayout.LayoutParams.MATCH_PARENT, 
    RelativeLayout.LayoutParams.WRAP_CONTENT); 

for(int iCounter=0;iCounter<parsedPhoto.size();iCounter++) 
{ 
    bitmap=oParser.DownloadImage(parsedPhoto.get(iCounter)); 
    ImageView img = new ImageView(this); 
    img.setImageBitmap(bitmap); 
    media.addView(img,p); 
} 

但在這種情況下,我所有的圖像都會相互重疊。 其中我想要3個圖像顯示在每一行,我可以通過XML來實現。

有人可以幫我。

回答

0

不使用RelativeLayout,而是使用GridView和一個適配器。它不僅可以提高效率,還可以添加許多其他功能(不要下載已下載的圖像,下載元數據等)

0

問題是你不是在RelativeLayout中定位圖像。有了這個佈局,你必須指定你想要定位每個元素的位置。如果你不這樣做,他們會位於同一個位置。

有像GridView這樣的視圖,可以讓你以更簡單的方式做到這一點。

但是,如果您願意,可以使用RelativeLayout。您可以使用RelativeLayout.LayoutParams類的addRule()方法來指定每個圖像的位置。例如:

首先,您可以存儲添加圖像的最後一個ID。

// The id of the first image added in the last row 
int lastFirstId; 
// The id of the last image added 
int lastId; 

然後,創建並指定ImageView的位置(與第一圖像的一些例外):

RelativeLayout.LayoutParams params = new RelativeLayoutParams(); 
params.setRule (params.RIGHT_OF, lastId); 
params.setRule (params.BELOW, lastFirstId); 
img.setLayoutParams(params); 

我希望它能幫助