Android 3D animation Rotation

Source: Internet
Author: User


A LinearLayout has a background image with an ImageButton sub-component, which is rotated 360 degrees clockwise.

MainActivity:

I only started the Activity to display the animation effect, so it was simulated in the oncreate () method. However, the size value of imageButton is not easy to obtain at this time. This problem has been mentioned in my previous article.

Get component size in oncreate: http://blog.csdn.net/u010152805/article/details/17592083

Public class MainActivity extends Activity {private ImageButton ib; float w, y; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the size of imageButton in oncreate (). ViewTreeObserver vto = ib. getViewTreeObserver (); vto. addOnGlobalLayoutListener (new OnGlobalLayoutListener () {@ Overridepublic void onGlobalLayout () {ib. getViewTreeObserver (). removeGlobalOnLayoutListener (this); w = ib. getWidth (); y = ib. getHeight (); rotateAnim (w, y) ;}});}/*** implement animation rotation effect */private void rotateAnim (float w, float y) {// Image rotation Center Coordinate final float centerX = w/2.0f; final float centerY = y/2.0f; RotateAnimation rotation; rotation = new RotateAnimation (0,-360, centerX, centerY, 310.0f, false); rotation. setDuration (1500); // animation duration rotation. setFillAfter (true); rotation. setInterpolator (new DecelerateInterpolator (); // sets the acceleration rotation. setRepeatCount (Animation. INFINITE); // number of repeated rotation. setRepeatMode (Animation. RESTART); // repeat the animation ib. startAnimation (rotation );}}
Animation:

/*** Three key categories of 3D Animation effect ** (Animation/Camera/Matrix) */public class RotateAnimation extends Animation {/** start angle */private final float mFromDegrees; /** end angle */private final float mToDegrees;/** central coordinate X axis */private final float mCenterX;/** central coordinate Y axis */private final float mCenterY; /** opposite side */private final boolean mReverse;/** Camera class */private Camera mCamera; /*** constructor ** @ param fromDegrees start angle * @ param toDegrees end angle * @ param centerX Center Coordinate * @ param centerY Center Coordinate * @ param depthZ * @ param reverse */ public RotateAnimation (float fromDegrees, float toDegrees, float centerX, float centerY, float depthZ, boolean reverse) {response = response; response = toDegrees; mCenterX = centerX; mCenterY = centerY; mReverse = reverse ;} /*** initialize the animation object size */@ Overridepublic void initialize (int width, int height, int parentWidth, int parentHeight) {super. initialize (width, height, parentWidth, parentHeight); mCamera = new Camera () ;}@ Overrideprotected void applyTransformation (float interpolatedTime, Transformation tran) {final float fromDegrees = Response; float degrees = centers + (response-fromDegrees) * interpolatedTime); final float centerX = mCenterX; final float centerY = mCenterY; final Camera camera = mCamera; final Matrix matrix = tran. getMatrix (); camera. save (); // save the status if (mReverse) {// x, y, and z respectively from the central axis. translate (0.0f, 0.0f, 0.0f);} else {camera. translate (0.0f, 0.0f, 0.0f);} // you can rotate camera in the X, Y, and Z directions. rotateY (degrees); camera. getMatrix (matrix); // restore the Saved state camera. restore (); matrix. preTranslate (-centerX,-centerY); matrix. postTranslate (centerX, centerY );}}


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.