一說到3D,可能第一反應就是使用OpenGL ES。。。。但是,實現這么個小功能,要動用這玩意,莫名的恐懼啊!!!!至今也沒弄明白這個怎么玩。。。
好吧,幸虧還有個Camera類可以幫助我們,據說底層實現實現也是使用的是OpenGL ES
注意:使用的是android.graphics.Camera
話不多說了,直接上代碼:
public class _3DAnimation extends Animation { private float mFromDegrees; private float mToDegrees; private float mCenterX; private float mCenterY; private Camera mCamera; public _3DAnimation(float fromDegress,float toDegress){ this.mFromDegrees=fromDegress; this.mToDegrees=toDegress; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); this.mCenterX=width/2; this.mCenterY=height/2; mCamera=new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { super.applyTransformation(interpolatedTime, t); final float fromDegrees = mFromDegrees; float degrees = fromDegrees + (mToDegrees - mFromDegrees) * interpolatedTime; final Matrix matrix = t.getMatrix(); //interpolatedTime 0~1變化 mCamera.save(); mCamera.rotateY(degrees); mCamera.getMatrix(matrix); mCamera.restore(); matrix.preTranslate(-mCenterX, -mCenterY);//相機位於(0,0),移動圖片,相機位於圖片中心 matrix.postTranslate(mCenterX, mCenterY); } }