Image view image need to change periodically in android - android

I have an image view. So in the image view i want to change the images after a certain time period. Images are coming from an array list. Now when the no of images are 3 or more than 3, it is working perfect. But when it is 2, my logic is not working. Second image is visible for a moment and then again changed to first image here is my code:
r = new Runnable(){
int i = 0;
public void run(){
iv.setImageBitmap(alBmps.get(i));
i++;
if(i >= alBmps.size()){
i = 0;
}
iv.postDelayed(r, 5000); //set to go off again in 5 seconds.
}
};
iv.postDelayed(r, 1000);
Can any one help me what changes i need on the above code?
Thanks.

Try this
declare variables
static int i=0;
private Timer myTimer;
in your onCreate or on button click where you want to call and start the methods
myTimer = new Timer();
myTimer.schedule(new TimerTask()
{
#Override
public void run()
{
TimerMethod();
}
}, 500, 5000);
add these methods to your class
private void TimerMethod()
{
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable()
{
public void run()
{
if(i<alBmps.size())
{
iv.setImageBitmap(alBmps.get(i));
}
else
{
i=0;
iv.setImageBitmap(alBmps.get(i));
}
i++;
}
};

Related

Can I slide an android viewpager within a different time slot?

I want to slide my an android viewpager within a different slide slot.It means first page comes after the 5 second and second page will be appearing in 8 second.I need to slide viewpager but that slides are need to come within different time frame.is it possible to do that thing? Any help to slow this error would be highly appreciated.
I did the following code segment.But it will change viwepager for some constant time period.
final Handler handler = new Handler();
final Runnable Update = new Runnable() {
public void run() {
if (currentPage == signageResourceStoreModelList.size()) {
currentPage = 0;
}
try {
viewPager.setCurrentItem(currentPage, true);
} catch (Exception e) {
e.printStackTrace();
}
currentPage = currentPage + 1;
}
};
timer = new Timer();
timer.schedule(new TimerTask() { // task to be scheduled
#Override
public void run() {
handler.post(Update);
}
}, DELAY_MS, PERIOD_MS);
}
Yes, definitely you can do this by some programming logic.
Declare a class variable
for eg. int time=2000;
To change the view inside viewpager programmatically
public void MoveNext(View view) {
pager.setCurrentItem(pager.getCurrentItem() + 1);
//Write logic of incrementing time here
//e.g.time=time+1000;
}
Now define a handler
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
MoveNext();
}
},time);

Timer not working properly in android

Any one give idea, Timer will be schedule based on duration array and if once complete duration array length again will be start from first. In this some loop working perfectly some times will be crash and through arrayoutofboundsexception. Any one cane Help Me???
int[] duration={10000,2000,8000};
int layoutIncrment=-1;
private void layoutRotate()
{ layoutIncrment++;
Timer timer = new Timer();
timer.schedule(new TimerTask()
{ public void run()
{
if((duration.length-1) <= layoutIncrment)
layoutIncrment=-1;
layoutRotate();
Log.i("Rotation",String.valueOf(layoutIncrment));
} },duration[layoutIncrment],100000);
}
why dont u try like this. it will keep running in the while loop.. thats only u want. right?
int n=1;
while(n>0)
{
for(int i=0;i<duration.length;i++)
{
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
//add ur code here
}
}, duration[i]);
}
}

I need to load multiple images, one by one, at a set interval

It is a basic app where you click the shoot button when a picture appears. A picture appears once every 5 seconds. However, all of my images are loading at the same time using the code below.
I call loadImage to load an image, and then shootHandler to remove it after 5 seconds. I'm not sure if I need to use AsyncTask or something like that.
void run() {
int i;
for(i = 0; i < imageList.length; i++) {
loadImage(i);
shootHandler(i);
}
}
private void shootHandler(final int trialNumber) {
loadImage(trialNumber);
handler.postDelayed(new Runnable() {
public void run() {
resetButtons(trialNumber);
}
}, 5000);
}
private void loadImage(int trialNumber) {
Glide.with(getApplicationContext()).load(R.drawable.red_btn).into(shoot);
int resID = getApplication().getResources().getIdentifier(imageList[trialNumber].name, "drawable", getApplicationContext().getPackageName());
Glide.with(getApplicationContext()).load(resID).into(imageList[trialNumber].image);
}
You seem to expect that postDelayed blocks the calling thread, but that is false.
If you want to call these one after another, each call should schedule the next one. See https://stackoverflow.com/a/5996270/4388512
i couldn't figure out what is your point but to load Images in the prettiest way you can use this:
final Handler handler = new Handler();
new Runnable(){
int counter = 0;
#Override
public void run() {
counter++;
if(counter == list.size()){
return;
}
Picasso.with(context).load(list.get(counter - 1)).into(imageView);
handler.postDelayed(this, 5000);
}
}.run();

Show custom dialog in for loop

I want to show custom dialogs in order. Each dialog must shown for 5 second then it must be dismissed and the other one must be shown. I use for loop and my code looks like:
for(int i = 0 ; i < 10 ; i++){
popupView.show();
SystemClock.sleep(3000);
popupView.dismiss();
SystemClock.sleep(1000);
}
Try this code and adjust the time interval as your need.
showDialogs();
final Handler handler = new Handler();
Timer timer = new Timer();
TimerTask doShowDialog = new TimerTask() {
#Override
public void run() {
handler.post(new Runnable() {
public void run() {
alertDialog.dismiss();
if (mDialogCounter < 5) {
showDialogs();
mDialogCounter++;
}
}
});
}
};
timer.schedule(doShowDialog, 5000, 5000);

Android Imageswitcher: switch images periodically?

I am using an ImageSwitcher with a TouchListener to change images from an array. Its working fine but i want it to switch images every x seconds or so, so that I can add imageSwitcher.setImageResource(imageList[curIndex]); to it.
Any suggestions?
Try this,
imageSwitcher.postDelayed(new Runnable() {
int i = 0;
public void run() {
imageSwitcher.setImageResource(
i++ % 2 == 0 ?
R.drawable.image1 :
R.drawable.mage2);
imageSwitcher.postDelayed(this, 1000);
}
}, 1000);
I think it is possible via TimerTask and Timer. please Try this code. I think It help you.
private Handler mHandler;
private Runnable mUpdateResults;
private Timer timerAnimate;
private TimerTask timerTask;
mHandler = new Handler();
mUpdateResults = new Runnable() {
public void run() {
AnimateandSlideShow();
}
};
int delay = 0;
int period = 15000;
timerAnimate = new Timer();
timerTask = new TimerTask() {
public void run() {
mHandler.post(mUpdateResults);
}
};
timerAnimate.scheduleAtFixedRate(timerTask, delay, period);
Public void AnimateandSlideShow()
{
imageSwitcher.setImageResource(imageList[curIndex]);
///Here You need To handle curIndex position.
}

Categories

Resources