Android animation, androidanimation

Source: Internet
Author: User
Tags time in milliseconds

Android animation, androidanimation
Android animation Basics

Animation type

Android animation consists of four types

In XML

Alpha Gradient transparency animation effect
Scale Gradient scaling animation effect
Translate Animation Effect
Rotate Animation effects of screen transfer and Rotation



Java Code

AlphaAnimation Gradient transparency animation effect
ScaleAnimation Gradient scaling animation effect
TranslateAnimation Animation Effect
RotateAnimation Animation effects of screen transfer and Rotation


Android Animation Mode

Animation mainly has two Animation modes:

One is tweened animation (gradient animation)

In XML JavaCode
Alpha AlphaAnimation
Scale ScaleAnimation


One is frame.
By frame (screen conversion animation)

In XML JavaCode
Translate TranslateAnimation
Rotate RotateAnimation


How to define an animation in an XML file

① Open Eclipse and create an Android Project
② Create an anim folder in the res directory
③ Create a new myanim. xml file in the anim directory (note that the file name is in lower case)
④ 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>

Android animation Parsing -- XML 

<Alpha> <? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <alphaandroid: fromAlpha = "0.1" android: toAlpha = "1.0" android: duration = "3000"/> <! -- Opacity control animation effect alpha floating point value: the fromAlpha attribute specifies the transparency when the animation starts. The toAlpha attribute specifies the transparency when the animation ends: 0.0 indicates completely transparent. 1.0 indicates completely opaque. The value above is a long integer of the float data type between 0.0 and 1.0. The duration attribute 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: effectx = "50%" android: effecty = "50%" android: fillAfter = "false" android: duration = "700"/> </set> <! -- Size scaling animation effect scale attribute: interpolator specifies an animation plug-in. During my experiment, android is used. res. three types of animation inserts are found in anim resources: accelerate_decelerate_interpolator acceleration-Deceleration animation inserter accelerate_interpolator acceleration-animation inserter decelerate_interpolator deceleration-animation inserter other floating point values of specific animation effects: the fromXScale attribute is the scale size toXScale attribute on the X coordinate at the animation start. The scale size on the X coordinate at the animation end. The fromYScale attribute is the scale size toYScale attribute on the Y coordinate at the animation start. description of scaling dimensions on Y coordinate at end: the preceding four property values 0.0 indicate that the image is scaled down to no 1.0, indicating that the normal scaling-free value is less than 1.0, indicating that the shrinking value is greater than 1.0, indicating that the zoomed-in tx attribute is the animation's starting position relative to the X coordinate of the object. description of the start position of Y coordinate relative to the object: the preceding two attribute values are from 0%-100%. The value 50% indicates the long integer value of the midpoint position on the X or Y coordinate of the object. The duration Attribute indicates the animation duration: boolean value in milliseconds: When the fillAfter attribute is set to true, the animation will be applied after the animation ends -->

<Translate> <? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <translateandroid: fromXDelta = "30" android: toXDelta = "-80" android: fromYDelta = "30" android: toYDelta = "300" android: duration = "2000"/> <! -- Integer value of the translate position transfer animation effect: the fromXDelta attribute is the position on the X coordinate when the animation starts. The toXDelta attribute is the position on the X coordinate when the animation ends. The fromYDelta attribute is the position on the Y coordinate when the animation starts. The toYDelta attribute is Y at the animation end. note: if fromXType toXType fromYType toYType is not specified, the animation duration is defined by default as long integer value of the reference object: duration Attribute. 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: durtx = "50%" android: Durty = "50%" android: duration = "3000"/> <! -- Rotate rotation animation effect attribute: interpolator specifies an animation plug-in. During my experiment, android is used. res. there are three types of animation inserts in anim resources: accelerate_decelerate_interpolator acceleration-accelerator animation plug-in accelerate_interpolator acceleration-animation plug-in decelerate_interpolator deceleration-animation plug-in other floating-point value for specific animation effects: the fromDegrees attribute is the angle of the object when the animation starts. The toDegrees attribute is that the angle of the object rotation when the animation ends can be greater than 360 degrees. Description: when the angle is negative, it indicates clockwise rotation. When the angle is positive, it indicates clockwise rotation (negative from -- to positive: clockwise rotation) (negative from -- to negative: counter-clockwise rotation) (positive from -- to positive: clockwise rotation) (positive from -- to negative: clockwise rotation) the "TX" attribute is the starting position of the animation relative to the X coordinate of the object. The "ty" attribute is the starting position of the animation relative to the Y coordinate of the object: the preceding two attribute values are from 0%-100%. The value 50% indicates the long integer value of the midpoint position on the X or Y coordinate of the object. The duration Attribute indicates the animation duration: time in milliseconds --> </set>

// How to use the Animation effect public static Animation loadAnimation (Context context, int id) in XML) // The first parameter Context is the Context of the program // The second parameter id is the reference of the animated XML file // example: myAnimation = AnimationUtils. loadAnimation (this, R. anim. my_action); // use the static method loadAnimation () of the AnimationUtils class () to load the Animation XML file in XML. How can I define an Animation in Java code? // define the Animation Instance Object private Animation myAnimation_Alpha in the Code; private Animation myAnimation_Scale; private Animation myAnimation_Translate; private Animation attributes; // Initialize an instance object myAnimation_Alpha = new AlphaAnimation (0.1f, 1.0f) based on their respective constructor; myAnimation_Scale = new ScaleAnimation (0.0f, 1.4f, 0.0f, 1.4f, animation. RELATIVE_TO_SELF, 0.5f, Animation. updated, 0.5f); myAnimation_Translate = new TranslateAnimation (30366f,-80366f, 30366f, 300366f); myAnimation_Rotate = new RotateAnimation (0.0f, + 350366f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );

Android animation parsing-JavaCode


AlphaAnimation

① AlphaAnimation class object definition

  1. Private AlphaAnimation myAnimation_Alpha;
Copy code

② AlphaAnimation Class Object Construction

  1. AlphaAnimation (float fromAlpha, float toAlpha)
  2. // The first parameter fromAlpha indicates the transparency when the animation starts.
  3. // The second toAlpha parameter indicates the transparency at the animation end.
  4. MyAnimation_Alpha = new AlphaAnimation (0.1f, 1.0f );
  5. // Description:
  6. // 0.0 indicates completely transparent
  7. // 1.0 indicates completely opaque
Copy code

③ Set the animation duration

  1. MyAnimation_Alpha.setDuration (5000 );
  2. // Set the duration to 5000 milliseconds
Copy code




ScaleAnimation 

① ScaleAnimation class object definition

  1. Private AlphaAnimation myAnimation_Alpha;
Copy code

② ScaleAnimation Class Object Construction

  1. ScaleAnimation (float fromX, float toX, float fromY, float toY,
  2. Int struct txtype, float struct txvalue, int struct tytype, float struct tyvalue)
  3. // The first parameter fromX is the scaling size on the X coordinate when the animation starts.
  4. // The second toX parameter is the scaling size on the X coordinate at the animation end.
  5. // The third parameter fromY is the scaling size on the Y coordinate when the animation starts.
  6. // The fourth toY parameter is the scaling size on the Y coordinate at the animation end.
  7. /* Description:
  8. The preceding four property values
  9. 0.0 indicates shrinking to none
  10. 1.0 indicates normal scaling-free
  11. A value smaller than 1.0 indicates contraction.
  12. Value greater than 1.0 indicates Amplification
  13. */
  14. // The Fifth parameter "animation txtype" indicates the animation position type on the X axis relative to the object.
  15. // The sixth parameter, txvalue, is the starting position of the animation relative to the X coordinate of the object.
  16. // The Seventh parameter "animation txtype" is the animation position type on the Y axis relative to the object.
  17. // The eighth parameter polictyvalue is the starting position of the animation relative to the Y coordinate of the object.
  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 );
Copy code

③ Set the animation duration

  1. MyAnimation_Scale.setDuration (700 );
  2. // Set the duration to 700 milliseconds
Copy code




TranslateAnimation



① TranslateAnimation class object definition

  1. Private AlphaAnimation myAnimation_Alpha;
Copy code

② Construct a TranslateAnimation Class Object

  1. TranslateAnimation (float fromXDelta, float toXDelta,
  2. Float fromYDelta, float toYDelta)
  3. // The first parameter fromXDelta is the moving position on the X coordinate when the animation starts.
  4. // The second parameter toXDelta is the moving position on the X coordinate when the animation ends.
  5. // The third parameter fromYDelta is the moving position on the Y coordinate when the animation starts.
  6. // The fourth parameter toYDelta is the moving position of the Y coordinate at the animation end.
Copy code

③ Set the animation duration

  1. MyAnimation_Translate.setDuration (2000 );
  2. // Set the duration to 2000 milliseconds
Copy code



RotateAnimation 
① RotateAnimation class object definition

  1. Private AlphaAnimation myAnimation_Alpha;
Copy code


② RotateAnimation Class Object Construction

  1. RotateAnimation (float fromDegrees, float toDegrees,
  2. Int struct txtype, float struct txvalue, int struct tytype, float struct tyvalue)
  3. // The first parameter fromDegrees is the rotation angle at the start of the animation.
  4. // The second parameter toDegrees indicates the animation rotation angle.
  5. // The third parameter "animation txtype" indicates the animation position type on the X axis relative to the object.
  6. // The fourth argument txvalue is the starting position of the animation relative to the X coordinate of the object.
  7. // The Fifth parameter "effectxtype" indicates the animation position type on the Y axis relative to the object.
  8. // The sixth parameter polictyvalue is the starting position of the animation relative to the Y coordinate of the object.
  9. MyAnimation_Rotate = new RotateAnimation (0.0f, + 3500000f,
  10. Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f );
Copy code


③ Set the animation duration

  1. MyAnimation_Rotate.setDuration (3000 );
  2. // Set the duration to 3000 milliseconds
Copy code



How to Use the animation effect in Java code

Use the startAnimation () method inherited from the View parent class to add an animation effect for the View or subclass View.

  1. Public void startAnimation (Animation animation)

Related Article

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.