迭代图像

时间:2011-11-02 15:54:19

标签: java android

我有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);
            }
        });
}

3 个答案:

答案 0 :(得分:1)

你可以把它们放在一个数组中(找到如何添加到数组的信息here),然后用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
}

答案 1 :(得分: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());

}

答案 2 :(得分:0)

根据OP的要求,从评论中回复copypasta:

  

查看How do I iterate through the id properties of R.java class?Android: Programatically iterate through Resource ids

......虽然这个问题应该只是作为一个副本关闭。