Android Animation principle Analysis

Source: Internet
Author: User

Recently on the Android to do some animation effect, on-line check some information, there are a variety of use, so the hot iron, want to specifically analyze how the animation is realized, Animation, animator are what differences and so on.

First say Animation(android.view.animation.Animation) object.

Whether you build animation objects with pure Java code or define animation through XML files, the end result is

New NEW Newtranslateanimation (); 

are transparency, scaling, rotation, and displacement of four animation effects.

And when we use it, it's usually in this form:

View.startanimation (a);

Then take a look at the startanimation () method in view.

1.view.startanimation (Animation)

First call the view.setanimation (Animation) method to set itself a Animation object, which is a member variable named mcurrentanimation in the View class.

It then calls invalidate () to redraw itself.

I think, since Setanimation (), then it will be used, it must be getanimation (), find out where this method is called. So through the search, in the View.draw (Canvas, ViewGroup, Long) method found its call, the code snippet is as follows:

2.View.draw (Canvas, ViewGroup, long)

The view.drawanimation () method is called.

3.view.drawanimation (ViewGroup, Long, Animation, Boolean)

The code snippet is as follows:

The animation.gettransformation () method is called.

4.animation.gettransformation (long, transformation, float)

The method calls the two parameter animation.gettransformation () method directly.

5.animation.gettransformation (long, transformation)

The method first processes the parameter currenttime into a float to indicate the current animation progress, for example, a 2000ms animation, has executed 1000ms, then the progress is 0.5 or 50%.

The progress value is then passed into the Interpolator (interpolator) to obtain a new progress value, the former is uniform, with time is a linear relationship, and through the interpolation calculation is a curve of the relationship.

The new progress values and transformation objects are then passed into the applytranformation () method.

6.animation.applytransformation (float, transformation)

Animation's Applytransformation () method is an empty implementation, specifically implementing it is the four subclass of animation, which is really the process of processing animation changes. Take a look at the implementation of the Applytransformation () of the four sub-classes respectively.

Scaleanimation

Alphaanimation

Rotateanimation

Translateanimation

Visible applytransformation () method is the implementation of the animation, the system will be a relatively high frequency to call this method, generally 60FPS, is a very smooth picture, that is, 16ms, To verify this, I validated the code in the Applytransformation method by adding a compute interval and printing the code as follows:

The resulting log is as follows:

On the right is the " manual " Calculation of the time difference, there is a certain fluctuation, but is generally 16-17ms appearance, the left is the log print, time is very regular difference of 20ms.

Therefore, according to the above results, the following can be obtained:

1. First of all, it proves that the animation.applytransformation () method, which is an animation-specific method of invocation, allows us to override this method and quickly make our own animations.

2. Another point, why is this method called around 16ms? Who's going to control the frequency?

For the above questions, I have two guesses:

1. This method is called by the system itself in the form of postdelayed (this,) . For specific wording, refer to "Using thread to implement view smooth scrolling"

2. System A dead loop crazy call, run a series of methods to go to this position interval is just about 16MS, if the main thread card, this interval becomes longer.

In order to find the answer, I posted a question on the stack overflow and then got an intelligence that let me take a look at the choreographer (Android.view.Choreographer) class.

1.construction method of choreographer

See the next choreographer class of construction method, is private, do not allow new external class new, and then found that it has a static getinstance () method, then I need to find getinstance () When the method is called, you can know where the choreographer object is being used. The Choreographer.getinstance () was found to be called in the Viewrootimpl construction method . The following code is a fragment of Viewrootimpl.viewrootimpl (Context, Display) .

OK, find the Viewrootimpl has a mchoreographer object, next, I need to find out how it was used, and what methods it was called. Then found the following code:

In the scheduletraversals () method, the use of this object was found.

2.choreographer.postcallback (int, Runnable, Object)

This method calls the two internal methods, and finally calls the choreographer.postcallbackdelayedinternal () method.

3.choreographer.postcallbackdelayedinternal (int, object, object, long)

In this method,

1. First get the current time.

There is a delay in the parameter, its value can be viewed in detail, you will find it is a static constant, defined in the choreographer class, its value is 10ms. In other words, ideally, all the time is running at 100FPS.

2. Add the content that will be executed to a mcallbackqueues .

3. Then execute scheduleframelocked () or send a message.

Then we look at the choreographer.scheduleframelocked (long) method

4.choreographer.scheduleframelocked (Long)

If the part of the if is judged whether to use vertical synchronization, temporarily do not consider.

