Android Custom Animation and android Custom Animation

Source: Internet
Author: User

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 );
}

Related Article

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.