Learn more about the animated animation in Android (based on the Android source directory structure)

Source: Internet
Author: User

Android Animation If you carefully tidy up, is a very worthwhile learning module. Because of the content involved in more, we often in the actual development process, just a little grab part of the application, more fragmented, below I will follow the source code in the animation of the actual coding structure, the actual system of animation to explain, I hope that we understand the Andorid in the animation design concept has a certain understanding.

First Class: Animationthread

As the name implies, the essence of this class is a thread. We mainly focus on the following points:

1. Inside there is a static inner class, Messagebundle, which is actually a single entity that encapsulates the object that the current thread executes. A series of these entities will be stored in a priority queue. The order of arrangement is based on the order in which the tasks are executed.

I believe you are more concerned about this thread of the Run method. Take a look at the core code block in the Run method:

try {if (mlistener.iscanceled ()) {break;} Bundle.mTarget.handleMessage (Bundle.mmessage); if (Msession.render (False/* Freshrender */). Issuccess ()) { Mlistener.onnewframe (session);}} finally {  msession.release ();}

and the code base above is actually executed in the while loop until there is no element in the priority queue.

Class Two: Animator

According to the comments on Google Docs, this class is the parent class for classes that provide basic API support for animations, such as callbacks for Start, end, and animated events.

Class Three: Animatorinflater

As the name implies, the current class is the object used to initialize the animation that is loaded from the animated XML file, as noted above in the document.

In this class, we see the following constants:

private static final int TOGETHER = 0;private static final int sequentially = 1;

The first constant represents the parsed set of animations that are collectively animated, that is, without sequencing.

The second constant means that the parsed animation collection is animated in the order one after the other.

We can realize the meaning of these two variables from the following code block:

if (parent! = NULL && childanims! = null) {    animator[] Animsarray = new animator[childanims.size ()];    int index = 0;    for (Animator a:childanims) {        animsarray[index++] = A;    }    if (sequenceordering = = TOGETHER) {       parent.playtogether (animsarray);    } else {       parent.playsequentially ( Animsarray);}    }
Class Fourth: Animatorlisteneradapter

From the class name, we can also roughly understand that this class is the animation set behavior interface adapter. Of course, this is an abstract class, without any implementation.

Class Fifth: Animatorset

This class in the entire animation module is finally extremely complex, whether it is the logic of its internal processing, or its overall architecture. Let me give you a brief explanation:

1. The function of this class is to play a complete set of objects in the order specified. The order in which the animation is played can be a collective animation, a sequence animation, or a specified delay.

2, we are concerned about, in its internal there are so several sets of variables:

Private arraylist<animator> Mplayingset = new arraylist<animator> ();p rivate hashmap<animator, Node> Mnodemap = new Hashmap<animator, node> ();p rivate arraylist<node> mnodes = new arraylist<node> (); Private arraylist<node> msortednodes = new arraylist<node> ();

The first data structure tracks the collection of nodes that are currently playing the animation.

The second data structure contains all node nodes, mapped to the entities of their respective animations. When one of the animations needs to produce a dependency, when the object of the currently mapped node is present, then there is no need to recreate the object.

The third data structure is unordered node nodes. is the collection of node created for Animatorset.

The fourth data structure is the sequential node nodes.


The point that deserves attention is: Although the method here is many, but original aim, basically most of operation is inseparable from node this object. Whether it's playing, sorting, or increasing dependencies.

Let's take a look at the definition of node's data structure:

    private static class Node implements Cloneable {public Animator animation;        Public arraylist<dependency> dependencies = null;       Public arraylist<dependency> tmpdependencies = null;         public arraylist<node> nodedependencies = null;        Public arraylist<node> nodedependents = null;        public Boolean do = false;        Public Node (Animator animation) {this.animation = animation; } public void Adddependency (Dependency Dependency) {if (dependencies = = null) {Dependen                cies = new arraylist<dependency> ();            Nodedependencies = new arraylist<node> ();            } dependencies.add (dependency);            if (!nodedependencies.contains (Dependency.node)) {Nodedependencies.add (Dependency.node);            } Node Dependencynode = Dependency.node; if (dependencynode.nodedependents = = null) {Dependencynode.nodedependents = new arraylist<node> ();        } dependencyNode.nodeDependents.add (this);                } @Override Public Node clone () {try {node node = (node) super.clone ();                Node.animation = (Animator) animation.clone ();            return node;            } catch (Clonenotsupportedexception e) {throw new Assertionerror (); }        }    }

1, from the above-class member variables can be seen, the main association is two aspects, animation animator and dependency dependency.

