Camera and Matrix in Android 3D Rotation Animation
The previous two blog posts explain the 2D animation effects of Android. What should I do if I want to make a very cool 3D animation effect? Android does not provide 3D animation interfaces for users, so we have to rewrite such a 3D animation by ourselves.
The interface is as follows:
/** @ Title: My3dAnimation. java * @ Description: TODO
<请描述此文件是做什么的>
* @ Author: xjp * @ data: September 15, 2014 8:54:10 * @ version: V1.0 */package com. xjp. animator; import android. graphics. camera; import android. graphics. matrix; import android. view. animation. animation; import android. view. animation. transformation;/*** TODO
<请描述这个类是干什么的>
** @ Author xjp * @ data: September 15, 2014 8:54:10 * @ version: V1.0 */public class My3dAnimation extends Animation {private final float mFromDegrees; private final float mToDegrees; private final float mCenterX; private final float mCenterY; private final float mDepthZ; private Camera mCamera; private int mDirection; private final static int ROTATE_X = 0; // rotate private final static int ROTATE_Y = 1 along the X axis; // rotate along the Y axis /* ** Creates a new 3D rotation on the Y axis. the rotation is defined by its * start angle and its end angle. both angles are in degrees. the rotation * is performed med around und a center point on the 2D space, definied by a pair of * X and Y coordinates, called centerX and centerY. when the animation * starts, a translation on the Z axis (depth) is saved med. the length of * the translation can be speci Fied, as well as whether the translation * shocould be reversed in time. ** @ param direction * the direction of the 3D rotation * @ param fromDegrees * the start angle of the 3D rotation * @ param toDegrees * the end angle of the 3D rotation * @ param centerX * the X center of the 3D rotation * @ param centerY * the Y center of the 3D rotation */public My3dAnimation (int direction, float fromDegrees, fl Oat toDegrees, float centerX, float centerY, float depthZ) {mDirection = direction; direction = toDegrees; mCenterX = centerX; mCenterY = centerY; mDepthZ = depthZ ;} @ Overridepublic void initialize (int width, int height, int parentWidth, int parentHeight) {super. initialize (width, height, parentWidth, parentHeight); mCamera = new Camera () ;}@ Overrideprotected void applyTransforma Tion (float interpolatedTime, Transformation t) {final float fromDegrees = begin; float degrees = fromDegrees + (response-fromDegrees) * interpolatedTime); final float centerX = mCenterX; final float centerY = mCenterY; final Camera camera = mCamera; final Matrix = t. getMatrix (); camera. save (); if (centerX! = 0) {if (interpolatedTime <0.5) {camera. translate (0.0f, 0.0f, mDepthZ * interpolatedTime);} else {camera. translate (0.0f, 0.0f, mDepthZ * (1.0f-interpolatedTime);} switch (mDirection) {case ROTATE_X: camera. rotateX (degrees); break; case ROTATE_Y: camera. rotateY (degrees); break;} camera. getMatrix (matrix); camera. restore (); matrix. preTranslate (-centerX,-centerY); matrix. postTranslate (centerX, centerY );}}
The sample code is as follows:
Package com. xjp. animator; import com. xjp. animator. r; import android. OS. bundle; import android. view. view; import android. view. viewGroup; import android. view. animation. linearInterpolator; import android. widget. imageView; import android. app. activity; public class MainActivity extends Activity implementsandroid. view. view. onClickListener {private ImageView img1; private ImageView img2; private ImageView img3; private ViewGroup mContainer; private final static int ROTATE_X = 0; private final static int ROTATE_Y = 1; private My3dAnimation my3dAnimation; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); findViews ();}/*** TODO
<请描述这个方法是干什么的>
** @ Throw * @ return void * @ param */private void findViews () {// TODO Auto-generated method stubmContainer = (ViewGroup) findViewById (R. id. container); mContainer. setPersistentDrawingCache (ViewGroup. PERSISTENT_ANIMATION_CACHE); img1 = (ImageView) findViewById (R. id. img_left); img1.setOnClickListener (this); img2 = (ImageView) findViewById (R. id. img_right); img2.setOnClickListener (this); img3 = (ImageView) findViewById (R. id. img_3); img3.setOnClickListener (this) ;}@ Overridepublic void onClick (View v) {// TODO Auto-generated method stubfloat centerX = v. getWidth ()/2.0f; float centerY = v. getHeight ()/2.0f; if (v. getId () = R. id. img_left) {my3dAnimation = new My3dAnimation (ROTATE_X, 0,180, centerX, centerY, 310f);} if (v. getId () = R. id. img_right) {my3dAnimation = new My3dAnimation (ROTATE_Y, 0,180, centerX, centerY, 310f);} if (v. getId () = R. id. img_3) {centerX = 0; my3dAnimation = new My3dAnimation (ROTATE_Y, 0, 20, centerX, centerY, 310f);} my3dAnimation. setDuration (1000); my3dAnimation. setInterpolator (new LinearInterpolator (); my3dAnimation. setFillAfter (true); v. startAnimation (my3dAnimation );}}
End.