Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

are android animations always within the UI Thread?

I'd like to clarify using of view, propery, etc. animations in Android.

Usually, I create animation without any Tread's things - directly within my Activity/Fagment/ViewClass:

AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
alphaAnimation.setDuration(100);
mView.setAnimation(alphaAnimation);
alphaAnimation.start();

I understand this as an animation executing within main UI thread.

But if I will create an animation within a worker thread - will it be normal way?

public void onClick(View v) {
  new Thread(new Runnable() {
    public void run() {
      final AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
      alphaAnimation.setDuration(100);
      mView.post(new Runnable() {
        public void run() {
          mView.setAnimation(alphaAnimation);
          alphaAnimation.start();
        }
      });
    }
  }).start();
}
like image 261
atlascoder Avatar asked Mar 12 '16 21:03

atlascoder


People also ask

Do all components run on the same thread Android?

By default, all components of the same application run in the same process and thread (called the "main" thread).

Which action should be performed on the main thread?

The main thread is responsible for dispatching events to the appropriate user interface widgets as well as communicating with components from the Android UI toolkit. To keep your application responsive, it is essential to avoid using the main thread to perform any operation that may end up keeping it blocked.

What is the difference between main thread and UI thread in Android?

Originally Answered: What is difference between UI thread and main thread in Android? UI thread is what render UI component/Views. Main thread is what which start the process/app. In Android UI thread is main thread.

What is UI thread in Android?

User Interface Thread or UI-Thread in Android is a Thread element responsible for updating the layout elements of the application implicitly or explicitly. This means, to update an element or change its attributes in the application layout ie the front-end of the application, one can make use of the UI-Thread.


2 Answers

Your thread has no real effect here. The call to mView.post() essentially schedules that runnable to execute on the main thread in its next available cycle. You might as well save the effort of starting a new thread and do it all on the main thread. There is not much going on here.

Animations work by scheduling lots of tiny bits of work to run on the main thread with each frame, which is about every 16ms. This is very typical. Unless you are seeing a very specific problem that you need to optimize, there is no reason to avoid doing this kind of work on the main thread.

like image 142
Doug Stevenson Avatar answered Sep 21 '22 15:09

Doug Stevenson


Expanding on Doug's answer, and the explanations in the comments to his answer -

For maintaining a frame rate of 60 fps for smooth transitions and animations, we have ≈ 16 ms/frame to bring a frame to the screen. In these 16 ms, all sorts of things need to be done, including but not limited to processing user input, processing animations, measurement & laying out of different views, preparing and issuing display lists to the GPU, compositing windows, and finally displaying the output on the screen. And there is no time limit specifically for the animation stage (or any of the stages).

Also, only a specific amount of work needs to be done per frame per animation. For example, to rotate a view by 180 degrees in 1 s, we need to make calculations to rotate it by 3 degrees per frame. And this calculation is not interrupted in case it exceeds some time limit (even 16 ms). However if the entire process of frame rendering is unable to execute in 16 ms, then we see a frame drop.

Now if one of the stages takes too long to execute, it is possible to run them in a worker thread to free up the UI thread for other stages. But there's a catch - you cannot manipulate components from the Android UI toolkit (components from the android.widget and android.view packages) from a worker thread. So Android provides a post() method, to schedule methods from the worker thread to run on the UI thread. Refer to https://developer.android.com/guide/components/processes-and-threads for more on this topic.

Now what you are doing is not helping much, because as Doug said, you're scheduling the animation runnable to execute on the main thread. The only work done in the worker thread is

final AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
alphaAnimation.setDuration(100);

which is not particularly resource intensive, hence there is not much to gain by putting it in a separate thread.

like image 40
Dwait Bhatt Avatar answered Sep 23 '22 15:09

Dwait Bhatt