简体   繁体   中英

Thread.sleep NOT working Android

I have an objectAnimator

final ObjectAnimator anim = (ObjectAnimator) AnimatorInflater
                .loadAnimator(getActivity(), R.anim.flip);
        anim.setTarget(tvDebitAmount);
        anim.setDuration(3000);
        anim.start();

I need this to work in a loop, with some gap. I mean , once this animation finishes I need to call anim.start again after a wait of 4000 ms.

I tried putting it in a infinite loop , and put thread.sleep() after anim.start(). It's not working(whole screen is not responding). How can I make it wait/sleep ?

Thread.sleep() blocks the current thread which is the UI thread and you don't want that.

To make the animation work the way you want, just wrap it in an animation set that runs 4000 ms longer while the actual animation in the set still takes only 3000 ms. Then set the animation repeat mode to RESTART and repeat count to INFINITE .

I recommend you to use ScheduledThreadPoolExecutor or else use the Timer . So that it works in seperate thread with out disturbing the main thread. Get an example about timer here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM