Android Animation detailed

Source: Internet
Author: User
Tags set time

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)

in XML
Javacode
Alpha Alphaanimation
Scale Scaleanimation

    • One is frame by frame (Picture conversion animation)
in XML
Javacode
Translate Translateanimation
Rotate 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

    1. <?xml version= "1.0" encoding= "Utf-8"?>
    2. <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
    3. <alpha/>
    4. <scale/>
    5. <translate/>
    6. <rotate/>
    7. </set>

Iv. Android XML Animation parsing

1. Alpha

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
  3. <alpha
  4. Android:fromalpha= "0.1"
  5. Android:toalpha= "1.0"
  6. Android:duration= "3000"
  7. />
  8. <!--transparency Controls animation effect Alpha
  9. Floating-point values:
  10. The Fromalpha property is the transparency at the beginning of the animation
  11. Toalpha property is the transparency at the end of the animation
  12. Description
  13. 0.0 indicates full transparency
  14. 1.0 means completely opaque
  15. The above values take a number of float data types between 0.0-1.0
  16. Long Integer value:
  17. The Duration property is the duration of the animation
  18. Description
  19. The time is measured in milliseconds
  20. -
  21. </set>
2. Scale
  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
  3. <scale
  4. Android:interpolator=
  5. "@android: Anim/accelerate_decelerate_interpolator"
  6. android:fromxscale= "0.0"
  7. android:toxscale= "1.4"
  8. android:fromyscale= "0.0"
  9. android:toyscale= "1.4"
  10. android:pivotx= "50%"
  11. Android:pivoty= "50%"
  12. Android:fillafter= "false"
  13. Android:duration= "/>"
  14. </set>
  15. <!--size Scaling animation effect scale
  16. Property: interpolator Specifies the insertion of an animation
  17. In my experiment, using the resources in Android.res.anim, I found
  18. There are three types of animation inserts:
  19. Accelerate_decelerate_interpolator acceleration-deceleration animation insertion device
  20. Accelerate_interpolator Acceleration-Animation insertion Device
  21. Decelerate_interpolator deceleration-Animation insertion device
  22. Other animations that belong to a specific animation effect
  23. Floating-point values:
  24. The Fromxscale property is the scaling dimension on the x-coordinate at the start of the animation
  25. The Toxscale property is the scaling dimension on the x-coordinate at the end of the animation
  26. The Fromyscale property is the scaling dimension on the y-coordinate at the start of the animation
  27. The Toyscale property is the scaling dimension on the y-coordinate at the end of the animation
  28. Description
  29. Above four attribute values
  30. 0.0 means shrink to No
  31. 1.0 indicates normal no scaling
  32. Values less than 1.0 indicate shrinkage
  33. Values greater than 1.0 indicate magnification
  34. The Pivotx property is the start position of the animation relative to the X coordinate of the object
  35. The Pivoty property is the start position of the animation relative to the y-coordinate of the object
  36. Description
  37. The above two attribute values are taken from the 0%-100% value
  38. 50% is the midpoint position on the x or Y coordinate of the object
  39. Long Integer value:
  40. The Duration property is the duration of the animation
  41. Description: The time is measured in milliseconds
  42. Boolean-Value:
  43. Fillafter property When set to True, the animation conversion is applied after the animation is finished
  44. -
3. Translate
  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
  3. <translate
  4. Android:fromxdelta= "30"
  5. Android:toxdelta= "-80"
  6. Android:fromydelta= "30"
  7. Android:toydelta= "300"
  8. Android:duration= "2000"
  9. />
  10. <!--translate position transfer animation effect
  11. Integer value:
  12. The Fromxdelta property is the position of the x-coordinate at the start of the animation
  13. The Toxdelta property is the position of the x-coordinate at the end of the animation
  14. The Fromydelta property is the position of the y-coordinate at the start of the animation
  15. The Toydelta property is the position of the y-coordinate at the end of the animation
  16. Attention:
  17. Not specified Fromxtype toxtype fromytype toytype time,
  18. The default is to use your own relative reference
  19. Long Integer value:
  20. The Duration property is the duration of the animation
  21. Description: The time is measured in milliseconds
  22. -
  23. </set>