2, focus on how node nodes are dependent. When a dependency is passed in as a parameter, it is first added to a temporary collection list, and then the node nodes in the current dependency are processed.

Class Sixth: Argbevaluator

This class, intuitively speaking, is a computational program of color values.

Class Seventh: Keyframe

Referring to the instructions on Google Docs, the general meaning of this class is that this class has mastered the value of an animation's time, and is used to define the value that the target has during the animation.

Its member variables are as follows:

float mfraction; Class mvaluetype;private timeinterpolator minterpolator = Null;boolean Mhasvalue = false;
The above four member variables mean roughly the following:

Mfraction represents the value stored by the current object. Mvaluetype represents the type of data that is stored. Minterpolator represents the frame's time interval. Mhasvalue represents whether the current object stores a numeric value.

To make it easy for everyone to understand, take one of them to analyze:

Static Class Intkeyframe extends Keyframe { int mvalue;            Intkeyframe (float fraction, int value) {mfraction = fraction;            Mvalue = value;            Mvaluetype = Int.class;        Mhasvalue = true;            } intkeyframe (float fraction) {mfraction = fraction;        Mvaluetype = Int.class;        } public int Getintvalue () {return mvalue;        } public Object GetValue () {return mvalue;                 } public void SetValue (Object value) {if (value! = null && value.getclass () = = Integer.class) {                Mvalue = ((Integer) value). Intvalue ();            Mhasvalue = true;                    }} @Override Public Intkeyframe clone () {Intkeyframe Kfclone = Mhasvalue?            New Intkeyframe (Getfraction (), Mvalue): New Intkeyframe (Getfraction ());       Kfclone.setinterpolator (Getinterpolator ());     return kfclone; }    }
From the above code, we can see a few points:

1, intkeyframe internal storage of the numeric mvalue is definitely the type of integers. Mvaluetype must have been int.class.

2, in the SetValue method, a mandatory type of conversion is required

3. When cloning the current object, a different constructor is selected depending on whether the value is currently stored.


Class Eighth: Keyframeset

The functions of this class are as follows:

This class holds a collection of Keyframe objects and is called by Valueanimator to calculate the value between a specified valueanimation,keyframe. Analysis class, we mainly look at the following three points of content.

1. Member Variables:

int mnumkeyframes; Keyframe Mfirstkeyframe; Keyframe Mlastkeyframe; Timeinterpolator Minterpolator; Only used in the 2-keyframe casearraylist<keyframe> mkeyframes; Only used when there is not 2 keyframestypeevaluator mevaluator;

For a general description of these member variables:

Mnumkeyframes indicates the number of keyframe stored in the current collection, and Mfirstkeyframe and Mlastkeyframe respectively indicate the first keyfame and the last Keyframe object. Minterpolator calculates the rate between keyframe and keyframe. Mkeyframes stores a collection of Keyframe objects. Mevaluator is a type of computational application.

2. Constructor function

Public Keyframeset (Keyframe ... keyframes) {        mnumkeyframes = keyframes.length;        Mkeyframes = new arraylist<keyframe> ();        Mkeyframes.addall (Arrays.aslist (keyframes));        Mfirstkeyframe = mkeyframes.get (0);        Mlastkeyframe = Mkeyframes.get (mNumKeyframes-1);        Minterpolator = Mlastkeyframe.getinterpolator ();    }

is primarily the assignment of member variables.

3, select a type of processing function to illustrate:

public static keyframeset ofint (int ... values) {        int numkeyframes = values.length;        Intkeyframe keyframes[] = new Intkeyframe[math.max (numkeyframes,2)];        if (numkeyframes = = 1) {            keyframes[0] = (intkeyframe) keyframe.ofint (0f);            KEYFRAMES[1] = (intkeyframe) keyframe.ofint (1f, values[0]);        } else {            Keyframes[0] = (intkeyframe) keyframe.ofint (0f, values[0]);            for (int i = 1; i < numkeyframes; ++i) {                keyframes[i] = (intkeyframe) keyframe.ofint ((float) I/(numKeyframes-1) , Values[i]);            }        }        return new Intkeyframeset (keyframes);    }

3, 1 Gets the length of the number of the current int type

3, 2 constructs an array of intkeyframe, the length of the array is the length of the above calculation

3, 3 calls the internal constructor of Keyframe, and constructs the Keyframe object through each value.


Class Nineth: Valueanimator

As above, a functional description is performed first.

Follow Google's documentation:

This class provides a simple time for the engine to run animations, calculate the values in the animation, and set them to the specified target.


Ok,animation analysis to the end of this, I hope you can deepen the understanding of animation.










Learn more about the animated animation in Android (based on the Android source directory structure)

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.