The part that else goes in, or sends the message to the Mhandler object. Well, let's just look at the Mhandler object.

5.Choreographer.framehandler

The type of the Mhandler instance is Framehandler, which is defined in the choreographer class with the following code:

It has three branches in its processing, but eventually calls the Doframe () method.

6.Choreographer.doframe ()

Doframe () method Balabala a large paragraph, but in the following there is a very neat piece of code, a bit attracted my eyeballs.

It calls three times the docallbacks () method, but does not say what the method is doing, but from its first argument you can see that the input, animation (ANIMATION), and traversal (traversal)are respectively.

So, first I looked at the meaning of these three constants. As shown:

Obviously, the comment is that the input event is processed first, then the animation is processed, and the layout and drawing of the view is processed at the end. Next we look at what choreographer.docallbacks () has done.

7.choreographer.docallbacks (int, long)

The operation of this method is very uniform, there are three different types of operations (input, animation, traversal), but there is no visible traces of these specific events, here we have to analyze the mcallbackqueues this member variable.

Mcallbackqueues is an array of Callbackqueue objects . And its subscript, its meaning does not mean element 1, element 2, Element 3 ... It refers to the type , see the code above Docallbacks (), the parameter Callbacktype passed to Mcallbackqueues[callbacktype] , And what is Callbacktype?

In fact, the three constants mentioned above,callback_input, callback_animation, callback_traverval.

Then you can take different types of Callbackqueue objects from this array, depending on the Callbacktype.

So what is callbackqueue ?

Callbackqueue is an inner class of choreographer , which I think has two very important methods, namely:extractduecallbackslocked (Long) and addcallbacklocked (Long, Object, object).

First say addcallbacklocked (Long, Object, object).

1.callbackqueue.addcallbacklocked (Long, Object, object)

First it constructs a Callbackrecord object through an internal method, and then the if judgment and the while loop, which is roughly the end of the link to the object in the parameter Callbackrecord. In fact, Callbackrecord is the object of a linked list structure.

2.callbackqueue.extractduecallbackslocked (Long)

This method is based on the current time, to select the execution of the list of the most recent operation of the time to deal with, in fact, we can be popularly understood as " skip frame ."

Imagine that if the main thread runs very fast, very smooth, and every step can be run on time within 10ms, then the elements in our execution list will always be one.

If the main thread takes a time-consuming action, then the various events have been added to their linked list, but when the main thread has empty to execute, and found that the list has accumulated so many overdue events, then directly select the last one to execute, then the interface looks like a bit.

So far, we have come to the following conclusion:

1. Control the external input event processing, animation execution, UI changes are processed in the same class, that is, choreographer, which specifies an ideal run interval of 10ms, because the various operations take a certain amount of time, so the external execution interval statistics is about 16ms.

2. There are three linked lists in the choreographer object that hold the input events to be processed, the animated events to be processed, and the traversal events to be processed.

3. Each time the execution, choreographer will be based on the current time, only the last event in the list of events, when there is time-consuming operation in the main thread, the event can not be executed in time, there will be so-called "skip frame", "lag" phenomenon.

The common method of 4.Choreographer, Postcallback (Callbacktype, Object), is the way to place events in the event list. and Doframe () is the method that consumes these events.

Now, it has been explored a lot of useful details. Here, and give yourself a question, according to the above facts, then, only need to find what to put in these three linked list of events?

then further explore. We just need to find out which methods are called by Postcallback () .

So click here to enumerate the methods of calling Postcallback () via Grepcode.

Understand the working principle of choreographer, and then to see the realization of objectanimator,valueanimator, it is very easy.

The Objectanimator.start () method actually calls the Valueanimator start () method several times, Valueanimator.start () A temporary variable, animationhandler.start (), is also called.

Animationhandler is actually a runnable, where the start () method calls Scheduleanimation ().

And this method:

The Postcallback () method is called.

After the This (Runnable) post, it is actually the Runnable.run () method that must be executed

The Doanimationframe () method is also called in the Run () method. This method specifically implements the process of animating a frame, and then calls the Scheduleanimation () method again.

is equivalent to postdelayed (this, 16) in this way.

So far, the analysis of the principle of animation to this end, originally only want to analyze the implementation of the animation process, did not expect to study the choreographer of the work principle, today harvest or a lot.

In fact, there are a lot of questions, technical learning is not a day, relying on the accumulation of daily slowly, I believe that someday, all kinds of doubts will be solved.

Android Animation principle Analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.