简体   繁体   中英

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. 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. If you want to manipulate a View directly from outside the UI thrad, you should use View.post(Runnable) instead. 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();
}

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