使用Camera实现3D变换效果
最后来简单介绍下Android提供的android.graphics.Camera类,通过该类可以在2D条件下实现3D动画效果,该类可以看做一个视图显示的3D空间,然后可以在里面做各种操作。把上面的ViewAnimation修改为如下实现可以具体看看Camera的功能:
- class ViewAnimation extends Animation {
- int mCenterX;//记录View的中间坐标
- int mCenterY;
- Camera camera = new Camera();
- public ViewAnimation() {
- }
-
- @Override
- public void initialize(int width, int height, int parentWidth,
- int parentHeight) {
- super.initialize(width, height, parentWidth, parentHeight);
- //初始化中间坐标值
- mCenterX = width/2;
- mCenterY = height/2;
- setDuration(2500);
- setFillAfter(true);
- setInterpolator(new LinearInterpolator());
- }
-
- @Override
- protected void applyTransfORMation(float interpolatedTime,
- Transformation t) {
- // final Matrix matrix = t.getMatrix();
- // matrix.setScale(interpolatedTime, interpolatedTime);
- // //通过坐标变换,把参考点(0,0)移动到View中间
- // matrix.preTranslate(-mCenterX, -mCenterY);
- // //动画完成后再移回来
- // matrix.postTranslate(mCenterX, mCenterY);
- final Matrix matrix = t.getMatrix();
- camera.save();
- camera.translate(0.0f, 0.0f, (1300 - 1300.0f * interpolatedTime));
- camera.rotateY(360 * interpolatedTime);
- camera.getMatrix(matrix);
- matrix.preTranslate(-mCenterX, -mCenterY);
- matrix.postTranslate(mCenterX, mCenterY);
- camera.restore();
- }
- }
camera.translate(0.0f, 0.0f, (1300 - 1300.0f * interpolatedTime))在第一次调用的时候interpolatedTime值为0,相当于把ImageView在Z轴后移1300像素,然后逐步的往前移动到0,同时camera.rotateY(360 * interpolatedTime)函数又把ImageView沿Y轴翻转360度,最终效果如下:
下图是中移动的Ophone的截图,当然,同样兼容android,
0