Java 动画器只能在 Android 的 Looper 线程上运行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37689903/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:33:32  来源:igfitidea点击:

Animators may only be run on Looper threads Android

javaandroidmultithreadingandroid-animation

提问by Adrien Zier

I'm trying to animate something when a task is completed. The problem here is I get this error message:

我正在尝试在任务完成时制作动画。这里的问题是我收到此错误消息:

android.util.AndroidRuntimeException: Animators may only be run on Looper threads
   at android.animation.ValueAnimator.cancel(ValueAnimator.java:1004)
   at android.view.ViewPropertyAnimator.animatePropertyBy(ViewPropertyAnimator.java:965)
   at android.view.ViewPropertyAnimator.animateProperty(ViewPropertyAnimator.java:921)
   at android.view.ViewPropertyAnimator.alpha(ViewPropertyAnimator.java:735)
   at com.design.zaton.prototypei.MainActivity.run(MainActivity.java:93)
   at java.lang.Thread.run(Thread.java:761)

The app worked fine before with the same exact code but now it simply doesn't. I'm really confused.

该应用程序之前使用相同的确切代码运行良好,但现在根本没有。我真的很困惑。

Here's where the error happens:

这是发生错误的地方:

new Thread(new Runnable() {
    @Override
    public void run() {
        final String s = getGiphyViews(String.valueOf(mEdit.getText()));
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                result.setText(s);
            }
        });

        loading.animate()
                .alpha(0)
                .setDuration(100);

        done.animate()
                .scaleY(1)
                .scaleX(1)
                .setDuration(300);
    }
}).start();

The error outlines the loading.animate()method.

该错误概述了该loading.animate()方法。

Thanks in advance!

提前致谢!

采纳答案by Guruprasad Rao

I think, there has been a solutionfor this using Handler. You can use postDelayedto minimal as 100and run your animating tasks. In your case it would be:

我想,出现了一个解决这个使用Handler。您可以使用postDelayed最小化100并运行您的动画任务。在您的情况下,它将是:

new Handler().postDelayed(new Runnable() {
     @Override
     public void run() {
        final String s = getGiphyViews(String.valueOf(mEdit.getText()));
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                result.setText(s);
            }
        });

        loading.animate()
                .alpha(0)
                .setDuration(100);

        done.animate()
                .scaleY(1)
                .scaleX(1)
                .setDuration(300);
     }
}, 100);

I had this problem today and above work resolved the problem. I would love to hear from anyone if there is any problem with this method.

我今天遇到了这个问题,上面的工作解决了这个问题。如果这种方法有任何问题,我很乐意听取任何人的意见。

回答by DeeV

Looper threadsare threads in Android that permanently loop (or until you cancel them). They work in conjunction with Handlerswhich will post and send messages to Looperthreads. Animators use heavy use of Looper threads because they perform their actions in repeated cycles. This allows the animator to not block after you press "start" so you can continue to perform other actions.

Looper 线程是 Android 中永久循环(或直到您取消它们)的线程。它们与处理程序一起工作,处理程序将向Looper线程发布和发送消息。动画师大量使用 Looper 线程,因为它们以重复的循环执行动作。这允许动画师在您按“开始”后不会阻止,因此您可以继续执行其他操作。

To further complicate matters, you most likely are performing animations on Viewobjects. These can only be run on the main UI thread (which happens to be the biggest Looperthread of them all). So, you can not run these animations on separate threads like you are trying.

更复杂的是,您很可能正在对View对象执行动画。这些只能在主 UI 线程上运行(这恰好是Looper它们中最大的线程)。因此,您不能像您尝试的那样在单独的线程上运行这些动画。

回答by tuzhao

I think you can create a main handler instance in your activity and override handle message method. you can save a handler quote in your work thread.When you finished work you can use handler to send a message and you receive message in handle message method. To start animation from handler message method and so on...

我认为您可以在您的活动中创建一个主处理程序实例并覆盖处理消息方法。您可以在工作线程中保存处理程序引用。完成工作后,您可以使用处理程序发送消息,并在处理消息方法中接收消息。从处理程序消息方法等开始动画......

回答by Sai Gopi N

Put Code Where Are getting error and doing any network operations

将代码放在何处 出现错误并执行任何网络操作

new Handler().post(new Runnable() {
        @Override
        public void run() {
            // add your code here 
        }
    });

回答by Davide

runOnUiThread(new Runnable() {
            @Override
            public void run() {
                //Your code
            }
});

You have to execute the code in the UI Thread

你必须在 UI 线程中执行代码