Android Common animations

Source: Internet
Author: User

I. Type of animation

Android animation consists of four types: alpha, scale, translate, rotate

In the XML configuration file

Alpha: Gradient Transparency animation effect

Scale: Gradient dimension stretch animation effect

Translate: Move animation effect on picture transition position

Rotate: Picture Transfer rotation animation effect

In Java Code code

Alphaanimation: Gradient Transparency Animation effect

Scaleanimation: Gradient Dimension stretch animation effect

Translateanimation: Move animation effect on picture transition position

Rotateanimation: Picture Transfer rotation animation effect

Second, Android animation mode

Animation There are two main modes of animation: tweened and Frame

    • One is tweened animation (gradient animation)

alphaanimation scaleanimation in alpha scale javacode in XML

    • One is frame by frame (Picture conversion animation)

XML in translate rotate javacode translateanimation rotateanimation

Iii. defining animations in XML files

① 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)

④ adding animated code to XML

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" >  <alpha/>  <scale/>  <translate/>  <rotate/></set>

  

Iv. Android XML Animation parsing

1. Alpha

<?xml version="1.0"encoding="Utf-8"?><SetXmlns:android="http://schemas.android.com/apk/res/android"><Alphaandroid:fromalpha="0.1"Android:toalpha="1.0"android:duration=" the"/> <!--transparency controls the animation effect Alpha Float value: The Fromalpha property is the Transparency Toalpha property at the beginning of the animation when the transparency description is at the end of the animation: 0. 0 indicates full transparency1. 0 means full opacity above the value of 0.0-1numeric long values for the float data type between. 0: Duration property for animation duration Description: Time in milliseconds--></Set>

2. 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:duration= "/></set><"!--size Stretch Animation effect Scale property: interpolator Specifies an animated insert during my trial, there are three types of animation inserts found when using resources in Android.res.anim: Accelerate_decelerate_interpolator Acceleration-deceleration animation insert accelerate_interpolator acceleration-Animation plug Decelerate_interpolator deceleration-Animation Insert other specific 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 dynamic                        Stretch dimensions on x-coordinate at the end of the drawing 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 Description: The above four property values0. 0 means shrink to no1. 0 indicates that a normal no-flex value of less than 1.0 means that a shrink value greater than 1.0 means that the magnification Pivotx property is animated relative to the object The starting position of the x-coordinate Pivoty property is the start position of the animation relative to the object's y-coordinate: The above two attribute values from 0%-100%value in50%a Long value for the midpoint position on the x or y-coordinate of an object: The Duration property is the animation duration description: The time is in milliseconds in the Boolean value: Fillafter property When set to True, the animation conversion is applied after the animation is finished-

3. Translate

<?xml version= "1.0" encoding= "Utf-8"? ><set xmlns:android= "Http://schemas.android.com/apk/res/android" ><Translateandroid:fromxdelta= "android:toxdelta=" -80 "android:fromydelta=" "Android:toydelta="android:duration= "$"/><!-- Translate position transfer animation effect        integer value: The Fromxdelta property is the position of 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:                     fromxtype toxtype fromytype toytype is not specified, the                     default is to use their own relative reference                     Long integer value:  The Duration property is an animation duration            Description: The   time is in milliseconds --></set>

4. 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 the insertion of an animation              In the course of my experiment, Using resources in Android.res.anim find              There are three kinds of animation inserts:                 accelerate_decelerate_interpolator    acceleration-deceleration animation Insert                 accelerate_interpolator           & nbsp;    Acceleration-animation plug-in                 decelerate_interpolator& nbsp               Slow-motion insertion              Other specific animation effects        Floating-point value:            fromdegrees property is the angle of the object at the start of the animation                 toDegrees    The object rotation angle can be greater than 360 degrees at the end of the animation                 Description:                       When angle is negative--counterclockwise rotation                       When angle is positive--indicates clockwise rotation                            &nbsP         (negative from--to positive: clockwise rotation)                           (negative from--to negative: rotate counterclockwise)                         (positive from--to positive: clockwise rotation)                         (positive from--to negative: counterclockwise)                     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              Description:         above two attribute values are taken from 0%-100%                           50% is the midpoint position of the object in X or y-direction coordinates          Long value: & nbsp           duration   property is animation duration              Description:       time in milliseconds--></set >

  XML use animation effects in

 Public Static int  /// The first parameter context for the program    contexts // second parameter ID is a reference to the animated XML file / / Example: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

