Android系列之浅谈Android 3D旋转

在ANDROID中实现3D旋转直接使用animation配合camera就可以实现,在apidemo里就有这样的实例 我们首先做一个继承animation的类Rotate3d.java 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 public class Rotate3d extends Animation { private float mFromDegree; private float mToDegree; private float mCenterX; private float mCenterY; private float mLeft; private float mTop; private Camera mCamera; private static final String TAG = "Rotate3d"; public Rotate3d(float fromDegree, float toDegree, float left, float top, float centerX, float centerY) { this.mFromDegree = fromDegree; this.mToDegree = toDegree; this.mLeft = left; this.mTop = top; this.mCenterX = centerX; this.mCenterY = centerY; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final float FromDegree = mFromDegree; float degrees = FromDegree + (mToDegree - mFromDegree) * interpolatedTime; final float centerX = mCenterX; final float centerY = mCenterY; final Matrix matrix = t.getMatrix(); if (degrees <= -76.0f) { degrees = -90.0f; mCamera.save(); mCamera.rotateY(degrees); mCamera.getMatrix(matrix); mCamera.restore(); } else if(degrees >=76.0f){ degrees = 90.0f; mCamera.save(); mCamera.rotateY(degrees); mCamera.getMatrix(matrix); mCamera.restore(); }else{ mCamera.save(); //这里很重要哦。 mCamera.translate(0, 0, centerX); mCamera.rotateY(degrees); mCamera.translate(0, 0, -centerX); mCamera.getMatrix(matrix); mCamera.restore(); } matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } } public class Rotate3d extends Animation { private float mFromDegree; private float mToDegree; private float mCenterX; private float mCenterY; private float mLeft; private float mTop; private Camera mCamera; private static final String TAG = "Rotate3d"; public Rotate3d(float fromDegree, float toDegree, float left, float top, float centerX, float centerY) { this.mFromDegree = fromDegree; this.mToDegree = toDegree; this.mLeft = left; this.mTop = top; this.mCenterX = centerX; this.mCenterY = centerY; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final float FromDegree = mFromDegree; float degrees = FromDegree + (mToDegree - mFromDegree) * interpolatedTime; final float centerX = mCenterX; final float centerY = mCenterY; final Matrix matrix = t.getMatrix(); if (degrees <= -76.0f) { degrees = -90.0f; mCamera.save(); mCamera.rotateY(degrees); mCamera.getMatrix(matrix); mCamera.restore(); } else if(degrees >=76.0f){ degrees = 90.0f; mCamera.save(); mCamera.rotateY(degrees); mCamera.getMatrix(matrix); mCamera.restore(); }else{ mCamera.save(); //这里很重要哦。 mCamera.translate(0, 0, centerX); mCamera.rotateY(degrees); mCamera.translate(0, 0, -centerX); mCamera.getMatrix(matrix); mCamera.restore(); } matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } } 有了这个类一切都会变得简单的,接着只要在activity中写两个Rotate3d的对象,让两个view,分别做这两个对象的animation就好了; 1 2 3 4 5 6 7 8 9 10 11 //下面两句很关键哦, Rotate3d leftAnimation = new Rotate3d(-0, -90, 0, 0, mCenterX, mCenterY); Rotate3d rightAnimation = new Rotate3d(-0+90, -90+90, 0.0f, 0.0f, mCenterX, mCenterY); leftAnimation.setFillAfter(true); leftAnimation.setDuration(1000); rightAnimation.setFillAfter(true); rightAnimation.setDuration(1000); mImageView1.startAnimation(leftAnimation); mImageView2.startAnimation(rightAnimation); 最后就是还要写一下mImageView1,mImageView2的xml, 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

 

你可能感兴趣的:(Android系列之浅谈Android 3D旋转)