Android animation effect Programming Basics--android Animation

Source: Internet
Author: User

Animation effect Programming Basics--android Animation

Type of animation

Android animation is made up of four types of XML

Alpha Gradient Transparency Animation effect

Scale gradient dimension stretch animation effect

Translate picture conversion position move animation effect

Rotate picture Rotation animation effect

In Javacode

Alphaanimation Gradient Transparency Animation effect

Scaleanimation Gradient Dimension Stretch animation effect

Translateanimation picture Conversion position Move animation effect

Rotateanimation Picture Rotation animation effect

There are two main modes of animation for Android animation mode animation:

One is the tweened animation (gradient animation) XML Javacode Alpha Alphaanimation scale scaleanimation

One is frame by frame (Image conversion animation) XML in Javacode translate translateanimation rotate rotateanimation

How to define animations in an XML file

① open Eclipse, new Android Project

② creating a new Anim folder in the Res directory

③ Create a new myanim.xml in the Anim directory (note the file name lowercase)

④ Add XML Animation code <?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "http://schemas.android.com/apk/res/ Android > <alpha/> <scale/> <translate/> <rotate/> </set> Copy Code

Android Animation parsing--xml

<alpha> <?xml version= "1.0" encoding= "Utf-8"?>

<set xmlns:android= "Http://schemas.android.com/apk/res/android" >

<alpha

Android:fromalpha= "0.1"

Android:toalpha= "1.0"

Android:duration= "/>"

<!--transparency controls the animation effect Alpha Float value: The Fromalpha property is the transparency at the beginning of the animation Toalpha property is the transparency description at the end of the animation: 0.0 means full transparency 1.0 means full opacity above the value of the float data type between 0.0-1.0 Digital

Long Integer value: Duration property is the animation duration description: Time in milliseconds--

</set>

<scale> <?xml version= "1.0" encoding= "Utf-8"?> <set xmlns:android= "http://schemas.android.com/apk/ Res/android ">

<scale

Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"

android:fromxscale= "0.0" android:toxscale= "1.4"

android:fromyscale= "0.0" android:toyscale= "1.4"

android:pivotx= "50%" android:pivoty= "50%"

Android:fillafter= "false" android:startoffset= "700"

Android:duration= "/>"

</set>

<!--scaling Animation effect Scale property: interpolator Specifies an animated insert in my experiment, using resources in Android.res.anim, there are three types of animation inserts found: Accelerate_decelerate_ Interpolator acceleration-deceleration animation Insert accelerate_interpolator acceleration-Animation insertion Decelerate_interpolator deceleration-animation insert other types of animation effects floating-point values:

The Fromxscale property is the scaling dimension on the x-coordinate at the start of the animation Toxscale property is the scaling dimension on the x-coordinate at the end of the animation

The Fromyscale property is the scaling dimension on the y-coordinate at the start of the animation Toyscale property is the scaling dimension on the y-coordinate at the end of the animation Startoffset property to start the next animation from the time the last animation was stopped

Description: The above four property values

0.0 means that shrinking to no 1.0 means that a normal no-flex value of less than 1.0 means that the shrink value is greater than 1.0 to enlarge

The Pivotx property is the start position of the animation relative to the object's x-coordinate Pivoty property is the start position of the animation relative to the object's y-coordinate

Note: The above two attribute values from the 0%-100% value 50% is the position of the midpoint on the X or y-direction coordinates of the object

Long Integer value: Duration property for animation duration Description: Time in milliseconds

Boolean value: Fillafter property When set to True, the animation conversion is applied after the end of the animation-

<translate>

<?xml version= "1.0" encoding= "Utf-8"?>

<set xmlns:android= "Http://schemas.android.com/apk/res/android" >

<translate

Android:fromxdelta= "30"

Android:toxdelta= "-80"

Android:fromydelta= "30"

Android:toydelta= "300"

Android:duration= "/>"

<!--translate position transfer animation effect integer value: Fromxdelta property is the position on the x-coordinate at the start of the animation Toxdelta property is the position on the x-coordinate at the end of the animation Fromydelta property is the position on the y-coordinate when the animation starts Toydelta property is the position of the y-coordinate at the end of the animation note: When Fromxtype toxtype fromytype Toytype is not specified, the default is to use itself as a relative reference long Integer value: Duration property for animation duration Description: Time in milliseconds--> ;

</set>

<rotate>

<?xml version= "1.0" encoding= "Utf-8"?>

<set xmlns:android= "Http://schemas.android.com/apk/res/android" >

<rotate

Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"

android:fromdegrees= "0"

Android:todegrees= "+350"

android:pivotx= "50%"

Android:pivoty= "50%"

Android:duration= "/>"

<!--rotate rotation animation effect properties: interpolator Specifies an animated insert in the course of my experiment, using resources in Android.res.anim, I found three kinds of animation inserts: Accelerate_decelerate_ Interpolator acceleration-deceleration animation Insert accelerate_interpolator acceleration-Animation insertion Decelerate_interpolator deceleration-animation insert other specific animation effects

Floating-point value: The Fromdegrees property is the angle of the object at the start of the animation Todegrees property is at the end of the animation when the object rotates at an angle greater than 360 degrees