4. Rotate

  1. <?xml version= "1.0" encoding= "Utf-8"?>
  2. <set xmlns:android= "Http://schemas.android.com/apk/res/android" >
  3. <rotate
  4. Android:interpolator= "@android: Anim/accelerate_decelerate_interpolator"
  5. android:fromdegrees= "0"
  6. Android:todegrees= "+350"
  7. android:pivotx= "50%"
  8. Android:pivoty= "50%"
  9. Android:duration= "/>"
  10. <!--rotate rotation animation effect
  11. Property: interpolator Specifies the insertion of an animation
  12. In my experiment, using the resources in Android.res.anim, I found
  13. There are three types of animation inserts:
  14. Accelerate_decelerate_interpolator acceleration-deceleration animation insertion device
  15. Accelerate_interpolator Acceleration-Animation insertion Device
  16. Decelerate_interpolator deceleration-Animation insertion device
  17. Other animations that belong to a specific animation effect
  18. Floating-point value:
  19. Fromdegrees property is the angle of the object at the beginning of the animation
  20. Todegrees property rotates the angle of the object at the end of the animation to be greater than 360 degrees
  21. Description
  22. When the angle is negative--indicates counterclockwise rotation
  23. When the angle is positive--Indicates clockwise rotation
  24. (Negative from--to positive: clockwise rotation)
  25. (Negative from--to negative: counterclockwise rotation)
  26. (Positive from--to positive: clockwise rotation)
  27. (Positive from--to negative: counterclockwise rotation)
  28. The Pivotx property is the start position of the animation relative to the X coordinate of the object
  29. The Pivoty property is the start position of the animation relative to the y-coordinate of the object
  30. Note: The above two attribute values are taken from the 0%-100% value
  31. 50% is the midpoint position on the x or Y coordinate of the object
  32. Long Integer value:
  33. The Duration property is the duration of the animation
  34. Description: The time is measured in milliseconds
  35. -
  36. </set>


XML use animation effects in

    1. public static Animation Loadanimation (context context, int id)
    2. The first parameter is context for the program
    3. The second parameter ID is a reference to the animated XML file
    4. Example:
    5. Myanimation= animationutils.loadanimation (this, r.anim.my_action);
    6. Use the static method of the Animationutils class Loadanimation () to load an animated XML file in the XML



v. Java defining animations in code

    1. Defining an animation instance object in code
    2. Private Animation Myanimation_alpha;
    3. Private Animation Myanimation_scale;
    4. Private Animation myanimation_translate;
    5. Private Animation myanimation_rotate;
    6. Initializes an instance object according to its own construction method
    7. Myanimation_alpha = new Alphaanimation (0.1f, 1.0f);
    8. Myanimation_scale = new Scaleanimation (0.0f, 1.4f, 0.0f, 1.4f,
    9. Animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);
    10. Myanimation_translate = new Translateanimation (30.0f, -80.0f, 30.0f, 300.0f);
    11. Myanimation_rotate = new Rotateanimation (0.0f, +350.0f,
    12. Animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);

Six, Android code animation analysis

1. Alphaanimation

Alphaanimation Class object definitions

    1. 1. Private Alphaanimation Myanimation_alpha;

Alphaanimation Class object constructs

    1. Alphaanimation (float fromalpha, float toalpha)
    2. The first parameter fromalpha is the transparency at the beginning of the animation
    3. The second parameter, Toalpha, is the transparency at the end of an animation
    4. Myanimation_alpha = new Alphaanimation (0.1f, 1.0f);
    5. Description
    6. 0.0 indicates full transparency
    7. 1.0 means completely opaque

Set the duration of an animation

    1. Myanimation_alpha.setduration (5000);
    2. Set time duration to 5000 milliseconds


