首页 > 代码库 > android 3D旋转效果实现

android 3D旋转效果实现

一说到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);    }}

 

android 3D旋转效果实现