2011-10-24 220 views
3

我的佈局中有一組10個圖像。我給了他們順序的ID也作爲動態更改圖像視圖背景

android:id="@+id/pb1" 

android:id="@+id/pb2" 

現在我想動態改變背景。

int totalPoi = listOfPOI.size(); 
    int currentPoi = (j/totalPoi)*10; 
    for (i=1;i<=currentPoi;i++) { 
      imageview.setBackgroundResource(R.drawable.progressgreen); 
} 

現在在for循環中,我想動態設置圖像視圖背景。如果currentpoi值爲3,則應改變3個圖像視圖的背景。 for循環迭代了許多圖像視圖背景應該改變的時間。希望問題現在已經清楚。

注:我有一個需要被設置爲10個的圖像視圖

只有1個圖像progressgreen
+0

你能展示完整的代碼並清楚解釋嗎?你有多少可繪製的。列出可繪製的s。 –

+0

你的問題又是什麼? – Reno

回答

5

最後我做了這在t他下面的方式,

我把所有的ID陣列中作爲

int[] imageViews = {R.id.pb1, R.id.pb2,R.id.pb3,R.id.pb4,R.id.pb5,R.id.pb6,R.id.pb7,R.id.pb8,R.id.pb9,R.id.pb10}; 

現在:

int pindex = 0; 

for (pindex; pindex <currentPoi; pindex++) { 

    ImageView img = (ImageView) findViewById(imageViews[pindex]) ; 
    img.setImageResource(R.drawable.progressgreen); 
} 

現在,我能夠動態地改變圖像。

@ goto10。謝謝你的幫助。我會調試你的觀點,看看我的身邊出了什麼問題

0

你需要給你的ImageViews順序ID,如「@ + ID/PB1」和「@ + ID/PB2" ,等等。然後你就可以在這樣的循環讓他們每個人:

for (i=1;i<=currentPoi;i++) { 
    // Find the image view based on it's name. We know it's pbx where 'x' is a number 
    // so we concatenate "pb" with the value of i in our loop to get the name 
    // of the identifier we're looking for. getResources.getIdentifier() is able to use 
    // this string value to find the ID of the imageView 
    int imageViewId = getResources().getIdentifier("pb" + i, "id", "com.your.package.name"); 

    // Use the ID retrieved in the previous line to look up the ImageView object 
    ImageView imageView = (ImageView) findViewById(imageViewId); 

    // Set the background for the ImageView 
    imageView.setBackgroundResource(R.drawable.progressgreen); 
} 

替換com.your.package.name與應用程序的包。

+0

你能告訴我imageview.setBackgroundResource(R.drawable.progressgreen)中的imageview是什麼;? – tejas

+0

imageView在前一行中設置,該行根據ID查找ImageView。該ID是從ImageView的名稱計算出來的。我會在代碼中添加一些註釋以使其更加清晰。 – goto10

+0

是的,但有一個愚蠢的疑問,你指的pb是圖像視圖對象嗎? – tejas

0

試試這個代碼..... 創建圖像陣列..

private Integer[] mThumbIds = { R.drawable.bg_img_1, R.drawable.bg_img_2, 
     R.drawable.bg_img_3, R.drawable.bg_img_4, R.drawable.bg_img_5 }; 

而不是修改你的代碼

int totalPoi = listOfPOI.size(); 
int currentPoi = (j/totalPoi)*10; 
for (i=1;i<=currentPoi;i++) { 
     imageview.setBackgroundResource(mThumbIds[i]);} 
+0

這將多次改變同一圖像的背景。我不認爲這是所期望的。 – goto10

+0

你想從你的圖像陣列中隨機改變圖像? –

+0

他想根據currentPoi的值在多個ImageView上更改背景。如果它是3,那麼前10個圖像中的前三個應該改變其背景。你的代碼有5個背景,如果currentPoi是3,則更改一個圖像的背景3次。 – goto10

0

你可以讓你的ImageViews的數組,然後更改它們在你的循環。

ImageView views[] = new ImageView[10]; 
views[0] = (ImageView)findViewById(R.id.imageView0); 
... 
views[9] = (ImageView)findViewById(R.id.imageView9); 

,然後改變你的for循環:

for (i=1;i<=currentPoi;i++) { 
    views[currentPoi].setBackgroundResource(R.drawable.progressgreen); 
} 

陣列開始在索引0,所以一定要確保有沒有一個差一錯誤在這裏。

1

您可以通過設置可繪製的名義這樣做是這樣的: img_1,IMG_2,img_3 ...

for (i=1;i<=currentPoi;i++) 
{ 
ImageView imageview=(ImageView) findViewById(getResources().getIdentifier("imgView_"+i, "id", getPackageName())); 
imageview.setImageResource(getResources().getIdentifier("img_"+i, "drawable", getPackageName())); 
} 
+0

這不會在多個ImageView上設置背景。它在同一個ImageView上多次設置背景。 – goto10

1

創建一個ImageView的數組:

ImageView views[] = new ImageView[10]; 
views[0] = (ImageView)findViewById(R.id.pb1); 
... 
views[9] = (ImageView)findViewById(R.id.pb10); 

現在迭代循環,設置這樣的圖像的背景:

for (i=1;i<=currentPoi;i++) 
{ 
    views[i-1].setBackgroundResource(R.drawable.progressgreen); 
}