I want to loop between two images continuously. The first image should be replaced by the second image and the second by the first infinitely.
The code that I have so far is as follows:
imageHandler = new Handler();
imageHandler.postDelayed(new Runnable() {
@Override
public void run() {
Picasso.with(TokenActivity.this).load(R.drawable.app_icon).into(iconImageView);
}
}, 1000);
imageHandler.postDelayed(new Runnable() {
@Override
public void run() {
Picasso.with(TokenActivity.this).load(R.drawable.camera_icon).into(iconImageView);
}
}, 2000);
Here the flip is happening but only once. How do I program it so that it can repeat infinitely?
Declare two runnable :
Runnable goToImage2;
Runnable goToImage1 = new Runnable() {
@Override
public void run() {
Picasso.with(TokenActivity.this).load(R.drawable.app_icon).into(iconImageView);
handler.postDelayed(goToImage2, 2000);
}
}
goToImage2 = new Runnable() {
@Override
public void run() {
Picasso.with(TokenActivity.this).load(R.drawable.camera_icon).into(iconImageView);
handler.postDelayed(goToImage1, 2000);
}
}
Then simply launch the first one :
handler.postDelayed(goToImage1, 0);
EDIT : If you only need two images, you can also simply use a boolean to know which one to display, and therefore only use one runnable :
boolean isShowingFirst = true;
Runnable changeImage = new Runnable() {
@Override
public void run() {
Picasso.with(TokenActivity.this).load(isShowingFirst ? R.drawable.camera_icon : R.drawable.app_icon).into(iconImageView);
isShowingFirst = !isShowingFirst;
handler.postDelayed(changeImage, 2000);
}
}
And launch it with :
handler.postDelayed(changeImage, 0);