A tween animation will perform a series of simple conversions on the content of the view object. In animation, there are four common classes for tween animation, alphaanimation (Transparency gradient ), rotateanimation, scaleanimation, translateanimation, and animationset ), the following describes the functions and parameters of the construction methods of common animation special effect classes.
(1) alphaanimation
Public alphaanimation (float fromalpha, float toalpha)
Fromalpha-transparency at the beginning, where 1 indicates completely opaque, 0 indicates completely transparent
Transparency at the end of toalpha
Setduration (long durationmillis) sets the animation execution time
When setfillafter (Boolean fillafter) is set to true, the animation stops after execution. By default, the animation returns to the initial effect after execution.
Setrepeatcount (INT repeatcount) sets the number of animation repetitions. The default value of repeatcount is 0, that is, once, 1, that is, twice
Setrepeatmode (INT repeatmode) sets the animation repetition mode, which has animation. reverse and animation. restart method. The default value is animation. restart, animation. restart indicates that, for example, you set the number of retries to 1. After the first animation is executed, you can return to the animation and then execute the second animation. in reverse, for example, if your animation is not transparent -----> transparent, after the first animation is executed, it becomes opaque. Then, after the second animation is executed, the animation is never transparent to transparency, do you understand what I mean?
I will introduce several common methods, and other animations also have the above methods. Then I will introduce the setinterpolator (interpolator) method.
(2) rotateanimation
Public rotateanimation (float fromdegrees, float todegrees, int limit txtype, float limit txvalue, int limit tytype, float limit tyvalue)
Fromdegrees animation start angle
Todegrees animation end angle
Returns the X and Y coordinates of txvalue and tyvalue around the center of the rotation.
Relative txtype, relative tytype indicates the relative link type of the rotation center. Three animation types are available. absolute, animation. relative_to_self, or animation. relative_to_parent, animation. absolute absolute coordinate type, that is, the position relative to the O point, animation. relative_to_self is relative to itself, and the point in the upper left corner of the view is the O point position, animation. the value of relative_to_parent is the O point in the upper left corner of the parent view, that is, the position of the viewgroup in which the view is located.
(3) scaleanimation
Public scaleanimation (float fromx, float tox, float fromy, float toy, int limit txtype, float limit txvalue, int limit tytype, float limit tyvalue)
Float fromx, float tox X axis from the start size to the end size
Float fromy, float toy the size from the start to the end of the Y axis
Pivotxvalue and pivotyvalue are the X and Y coordinates of the scaled center.
The relative link type of the zoomed txtype and tytype zoom center. There are three types of animations. absolute, animation. relative_to_self, or animation. relative_to_parent, animation. absolute absolute coordinate type, that is, the position relative to the O point, animation. relative_to_self is relative to itself, and the point in the upper left corner of the view is the O point position, animation. the value of relative_to_parent is the O point in the upper left corner of the parent view, that is, the position of the viewgroup in which the view is located.
(4) translateanimation
Public translateanimation (INT fromxtype, float fromxvalue, int toxtype, float toxvalue, int fromytype, float fromyvalue, int toytype, float toyvalue)
Fromxtype relative type of start point on the X axis
Fromxvalue start point value
Relative type of end point on the X axis of toxtype
Toxvalue, end point value
Same as Y axis
(5) animationset
This is an animation collection class. You can set multiple animations to execute together. This is simple and I will not introduce it much.
Explanation of interpolator
Interpolator defines the rate of change of an animation ). This allows the basic animation effects (alpha, scale, translate, and rotate) to accelerate, slow down, and repeat.
Interpolator defines the speed at which an animation changes, including constant speed, positive acceleration, negative acceleration, and irregular acceleration. Interpolator is a base class that encapsulates all the common methods of interpolator. It has only one method, that is, getinterpolation (float input ), this method maps a point on the timeline to a multiplier to be applied to the transformations of an animation. Android provides several interpolator
Sub-class to implement different velocity curves, as shown below:
Acceleratedecelerateinterpolator |
The speed changes are slow at the beginning of the animation and in the middle of the introduction. |
Accelerateinterpolator |
Speed changes are slow at the beginning of the animation and acceleration starts. |
Cycleinterpolator |
Specifies the number of times the animation is played cyclically, and the speed changes along the sine curve. |
Decelerateinterpolator |
The speed changes slowly at the beginning of the animation, and then starts to slow down. |
Linearinterpolator |
Changes at an even rate in an animation |
The above is an animation constructed by code. Of course, we can also write an animation through an XML file. I personally recommend using an XML file.
The animation is placed in anim under res. The following describes how to generate code and XML files.
Alphaanimation code implementation
// Construct the animation with transparent changes: alphaanimation = new alphaanimation (1.0f, 0.0f); // set the animation execution time alphaanimation. setduration (2000); // sets the animation repetition mode alphaanimation. setrepeatmode (animation. reverse); // sets the number of animation repetitions alphaanimation. setrepeatcount (5); // sets the animation change rate alphaanimation. setinterpolator (New accelerateinterpolator ());
Alphaanimation XML implementation
<?xml version="1.0" encoding="utf-8"?><alphaxmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2000" android:repeatCount="5" android:repeatMode="reverse" android:interpolator="@android:anim/accelerate_interpolator" />
You can get the animation through animationutils. loadanimation (this, R. anim. Alpha ).
Rotateanimation code implementation
Animation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);rotateAnimation.setDuration(2000);rotateAnimation.setRepeatMode(Animation.RESTART);rotateAnimation.setRepeatCount(5);rotateAnimation.setInterpolator(new LinearInterpolator());
Rotateanimation XML implementation
<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:duration="2000" android:repeatCount="5" android:repeatMode="reverse" android:interpolator="@android:anim/linear_interpolator"></rotate>
It is worth noting that Android: Release Tx = "50%", Android: Release ty = "50%" should be added "% ", do not add "%" to the parent container. This is important here.
The rotateanimation animation can customize the circular progress bar. For example, it is defined in an XML file.
<?xml version="1.0" encoding="utf-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/loading" android:duration="1000" android:interpolator="@android:anim/linear_interpolator" android:pivotX="50.0%" android:pivotY="50.0%" android:repeatCount="infinite" />
Let's implement the other two types by yourself. I believe you have read the introduction and it is not very difficult to implement the other two types. Here we will introduce the tween animation, Which is messy, thank you for your valuable comments and suggestions! Don't talk about it. I went to dinner!