Four types of animations for Android

Source: Internet
Author: User
Tags time in milliseconds

Animation type

Android animation consists of four types

In XML
Alpha gradient transparency animation effect
Scale scaling Animation
Animated effect of moving the translate position
Rotate screen transfer rotation animation effect

Java code
Alphaanimation gradient transparency animation effect
Scaleanimation gradient scaling animation effect
Translateanimation
Rotateanimation image transfer rotation animation effect

Android Animation Mode

Animation mainly has two animation modes:

One is tweened animation (gradient animation)
Java code in XML
Alpha alphaanimation
Scale scaleanimation

One is frame by frame (screen conversion animation)
Java code in XML
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

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>

Copy code

Android animation parsing-XML

<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 the animation effect alpha
9. floating point value:
10. The fromalpha attribute is the transparency when the animation starts.
11. The toalpha attribute is the transparency when the animation ends.
12. Note:
13. 0.0 indicates completely transparent
14. 1.0 indicates completely opaque
15. The above value is a float data type number between 0.0 and 1.0.
16.
17. long integer value:
18. The duration attribute is the animation duration.
19. Note:
20. Time in milliseconds
21. -->
22. </set>

Copy code

<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: Export Tx = "50%"
11. Android: Ty = "50%"
12. Android: fillafter = "false"
13. Android: Duration = "700"/>
14. </set>
15. <! -- Scale up and down the animation effect
16. attribute: interpolator specifies an animation plug-in.
17. During my experiment, when I used resources in Android. res. anim, I found that
18. There are three types of animation inserts:
19. accelerate_decelerate_interpolator acceleration-Deceleration animation inserter
20. accelerate_interpolator acceleration-animation insertor
21. decelerate_interpolator deceleration-animation insertor
22. Other animation Effects
23. floating point value:
24.
25. The fromxscale attribute is the scaling size on the X coordinate when the animation starts.
26. The toxscale attribute is the scaling size on the X coordinate at the animation end.
27.
28. The fromyscale attribute is the scaling size on the Y coordinate when the animation starts.
29. The toyscale attribute is the scaling size on the Y coordinate when the animation ends.
30.
31. Note:
32. The preceding four property values
33.
34. 0.0 indicates shrinking to no
35. 1.0 indicates normal scaling-free
36. A value smaller than 1.0 indicates shrinking
37. A value greater than 1.0 indicates Amplification
38.
39. The ready TX attribute is the starting position of the animation relative to the X coordinate of the object.
40. The Ty attribute is the starting position of the animation relative to the Y coordinate of the object.
41.
42. Note:
43. The preceding two attribute values are from 0% to 100%.
44. 50% indicates the midpoint position on the X or Y coordinate of the object.
45.
46. long integer value:
47. The duration attribute is the animation duration.
48. Description: The time is in milliseconds.
49.

50. boolean value:
51. When the fillafter attribute is set to true, the animation is applied after the animation ends.
52. -->

Copy code

<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 attribute is the position on the X coordinate when the animation starts.
13. The toxdelta attribute is the position on the X coordinate when the animation ends.
14. The fromydelta attribute is the position on the Y coordinate when the animation starts.
15. The toydelta attribute is the position on the Y coordinate when the animation ends.
16. Note:
17. When fromxtype toxtype fromytype toytype is not specified,
18. By default, you are taken as a reference object.
19. long integer value:
20. The duration attribute is the animation duration.
21. Note: The time is in milliseconds.
22. -->
23. </set>

Copy code

<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: Export Tx = "50%"
8. Android: Ty = "50%"
9. Android: Duration = "3000"/>
10. <! -- Rotate rotation animation effect
11. attribute: interpolator specifies an animation plug-in.
12. During my experiment, when I used resources in Android. res. anim, I found that
13. There are three types of animation inserts:
14. accelerate_decelerate_interpolator acceleration-Deceleration animation inserter
15. accelerate_interpolator acceleration-animation insertor
16. decelerate_interpolator deceleration-animation insertor
17. Other animation Effects
18.
19. floating point value:
20. The fromdegrees attribute is the object angle when the animation starts.
21. The todegrees attribute indicates that the rotation angle of the object can be greater than 360 degrees when the animation ends.
22.

23.
24. Note:
25. When the angle is negative, it indicates clockwise rotation.
26. When the angle is positive, it indicates clockwise rotation.
27. (negative from -- to positive: clockwise rotation)
28. (negative from -- to negative: counter-clockwise rotation)
29. (positive from -- to positive: clockwise rotation)
30. (positive number from -- to negative number: clockwise rotation)
31.

32. The ready TX attribute is the starting position of the animation relative to the X coordinate of the object.
33. The Ty attribute is the starting position of the animation relative to the Y coordinate of the object.
34.
35. Note: The preceding two attribute values are from 0% to 100%.
36. 50% indicates the midpoint position on the X or Y coordinate of the object.
37.

38. long integer value:
39. The duration attribute is the animation duration.
40. Description: The time is in milliseconds.
41. -->
42. </set>

Copy code

How to Use the animation effect in XML

1. Public static animation loadanimation (context, int ID)
2. // The first parameter context is the context of the program
3. // The second parameter ID is the reference of the animated XML file.
4. // example:
5. myanimation = animationutils. loadanimation (this, R. anim. my_action );
6. // use the static method loadanimation () of the animationutils class to load the animation XML file in XML.

Copy code

How to define animations in Java code

1. // define the animation instance object in the code
2. Private animation myanimation_alpha;
3. Private animation myanimation_scale;
4. Private animation myanimation_translate;
5. Private animation myanimation_rotate;
6.
7. // Initialize an instance object based on its own Constructor
8. myanimation_alpha = new alphaanimation (0.1f, 1.0f );
9.

10. myanimation_scale = new scaleanimation (0.0f, 1.4f, 0.0f, 1.4f,
11. animation. relative_to_self, 0.5f, animation. relative_to_self, 0.5f );
12.

13. myanimation_translate = new translateanimation (30366f,-80366f, 30366f, 300366f );
14.

15. myanimation_rotate = new rotateanimation (0.0f, + 3500000f,
16. animation. relative_to_self, 0.5f, animation. relative_to_self, 0.5f );

Copy code

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 parameter toalpha 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 Ms

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 comment txtype, float comment txvalue, int comment tytype, float comment 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 parameter toy 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 no
10. 1.0 indicates normal scaling-free
11. A value smaller than 1.0 indicates contraction.
12. A value greater than 1.0 indicates Amplification
13 .*/
14. // The Fifth parameter "effectxtype" indicates the type of the animation position 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, "effectxtype", indicates the animation position on the Y axis relative to the object type.
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 ms

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 on the Y coordinate when the animation ends.

Copy code
③ Set the animation duration

1. myanimation_translate.setduration (2000 );
2. // set the duration to 2000 ms

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 comment txtype, float comment txvalue, int comment tytype, float comment 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 "effectxtype" indicates the type of the animation position on the X axis relative to the object.
6. // The fourth parameter, 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 Ms

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)

Copy code

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.