简体   繁体   English

Thread.sleep无法正常工作的Android

[英]Thread.sleep NOT working Android

I have an objectAnimator 我有一个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. 我的意思是,一旦动画完成,我需要在等待4000毫秒后再次调用anim.start。

I tried putting it in a infinite loop , and put thread.sleep() after anim.start(). 我尝试将其置于无限循环中,然后将thread.sleep()放在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. Thread.sleep()阻止当前线程,即UI线程,您不希望这样。

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. 要使动画按照您想要的方式工作,只需将其包装在运行时间延长4000毫秒的动画集中,而该集中的实际动画仍然只需要3000毫秒。 Then set the animation repeat mode to RESTART and repeat count to INFINITE . 然后将动画重复模式设置为RESTART并将重复计数设置为INFINITE

I recommend you to use ScheduledThreadPoolExecutor or else use the Timer . 我建议您使用ScheduledThreadPoolExecutor或使用Timer So that it works in seperate thread with out disturbing the main thread. 这样它就可以在独立线程中工作而不会干扰主线程。 Get an example about timer here . 在此处获取有关计时器的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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