Properties Animation of Android notes

Source: Internet
Author: User

Preface, Animation classification

as shown, Android The animation is divided into three main types:


Let's start with the property animation .

The so-called attribute animation- refers to the object's property values have changed, such as control position and transparency.

For example , now you want to implement a button to move down first, and then move the animation to the right.

( 1 ) compose an animation XML

because the newAndroidEngineering, inResThe following does not specifically place animationsXMLfolder, so we create a newAnimatorname of the folder. It is advisable not to start another name becauseADTtheResthe folder name has a search function, such asAnimatorThe name of the time,ADTyou can identify this as an animation by name .XMLfolder, when you create a newXML, the corresponding root element is selected.

As shown in the following:


The code for the animated XML is as follows--

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android"    android:ordering= "sequentially" >    <objectanimator         android:duration= "$"        android: Propertyname= "y"        android:valueto= "x"        android:valuetype= "Inttype" ></objectAnimator>    < Objectanimator         android:duration= "a"        android:propertyname= "a"        android:valueto= "        android" : valuetype= "Inttype" ></objectAnimator></set>


on animation XML is described below:   

  • android:ordering describes the sequence of execution of a series of animation actions, with two choices, sequentially and together, executed sequentially or together;
  • Objectanimatoris the object that sets the animation implementation,Durationis the time that the animation action takes from start to finish, and the property isy(Back inActivityThis is a property of your own definition, used to refer to theButtonIn the screen of theyThe coordinate value. ), the property value is -, the type is an integral type.

( 2 ) Activity Code

public class Propertyactivity extends Activity {public final static String TAG = "propertyactivity"; @Overrideprotected voi D onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Final Button Movebutton = (Button) Findviewbyid (R.ID.MOVE_BTN); final move move = new Move (Movebutton); Movebutton.setonclicklistener (New Onclicklistener () {@Overridepublic void OnClick (View v) {//Mount Property Animation Resource Animatorset set = (Animatorset) Animatorinflater.loadanimator (Propertyactivity.this,r.animator.property_anim);//Set the object to be controlled set.settarget (move);// Start animation Set.start (); LOG.D (TAG, "getwidth:" +movebutton.getwidth ()); LOG.D (TAG, "Top:" +movebutton.gettop ()); LOG.D (TAG, "Getmeasuredwidth:" +movebutton.getmeasuredwidth ()); LOG.D (TAG, "Getbottom:" +movebutton.getbottom ());});}  Private class move{private int A; private int y; private view view; public Move (view view) {This.view = view;} public int GetY () {return y;} public void sety (int y) {this.y = Y;view.layout (view.geTLeft (), Y, View.getright (), Y + view.getmeasuredheight ());} public int Geta () {return A;} public void SetA (int a) {this.a = A; LOG.D (TAG, "End_getwidth:" +view.getwidth ()); LOG.D (TAG, "End_top:" +view.gettop ()); LOG.D (TAG, "End_getmeasuredwidth:" +view.getmeasuredwidth ()); LOG.D (TAG, "End_getbottom:" +view.getbottom ()); View.layout (A, View.gettop (), A + view.getmeasuredwidth (), View.getbottom ());} }}

we are in the program Logcat Print out the Button the specific value of the location.

LOG.D (TAG, "getwidth:" +movebutton.getwidth ());

LOG.D (TAG, "Top:" +movebutton.gettop ());

LOG.D (TAG, "Getmeasuredwidth:" +movebutton.getmeasuredwidth ());

LOG.D (TAG, "Getbottom:" +movebutton.getbottom ());

The results are as follows:


The upper and lower heights are indeed from 0 Change to , implements the move.

the properties in the above codex, yAre the variable values that you arbitrarily take,Setmethod, you set the specificViewthe height and width, so the variable name is not important, as long asXMLand hereJavaThe code matches the line. Get() method is not required, and theSetmethod is necessary because   

Animatorset in the animation settings class, you need to invoke the object's property setting method and animate it to change the corresponding property.


The above is just a simple demonstration of the property animation, actually this is from Android 3.0 only then come out, 3.0 before there is a very famous animated open-source project, called Nine Old Androids, the project developed a lot of property animation, as shown, sorry, Do not make GIF dynamic graphs. You can directly behind the post link, there are apk and source code. Worth a read.



Open source project nine old androids data




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.