v. Java defining animations in code

//defining an animation instance object in codePrivateAnimation Myanimation_alpha;PrivateAnimation Myanimation_scale;PrivateAnimation myanimation_translate;PrivateAnimation myanimation_rotate; //Initializes an instance object according to its own construction methodMyanimation_alpha =NewAlphaanimation (0.1f, 1.0f); Myanimation_scale=NewScaleanimation (0.0f, 1.4f, 0.0f, 1.4f, Animation.relative_to_self,0.5f, Animation.relative_to_self, 0.5f); Myanimation_translate=NewTranslateanimation (30.0f, -80.0f, 30.0f, 300.0f); Myanimation_rotate=NewRotateanimation (0.0f, +350.0f, Animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);

Six, Android code animation analysis

1. Alphaanimation

Alphaanimation Class object definitions

   Private Alphaanimation Myanimation_alpha;

Alphaanimation Class object constructs

Alphaanimation (floatfloat/// first parameter Fromalpha for animation start time Transparency // the second parameter, Toalpha, is the transparency at the end of an animation New Alphaanimation (0.1f, 1.0f); //  //                0.0 indicates full transparency //                1.0 for completely opaque

Set the duration of an animation

Myanimation_alpha.setduration (the); // set time duration to 5000 milliseconds

2. Scaleanimation
Scaleanimation Class object definitions

Private Scaleanimation Myanimation_scale;

Scaleanimation Class object constructs

Scaleanimation (floatFromX,floatToX,floatFromY,floatToY,intPivotxtype,floatPivotxvalue,intPivotytype,floatpivotyvalue)//The first parameter FROMX is the scaling dimension on the x-coordinate at the start of the animation//the second parameter tox the scaling dimension at the end of the animation at the x-coordinate//the third parameter, fromy, is the scaling dimension on the y-coordinate at the start of the animation//The fourth parameter toy is the scaling dimension on the y-coordinate at the end of the animation/*Description: The above four kinds of attribute value 0.0 means that shrinking to no 1.0 means normal no scaling A value of less than 1.0 means that a shrinking value greater than 1.0 indicates magnification*///The fifth parameter Pivotxtype for the animation on the x-axis relative to the object position type//The sixth parameter pivotxvalue the start position of the animation relative to the x-coordinate of the object//The 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-coordinateMyanimation_scale =NewScaleanimation (0.0f, 1.4f, 0.0f, 1.4f, Animation.relative_to_self,0.5f, Animation.relative_to_self, 0.5f);

Set the duration of an animation

Myanimation_scale.setduration (a); // set time duration to 700 milliseconds

3. Translateanimation

Ranslateanimation Class object definitions

Private Translateanimation myanimation_translate;

Translateanimation Class object constructs

Translateanimation (floatfloat  toxdelta,                       float  float // The first argument fromxdelta the position    at the start of the animation when the x-coordinate is moved // The second parameter toxdelta is the      position of movement at the end of the animation at the X coordinate // the third parameter, Fromydelta, is the moving     position on the y-coordinate at the start of the animation // the fourth parameter, Toydelta, is the moving position on the y-coordinate at the end of the animation

Set the duration of an animation

New translateanimation (10f, 100f, 10f, 100f); Myanimation_translate.setduration (+) ; // set time duration to 2000 milliseconds

4. Rotateanimation
Rotateanimation Class object definitions

Private Rotateanimation myanimation_rotate;

Rotateanimation Class object constructs

Rotateanimation (floatFromdegrees,floatTodegrees,intPivotxtype,floatPivotxvalue,intPivotytype,floatpivotyvalue)//The first parameter fromdegrees is the angle of rotation at the beginning of the animation//the second parameter todegrees the angle to which the animation is rotated//The third parameter Pivotxtype for the animation on the x-axis relative to the object position type//The fourth parameter pivotxvalue the start position of the animation relative to the x-coordinate of the object//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-coordinateMyanimation_rotate =NewRotateanimation (0.0f, +350.0f, Animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);
Set the duration of an animation
Myanimation_rotate.setduration (+); // set time duration to 3000 milliseconds
How to use animation effects in Java code to add an animation effect to a view or subclass view, using the method Startanimation () inherited from the View parent class  Public void startanimation (Animation Animation) view.startanimation (Myanimation_alpha); View.startanimation (Myanimation_ scale); view.startanimation (myanimation_translate); View.startanimation (myanimation_rotate) ;

Android Common animations

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.