一、概念
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.Transformation;
public class Rotate3DAnimation extends Animation {
// 3d rotate
private float mFromDegrees;
private float mToDegrees;
private float mCenterX;
private float mCenterY;
private Camera mCamera;
public Rotate3DAnimation(float fromDegrees, float toDegrees)
{
mFromDegrees = fromDegrees;
mToDegrees = toDegrees;
}
@Override
public void initialize(int width, int height, int parentWidth,
int parentHeight)
{
super.initialize(width, height, parentWidth, parentHeight);
mCenterX = width / 2;
mCenterY = height / 2;
mCamera = new Camera();
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t)
{
final float fromDegrees = mFromDegrees;
float degrees = fromDegrees + (mToDegrees - mFromDegrees) * interpolatedTime;
final Matrix matrix = t.getMatrix();
mCamera.save();
mCamera.rotateY(degrees);
mCamera.getMatrix(matrix);
mCamera.restore();
//
matrix.preTranslate(-mCenterX, -mCenterY);
matrix.postTranslate(mCenterX, mCenterY);
}
} 看代码,首先重载了initialize()方法,在里面初始化中间坐标mCenterX、mCenterY 以及实例化Camera对象,initialize()是一个回调函数告诉Animation目标View的大小参数,在这里可以初始化一些相关的参数,例如设置动画持续时间、设置Interpolator、设置动画的参考点等。applyTransformation (float interpolatedTime, Transformation t)函数来实现自定义动画效果,在绘制动画的过程中会反复的调用applyTransformation 函数。通过参数Transformation 来获取变换的矩阵(matrix),final Matrix matrix = t.getMatrix(),通过改变矩阵就可以实现各种复杂的效果。Camera类是用来实现绕Y轴旋转后透视投影的,首先通过t.getMatrix()取得当前的矩阵,然后camera.rotateY对矩阵进行旋转。preTranslate函数是在旋转前移动而postTranslate是在旋转完成后移动,主要作用是让对象围绕自己的中心二旋转。Android Camera 3D效果,布布扣,bubuko.com
原文:http://blog.csdn.net/wangjinyu501/article/details/23673461