简体   繁体   English

如何在不阻塞主线程的情况下获得流畅的动画?

[英]How to have a smooth animation without blocking Main thread?

Basically, in my activity, there are two vies, one is a surface view that animates and the other is a button which has an animation. 基本上,在我的活动中,有两个方法,一个是动画的表面视图,另一个是具有动画的按钮。

Originally i created an async task in a surface view, and had it 'draw' in the 'onprogressupdate' method. 最初,我在表面视图中创建了一个异步任务,并在“ onprogressupdate”方法中对其进行了“绘制”。 To my understanding this is how it is meant to be done. 据我了解,这就是它的含义。

However whenever i press the button is animated , the surface view stops, as if the animation of the button is stopping the animation of the surface view, which seems logical. 但是,每当我按下该按钮动画时,表面视图就会停止,就好像按钮的动画正在停止表面视图的动画一样,这似乎是合乎逻辑的。

To overcome this i put the 'draw' back into the 'run in background' . 为了克服这个问题,我将“抽奖”放回“后台运行”中。 I have a feeling this is obviously wrong. 我感觉这显然是错误的。 Is there a way I can run the two animations without blocking each other? 有没有办法我可以运行两个动画而又不会互相阻塞?

You should not access the Android UI toolkit from outside the UI thread. 您不应从UI线程外部访问Android UI工具包。 If you want to manipulate a View directly from outside the UI thrad, you should use View.post(Runnable) instead. 如果要直接从UI界面外部操作View ,则应改用View.post(Runnable) For example, 例如,

public void onClick(View v) {
    new Thread(new Runnable() {
        public void run() {
            mButton.post(new Runnable() {
                public void run() {
                    // do something here
                }
            });
        }
    }).start();
}

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

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