2011-11-02 94 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()); 

}