Reputation: 485
How should I do if i want to switch images depending of the position.
I got a for loop, going from 0 to any number, and the idea its that the image will switch depending of the value,
eg.
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
and so on, always switching in order between those 3 images
Thanks in advance
Upvotes: 0
Views: 277
Reputation: 1737
Supose your images are on drawble folder and you will put them on some view on your activity.
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]))
}
}
Upvotes: 1
Reputation: 4233
use module arithmetic. For better understanding you can see how circular queue has been implemented
0%3=0
1%3=1
2%3=2
3%3=0
4%3=1
5%3=5
6%3=0
So you can see that number will be in range of 0-2 for module 3, 0-3 for module 4
Upvotes: 0
Reputation: 9035
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;
}
Upvotes: 0