How to add wait time?

2019-08-03 06:20发布

问题:

The backgrounds of all the pictures are changing at the same time. I have to change one by one, wait 5 seconds after each picture, change the other,

ImageView[] imajlar={img1,img2,img3,img4,img5,img6,img7,img8};

for (final ImageView resmi:imajlar) {
//resmi.startAnimation(fadeout);
   new CountDownTimer(16000, 1000) {
   @Override
   public void onTick(long millisUntilFinished) {
        resmi.setBackground(hediye);
        resmi.startAnimation(fadein);
        onPause();
   }

   @Override
   public void onFinish() {

   }
   }.start();

回答1:

Use Handler for this. I hope this helps you.

new Handler().postDelayed(new Runnable(){
       @Override
       public void run() {
           changeImage();
       }
}, 5000);


回答2:

Try Timer with Handler

 Timer timer = new Timer();
            timer.scheduleAtFixedRate(new TimerTask() {
                @Override
                public void run() {

                   //Your code
                }
            }, 0, 5000);


回答3:

Try this code..

new Handler().postDelayed(new Runnable() {
    @Override
    public void run() {
        // define your code..
    }
},5000);


回答4:

Do it like

Define an array of images.

String img[]={img1,img2,img3};

and a variable

int i=0;

now in your method

changeImage()
{
if(i<img.length())
{
//Pseudo code(your logic of setting image)
setImage.array[i];
i++;
}
else
{
//This is to start again;
i=0
}
}

Then in you defined handler.

final Handler mHandler=new Handler();
ha.postDelayed(new Runnable() {

    @Override
    public void run() {
       changeImage();

        mHandler.postDelayed(this, 5000);
    }
}, 5000);


回答5:

int totalImageSize = yourTotalImageCount;

        final Handler mHandler = new Handler();
        mHandler.postDelayed(new Runnable() {

            @Override
            public void run() {
                changeYourImage(totalImageSize);// this will give the position of image to add/remove
                totalImageSize - 1;
                if(totalImageSize > 0)
                mHandler.postDelayed(this, 5000);
            }
        }, 5000);
    }