2011-11-02 113 views
2

我有100個圖像,每個圖像的百分比,例如1%,2%,3%等。遍歷圖像

什麼是通過每個圖像的最佳方式?我應該將每個圖像資源添加到列表或詞典(如果存在於Android中)。還是我被迫硬編碼?

public void ShowCircle(final int percentage){ 
    final ImageView ring = (ImageView)findViewById(R.id.ring); 
    ring.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       for(int i = 0; i != percentage; i++) 

          //I was thinking here I can access list/dictionary records. 
       ring.setImageResource(R.drawable.percent_1); 
      } 
     }); 
} 
+1

看看[如何遍歷R.java類的id屬性?](http://stackoverflow.com/questions/2941459/how-do-i-iterate-through-the-id- r-java-class的屬性)和[以編程方式遍歷資源ID](http://stackoverflow.com/questions/3545196/android-programatically-iterate-through-resource-ids)。 –

+0

@MattBall - 謝謝,'我如何迭代R.java類的id屬性?'有我正在尋找的答案。 – Kukoy

+0

我使用了MattBall建議的以下資源。 http://stackoverflow.com/questions/2941459/how-do-i-iterate-through-the-id-properties-of-r-java-class – Kukoy

回答

1

你可以把它們放入數組(關於如何添加到一個數組信息發現here),然後重複trhough他們的foreach循環,像這樣:

for(ImageView image : ImageArray) { 
    // Do stuff with your image that is now in the variable called image. 
    // All images will be called in the order that they are positioned in the array 
} 
0

我會把他們成這樣

public static int[] picArray = new int[100]; 
picArray[0] = R.raw.img1; 
picArray[1] = R.raw.img2; 
//etc  

陣列和遍歷它們這樣

public void onClick(View view) 
{ 

    ImageView image = (ImageView) findViewById(R.id.imageView1); 

    if(counter== (picArray.length - 1)) 
    { 
     counter =0; 


    } 
    else 
    { 
     counter++; 

    } 
    image.setImageResource(picArray[counter].getImg()); 

}