Android simple animation animtion and androidanimtion
Android's simple animation effects include the Animtion class. 1. Implement the animation effect in XML; 2. Implement the animation effect in java code.
Animation-related attributes
Table 1: SET attributes
Name |
Attribute |
Remarks |
Android: Movie interpolator |
Whether to share the inserter |
When shared, each of the four subnodes uses an insertor. |
Android: interpolator |
Specifies an animation insertor. |
Use System Resources |
Android: fillEnabled |
|
If it is set to true, both fillAfter and fillBefroe will be true, and the fillBefore and fillAfter attributes will be ignored. |
Android: fillAfter |
Whether the animation is applied after the animation ends |
Boolean |
Android: fillBefore |
Whether the animation is applied before the animation starts. |
Boolean |
Android: repeatMode |
Repeat mode |
"Restart" = start from scratch or "reverse" = start from the end |
Android: repeatCount |
Repeated times |
Integer-1 is an infinite loop |
Android: duration |
Animation duration |
Integer |
Android: startOffset |
Animation time interval (animation duration before execution) |
Long |
Android: zAdjustment |
Define the z order Transformation of an animation |
[Normal] or [top] or [bottom] |
Android: detachWallpaper |
Unknown |
Boolean |
|
|
|
|
|
|
Here we will explain in detail the attributes of the android: interpolator animation plug-in (the embodiment of the animation speed effect ).
1. @ android: anim/accelerate_decelerate_interpolator: acceleration before deceleration
2. @ android: anim/accelerate_interpolator: acceleration
3. @ android: anim/decelerate_interpolator: deceleration
4. @ android: anim/anticipate_interpolator: first move a point in the opposite direction of the animation and then move the track along the set animation, as shown in.
5. anticipate_overshoot_interpolator: first move a point to the opposite direction of the animation, then move it to the end point in the set direction, and then move it back to the final position. For example:
6. @ android: anim/bounce_interpolator: After an animation is moved to the last position, it will have a rebound effect several times, and the animation will be stopped at the specified position.
7. @ android: anim/cycle_interpolator: animation cycle movement.
8. @ android: anim/linear_interpolator: the animation moves at a constant speed.
9. @ android: anim/overshoot_interpolator: the animation moves in the opposite direction to the last position.
Interpolator object |
Resource ID |
Function |
AccelerateDecelerateInterpolator |
@ Android: anim/accelerate_decelerate_interpolator |
Acceleration before deceleration |
AccelerateInterpolator |
@ Android: anim/accelerate_interpolator |
Acceleration |
AnticipateInterpolator |
@ Android: anim/anticipate_interpolator |
Let's take a small step back and then speed up. |
AnticipateOvershootInterpolator |
@ Android: anim/anticipate_overshoot_interpolator |
On the basis of the above, a small step beyond the end and then return to the end |
BounceInterpolator |
@ Android: anim/bounce_interpolator |
Final fl Effect |
CycleInterpolator |
@ Android: anim/cycle_interpolator |
Periodic exercise |
DecelerateInterpolator |
@ Android: anim/decelerate_interpolator |
Deceleration |
LinearInterpolator |
@ Android: anim/linear_interpolator |
Uniform speed |
OvershootInterpolator |
@ Android: anim/overshoot_interpolator |
Quickly reach the destination and return to the destination after a small step |
For a link to modify the attributes of the animation insertor, click the open link.
Table 2: alpha
Name |
Attribute |
Remarks |
Android: toAlpha |
Transparency at animation end |
Float [0, 1] 0 indicates completely transparent 1 indicates completely opaque |
Android: duration |
Animation duration |
Integer |
Android: fromAlpha |
Transparency at the beginning of the animation |
Float [0, 1] 0 indicates completely transparent 1 indicates completely opaque |
|
|
|
Table 3: SCALE
Name |
Attribute |
Remarks |
Android: fromXScale |
Scaling size on the X coordinate at the start of the animation |
Float, 0.0 indicates shrinking to none, 1.0 indicates normal scaling, less than 1 indicates shrinking, greater than 1 indicates enlarging |
Android: toXScale |
Scaling size on X coordinate at animation end |
Same as above |
Android: fromYScale |
Scaling size on Y coordinate at animation start |
Same as above |
Android: toYScale |
Scaling size on Y coordinate at animation end |
Same as above |
Android: strongtx |
Start position of the animation relative to X of the object |
[0%, 100%], 50% indicates the center |
Android: policty |
Start position of the animation relative to Y of the object |
0%, 100%], 50% indicates the center |
Android: duration |
Same as set |
Same as set |
|
|
|
Table 4: TRANSLATE
Name |
Attribute |
Remarks |
Android: fromXDelta |
Position on X coordinate at animation start |
By default, integer takes itself as the reference object. |
Android: toXDelta |
Position on X coordinate at animation end |
By default, integer takes itself as the reference object. |
Android: fromYDelta |
Position on Y coordinate at animation start |
By default, integer takes itself as the reference object. |
Android: toYDelta |
Position on Y coordinate at animation end |
By default, integer takes itself as the reference object. |
|
|
|
Table 5: ROTATE
Name |
Attribute |
Remarks |
Android: fromDegrees |
The angle of the object at the beginning of the animation. |
Negative: clockwise rotation, positive: clockwise rotation |
Android: toDegrees |
The angle of the object at the end of the animation (up to 360 degrees) |
Same as above |
Android: strongtx |
Start position of the animation relative to X of the object |
[0%, 100%], 50% indicates the center |
Android: policty |
Start position of the animation relative to Y of the object |
[0%, 100%], 50% indicates the center |
|
|
1. Implement alpha in. XML:
<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android"> <alpha android: duration = "2000" android: fromAlpha = "0" android: repeatCount = "3" // Number of animation repetitions android: repeatMode = "reverse" // repeated Animation Mode reverse: reverse order, that is, playing the animation from the last frame; restart: order, android: toAlpha = "1.0"/> </set>
Note: If you need to set the animation to android: fillAfter = "true"
Android: fillBefore = "false" must be valid only when the property is under <set>. For example, the valid settings are as follows:
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" > <alpha android:duration="2000" android:fromAlpha="0" android:repeatCount="3" android:repeatMode="reverse" android:toAlpha="1.0" /></set>
Then the above animation stays on the last frame.
Ii. Code Implementation alpha
Animation anim = new AlphaAnimation(0f, 1.0f);anim.setDuration(2000);anim.setRepeatCount(Animation.INFINITE);anim.setRepeatMode(Animation.RESTART);
Iii. scale with XML
<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android" android: fillAfter = "true" android: interpolator = "@ android: anim/decelerate_interpolator"> <! -- The screen stays at the last frame --> <scale android: duration = "2000" android: fromXScale = "0.5" android: fromYScale = "0.5" android: optional Tx = "50.0%"> <! -- The animation starts at 50 relative to the space X coordinate. 0% indicates scaling the animation from the center of the Space --> android: equalty = "50.0%" android: repeatCount = "3" android: repeatMode = "reverse" android: toXScale = "1.0" android: toYScale = "1.0"/> </scale> </set>
Iv. Code Implementation scale
Animation anim1 = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f, 0.5f, 0.5f);anim1.setDuration(2000);anim1.setRepeatCount(3);anim1.setRepeatMode(Animation.REVERSE);anim1.setInterpolator(this, interpolator.accelerate_decelerate);anim1.setFillAfter(true);
V. Implement stranslate in XML
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:fillAfter="true" > <translate android:duration="2000" android:fromXDelta="0" android:fromYDelta="0" android:toXDelta="0" android:toYDelta="1080" /></set>
Code 6: Implement Stranslate
Animation animTranlate = new TranslateAnimation(0, 0, 0, 400);animTranlate.setDuration(2000);animTranlate.setFillAfter(true);animTranlate.setInterpolator(this, interpolator.bounce);text.startAnimation(animTranlate);
VII. Implement rotate in XML
<? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android" android: fillAfter = "true" android: interpolator = "@ android: anim/accelerate_decelerate_interpolator"> <rotate android: duration = "2000" android: fromDegrees = "0" android: Required Tx = "50%" android: Required ty = "50%" android: toDegrees = "180" // negative values are counter-clockwise and positive values are clockwise.
/></set>
8. Code Implementation rotate
Animation animRotate = new RotateAnimation (0f, 360f, Animation. RELATIVE_TO_SELF, 0.5f, Animation. RELATIVE_TO_SELF, 0.5f); animRotate. setDuration (1, 2000); animRotate. setInterpolator (this, interpolator. bounce); animRotate. setFillAfter (true); <span style = "white-space: pre"> animRotate. setStartOffset (2000); </span> // specifies the animation duration text before execution. startAnimation (animRotate );
9. Implementation of composite Animation
<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" android:duration="2000" android:fillAfter="true" android:interpolator="@android:anim/accelerate_decelerate_interpolator" > <alpha android:fromAlpha="0" android:toAlpha="1.0" /> <scale android:fromXScale="0" android:fromYScale="0" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.0" android:toYScale="1.0" /></set>
10. Code-based composite Animation
Animation anim1 = new ScaleAnimation(0.5f, 1.0f, 0.5f, 1.0f, 0.5f, 0.5f);anim1.setDuration(2000);anim1.setRepeatCount(3);anim1.setRepeatMode(Animation.REVERSE);anim1.setInterpolator(this, interpolator.accelerate_decelerate);anim1.setFillAfter(true);// text.setAnimation(anim1);Animation animTranlate = new TranslateAnimation(0, 0, 0, 400);animTranlate.setDuration(2000);animTranlate.setFillAfter(true);animTranlate.setInterpolator(this, interpolator.bounce);// text.startAnimation(animTranlate);Animation animRotate = new RotateAnimation(0f, 360f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,0.5f);animRotate.setDuration(2000);animRotate.setInterpolator(this, interpolator.bounce);animRotate.setFillAfter(true);// text.startAnimation(animRotate);AnimationSet animationSet = new AnimationSet(true);animationSet.addAnimation(animRotate);animationSet.addAnimation(animTranlate);animationSet.addAnimation(anim1);text.startAnimation(animationSet);
For example, set an animation effect on the current page.
Write this in the Code:
public void setCurPage(final int page) {Animation a = AnimationUtils.loadAnimation(MiLaucherMainActivity.this,R.anim.mi_laucher_scale_in);a.setAnimationListener(new Animation.AnimationListener() {@Overridepublic void onAnimationStart(Animation animation) {}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic void onAnimationEnd(Animation animation) {// TODO Auto-generated method stubtv_page.setText((page + 1) + "");tv_page.startAnimation(AnimationUtils.loadAnimation(MiLaucherMainActivity.this,R.anim.mi_laucher_scale_out));}});tv_page.startAnimation(a);}
Mi_laucher_scale_in.xml file: <? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android" android: Using interpolator = "false"> <scale android: duration = "300" android: fromXScale = "1.0" android: fromYScale = "1.0" android: effectx = "50%" android: effecty = "50%" android: toXScale = "0.0" android: toYScale = "1.0"/> </set>
Mi_laucher_scale_out.xml file: <? Xml version = "1.0" encoding = "UTF-8"?> <Set xmlns: android = "http://schemas.android.com/apk/res/android" android: Using interpolator = "false"> <scale android: duration = "300" android: fromXScale = "0.0" android: fromYScale = "1.0" android: effectx = "50%" android: effecty = "50%" android: toXScale = "1.0" android: toYScale = "1.0"/> </set>
End!
Android: animation Problems
You can use property animation to adjust translationX or translationY to move the file.
Android Animation: translateanimation. Can two widgets smoothly execute the same animation?
Yes, you can use an animation to write an internal file separately ?? Method, and the view parameter control is passed in to use the thread for Synchronous playback. Of course, the upstairs said yes. It is a global influence, not the influence of every component.