Android Custom Animation and android Custom Animation
Android Custom Animation inherits the Animation and overwrites the initialize and applyTransformation in it. initialize the initialize method and change the corresponding rail arrays in the applyTransformation method!
Go directly to demo!
Public class Rotate3dAnimation extends Animation {
// Start angle
Private final float mFromDegrees;
// End angle
Private final float mToDegrees;
// Center point
Private final float mCenterX;
Private final float mCenterY;
Private final float mDepthZ;
// Do You Need To twist
Private final boolean mReverse;
// Camera
Private Camera mCamera;
Public Rotate3dAnimation (float fromDegrees, float toDegrees, float centerX,
Float centerY, float depthZ, boolean reverse ){
MFromDegrees = fromDegrees;
MToDegrees = toDegrees;
MCenterX = centerX;
MCenterY = centerY;
MDepthZ = depthZ;
MReverse = reverse;
}
@ Override
Public void initialize (int width, int height, int parentWidth,
Int parentHeight ){
Super. initialize (width, height, parentWidth, parentHeight );
MCamera = new Camera ();
}
// Generate Transformation
@ Override
Protected void applyTransformation (float interpolatedTime, Transformation t ){
Final float fromDegrees = mFromDegrees;
// Generate an intermediate Angle
Float degrees = fromDegrees
+ (MToDegrees-fromDegrees) * interpolatedTime );
Final float centerX = mCenterX;
Final float centerY = mCenterY;
Final Camera camera = mCamera;
Final Matrix matrix = t. getMatrix ();
Camera. save ();
If (mReverse ){
// Camera. translate (0.0f, 0.0f, mDepthZ * interpolatedTime );
Camera. rotateY (mCenterY );
} Else {
// Camera. translate (0.0f, 0.0f, mDepthZ * (1.0f-interpolatedTime ));
Camera. rotateY (-mCenterY );
}
Camera. rotateY (degrees );
// Obtain the transformed Matrix
Camera. getMatrix (matrix );
Camera. restore ();
Matrix. preTranslate (-centerX,-centerY );
Matrix. postTranslate (centerX, centerY );
}
}
This is an effect that imitates 3D Rotation animation. The following is a call method. mSecondMenuLayout refers to the view to start the animation.
/**
*
* @ Param start angle
* @ Param end angle
*/
Private void applyRotation (float start, float end ){
// Calculate the center point
Final float centerX = mSecondMenuLayout. getWidth ()/2.0f;
// Final float centerY = mSecondMenuLayout. getHeight ()/2.0f;
Final Rotate3dAnimation rotation = new Rotate3dAnimation (start, end,
Centerx, 360,310.0 f, true );
// Set the animation duration
Rotation. setDuration (1000 );
// Whether the animation is retained as is after it is set
Rotation. setFillAfter (false );
// Set the animation plug-in
Rotation. setInterpolator (new AccelerateDecelerateInterpolator ());
// Set an animation listener
Rotation. setAnimationListener (mAnimListener );
MSecondMenuLayout. startAnimation (rotation );
}