Description: When the angle is negative--indicates counterclockwise rotation when the angle is positive--indicates clockwise rotation (negative from--to positive: clockwise rotation) (negative from--to negative: counterclockwise) (positive from--to positive: clockwise rotation) (positive from--to negative number: Counterclockwise rotation)

The Pivotx property is the start position of the animation relative to the object's x-coordinate Pivoty property is the start position of the animation relative to the object's y-coordinate

Note: The above two attribute values from the 0%-100% value 50% is the position of the midpoint on the X or y-direction coordinates of the object

Long Integer value: Duration property is the animation duration description: Time in milliseconds--

</set>

How to use the animation effect in XML public static Animation Loadanimation (context context, int id) ///The first parameter context for program contexts// The second parameter ID is a reference//example of an animated XML file: myanimation= animationutils.loadanimation (this,r.anim.my_action); Use the static method of the Animationutils class Loadanimation () to load an animated XML file in the XML

How to define animations in Java code//Define animation instance objects in code

Private Animation Myanimation_alpha;

Private Animation Myanimation_scale;

Private Animation myanimation_translate;

Private Animation myanimation_rotate;

Initializes an instance object Myanimation_alpha=new alphaanimation (0.1f, 1.0f) according to the respective construction method;

Myanimation_scale =new scaleanimation (0.0f, 1.4f, 0.0f, 1.4f, Animation.relative_to_self, 0.5f, animation.relative_to_ Self, 0.5f);

Myanimation_translate=new translateanimation (30.0f, -80.0f, 30.0f, 300.0f);

Myanimation_rotate=new rotateanimation (0.0f, +350.0f, Animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);

Android Animation parsing--javacode

Alphaanimation

The ①alphaanimation class object defines the private alphaanimation myanimation_alpha;

②alphaanimation Class object Construction alphaanimation (float fromalpha, float toalpha) //First parameter Fromalpha for animation start time transparency//second parameter toalpha to Animation at the end of the transparency Myanimation_alpha=new alphaanimation (0.1f, 1.0f); Description://0.0 indicates full transparency//1.0 for fully opaque copy code

③ Set Animation duration myanimation_alpha.setduration (5000); //Set time duration is 5000 milliseconds

Scaleanimation

The ①scaleanimation class object defines the private alphaanimation myanimation_alpha;

②scaleanimation class object constructs scaleanimation (float FromX, float toX, float fromY, float toY, int pivotxtype, float pivotxvalue, in T Pivotytype, float Pivotyvalue) //The first parameter fromx the scaling dimension on the x-coordinate at the start of the animation//The second parameter tox the scaling dimension on the x-coordinate at the end of the animation// The third parameter, fromy, is the scaling dimension on the y-coordinate at the start of the animation//fourth parameter toy the scaling dimension on the y-coordinate at the end of the animation/* Description: The above four attribute values 0.0 means that a contraction to no 1.0 means that the normal no scaling value is less than 1.0 means that the shrinkage value is greater than 1.0. The fifth parameter Pivotxtype for animation at the x-axis relative to the object position type//sixth parameter pivotxvalue the start position of the x-coordinate of the animation relative to the object//seventh parameter Pivotxtype for the animation on the y-axis relative to the object position type// The eighth parameter pivotyvalue the start position of the animation relative to the object's y-coordinate

Myanimation_scale =new scaleanimation (0.0f, 1.4f, 0.0f, 1.4f, Animation.relative_to_self, 0.5f, animation.relative_to_ Self, 0.5f);

③ Set Animation duration myanimation_scale.setduration (700); //Set time duration is 700 milliseconds

Translateanimation

The ①translateanimation class object defines the private alphaanimation myanimation_alpha;

②translateanimation Class object Construction translateanimation (float fromxdelta, float toxdelta, float fromydelta, float toydelta) // The first parameter fromxdelta the move position at the start of the animation at the x-coordinate//The second parameter toxdelta the position at the end of the animation at the x-coordinate.//The third parameter Fromydelta the movement position on the y-coordinate when the animation starts// The fourth parameter, Toydelta, is the moving position on the y-coordinate at the end of the animation

③ Set Animation duration myanimation_translate.setduration (2000); //Set time duration is 2000 milliseconds

Rotateanimation

The ①rotateanimation class object defines the private alphaanimation myanimation_alpha;

②rotateanimation Class object Construction rotateanimation (float fromdegrees, float todegrees, int pivotxtype, float pivotxvalue, int pivotyt ype, float Pivotyvalue) ///The first parameter fromdegrees the angle of rotation at the start of the animation///The second parameter todegrees the point at which the animation is rotated The third parameter Pivotxtype for the animation on the x-axis relative to the object position type//fourth parameter pivotxvalue the start position of the animation relative to the object's x-coordinate, or the fifth parameter Pivotxtype for the animation on the y-axis relative to the object position type// The sixth parameter pivotyvalue the start position of the animation relative to the object's y-coordinate

Myanimation_rotate=new rotateanimation (0.0f, +350.0f, Animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);

③ Set Animation duration myanimation_rotate.setduration (3000); //Set time duration is 3000 milliseconds

How to use the animation effect in Java code

Use the method inherited from the View parent class Startanimation () to add an animation effect to view or subclass view, and so on.

Android animation effect Programming Basics--android Animation

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.