Java在3个图像之间切换

时间:2013-04-19 02:20:58

标签: java android

如果我想根据位置切换图像,该怎么办。

我有一个for循环,从0到任意数字,并且想法图像将根据值切换,

例如

    on position 0, it should display image 'A'
    on position 1, it should display image 'B'
    on position 2, it should display image 'C'
    on position 3, it should display image 'A' again
    on position 4, it should display image 'B' again

等等,总是在这3张图像之间按顺序切换

提前致谢

3 个答案:

答案 0 :(得分:1)

假设您的图片位于可绘制文件夹中,您将把它们放在活动的某个视图上。

int imagesRid = {R.id.imageA, R.id.imageB, R.id.imageC}
for(int i=0;i<anyNo;i++)
{
     view.setBrackground(context.getResources().getDrawable(imagesRid[i%imagesRid.lenght]))    

}

}

答案 1 :(得分:0)

int count=1;
for(int i=0;i<anyNo;i++)
{
    switch(count)
     {
           case 1:
               //display A
                break; 
           case 2:
              //display B
               break; 

           case 3:
              //display C
              break; 


    }
      count++;

     if(count>3)
     count=1;
}

答案 2 :(得分:0)

使用模块算术。为了更好地理解,您可以看到循环队列的实现方式

0%3=0
1%3=1
2%3=2
3%3=0
4%3=1
5%3=5
6%3=0

因此,您可以看到模块3的编号范围为0-2,模块4的编号范围为0-3