2. Scaleanimation
Scaleanimation Class object definitions

    1. Private Scaleanimation Myanimation_scale;

Scaleanimation Class object constructs

  1. Scaleanimation (float FromX, float toX, float fromY, float ToY,
  2. int Pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue)
  3. The first parameter fromx is the scaling dimension on the x-coordinate at the start of the animation
  4. The second parameter tox the scaling dimension at the end of the animation at the x-coordinate
  5. The third parameter, fromy, is the scaling dimension on the y-coordinate at the start of the animation
  6. The fourth parameter toy is the scaling dimension on the y-coordinate at the end of the animation
  7. /* Description:
  8. Above four attribute values
  9. 0.0 means shrink to No
  10. 1.0 indicates normal no scaling
  11. Values less than 1.0 indicate shrinkage
  12. Values greater than 1.0 indicate magnification
  13. */
  14. The fifth parameter Pivotxtype for the animation on the x-axis relative to the object position type
  15. The sixth parameter pivotxvalue the start position of the animation relative to the x-coordinate of the object
  16. The seventh parameter Pivotxtype for the animation on the y-axis relative to the object position type
  17. The eighth parameter pivotyvalue the start position of the animation relative to the object's y-coordinate
  18. Myanimation_scale = new Scaleanimation (0.0f, 1.4f, 0.0f, 1.4f,
  19. Animation.relative_to_self, 0.5f, Animation.relative_to_self, 0.5f);

Set the duration of an animation

    1. Myanimation_scale.setduration (700);
    2. Set time duration to 700 milliseconds

3. Translateanimation

Ranslateanimation Class object definitions

    1. Private Translateanimation myanimation_translate;

Translateanimation Class object constructs

    1. Translateanimation (float Fromxdelta, float toxdelta,
    2. Float Fromydelta, float toydelta)
    3. The first argument fromxdelta the position at the start of the animation when the x-coordinate is moved
    4. The second parameter toxdelta is the position of movement at the end of the animation at the X coordinate
    5. The third parameter, Fromydelta, is the moving position on the y-coordinate at the start of the animation
    6. The fourth parameter, Toydelta, is the moving position on the y-coordinate at the end of the animation

Set the duration of an animation

    1. Myanimation_translate = new Translateanimation (10f, 100f, 10f, 100f);
    2. Myanimation_translate.setduration (2000);
    3. Set time duration to 2000 milliseconds


4. Rotateanimation
Rotateanimation Class object definitions

    1. Private Rotateanimation myanimation_rotate;

Rotateanimation Class object constructs

    1. Rotateanimation (float fromdegrees, float todegrees,
    2. int Pivotxtype, float pivotxvalue, int pivotytype, float pivotyvalue)
    3. The first parameter fromdegrees is the angle of rotation at the beginning of the animation
    4. The second parameter todegrees the angle to which the animation is rotated
    5. The third parameter Pivotxtype for the animation on the x-axis relative to the object position type
    6. The fourth parameter pivotxvalue the start position of the animation relative to the x-coordinate of the object
    7. The fifth parameter Pivotxtype for the animation on the y-axis relative to the object position type
    8. The sixth parameter pivotyvalue the start position of the animation relative to the object's y-coordinate
    9. Myanimation_rotate = new Rotateanimation (0.0f, +350.0f,
    10. Animation.relative_to_self,0.5f,animation.relative_to_self, 0.5f);

Set the duration of an animation

    1. Myanimation_rotate.setduration (3000);
    2. Set time duration to 3000 milliseconds


how Java using animation effects in your code
Use the method inherited from the View parent class Startanimation () to add an animation effect for view or subclass view, and so on.

    1. public void Startanimation (Animation Animation)
    2. View.startanimation (Myanimation_alpha);
    3. View.startanimation (Myanimation_scale);
    4. View.startanimation (myanimation_translate);
    5. View.startanimation (myanimation_rotate);

Reference: http://blog.csdn.net/ithomer/article/details/7523328

Android Animation detailed

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.