Reprint please indicate source: http://blog.csdn.net/Airsaid/article/details/51591282
This article is from: Travel blog
Objective
The last one wrote the use of tweened animation, because of the length of the reason, the custom tween animation is taken out alone. This article continues to write a motion tween ~
In the previous article, I wrote that Android provides the abstract base class for the animation class as a tweened animation and provides four subclasses: Scaleanimation, Translateanimation, Alphaanimation, Rotateanimation implements four basic animation forms: Zoom change, position change, transparency change, and rotation change.
However, in the actual project development, we may also encounter more complex animation forms. For example, three-dimensional rotation and so on. So this time Android itself will not be able to meet our needs. We need to define a motion tween ourselves.
In fact, it is not difficult to customize the tweened animation, we only need to inherit the animation abstract class, the key to inheriting the animation class is to override the applytransformation of the abstract class (float interpolatedtime, Transformation T) method, the two parameters in the method are described as follows:
* Interpolatedtime: Represents the time of the animation to be compared. This parameter always changes from 0 to 1, regardless of the actual duration of the animation, when the animation plays.
- Transformation: Represents the degree to which a tweened animation distorts a graphic or component at different times.
As can be seen from the above introduction, the focus of implementing a custom tweened animation is to dynamically calculate the degree of distortion of the animation to the graph based on the interpolatedtime time when overriding the Applytransformation method .
Transformation represents the degree of distortion of a picture or view, which encapsulates a matrix object that rotates, positions, tilts, and transforms the matrix object it wraps. The transformation will control the image or view to be transformed accordingly.
The following is an example of a custom three-bit rotation motion tween that demonstrates the use of a custom tween animation.
Use
First you need to introduce a class: Camera. This camera is not a camera for a mobile phone, but a spatial transformation tool that is somewhat similar to the matrix, but more powerful than the matrix.
Camera provides the following common methods:
* Getmatrix (Matrix matrix): Applies the transformation made by the camera to the specified Matrix.
* Rotatex (float deg): Rotates the target component along the x-axis.
* Rotatey (float deg): Rotates the target component along the y-axis.
* Rotatez (float deg): Rotates the target component along the z axis.
* Translate (float x, float y, float z): Causes the target component to shift in three-dimensional space.
* Applytocanvas (canvas canvas): Apply the changes made to the camera to the canvas.
The following program uses the camera to customize the animation in three-dimensional space, the program's Custom animation class code is as follows:
/** * Travel * Time: 2016/06/05 13:03 * Description: Custom tweened Animation Demo (three-dimensional rotation animation) */ Public class customtweenanimation extends Animation { Private floatMcenterx;Private floatMcentery;Private intMduration;PrivateCamera Mcamera =NewCamera (); Public customtweenanimation(floatXfloatYintDuration) { This. Mcenterx = x; This. mcentery = y; This. mduration = Duration; }@Override Public void Initialize(intWidthintHeightintParentwidth,intParentheight) {Super. Initialize (width, height, parentwidth, parentheight);//Set animation durationSetduration (mduration);//Set effect retention after animation finishesSetfillafter (true);//Set constant speed transformSetinterpolator (NewLinearinterpolator ()); }@Override protected void applytransformation(floatInterpolatedtime, transformation t) {Super. applytransformation (Interpolatedtime, T); Mcamera.save ();//Control the offset on the x, Y, Z axes according to interpolatedtime timeMcamera.translate (100.0F-100.0F * interpolatedtime,150.0F * interpolatedtime- Max,80.0F-80.0f * interpolatedtime);//set to rotate different angles on the x-axis according to Interpolatedtime timeMcamera.rotatex ( the* interpolatedtime);//set to rotate different angles on the y-axis according to Interpolatedtime timeMcamera.rotatey ( the* interpolatedtime);//Get the transformation parameter of the Matrix objectMatrix matrix = T.getmatrix ();//Apply the camera's transformations to the transformation matrixMcamera.getmatrix (matrix); Matrix.pretranslate (-mcenterx,-mcentery); Matrix.posttranslate (Mcenterx, mcentery); Mcamera.restore (); }}
To start an animation:
CustomTweenAnimation customTweenAnimation = new CustomTweenAnimation(50, 50, 5000);mTextView.startAnimation(customTweenAnimation);
Operation Result:
Custom tweened animations for the Android animation series