您的位置:首页 > 移动开发 > Android开发

android 翻转效果动画源码

2014-10-20 19:40 405 查看
     最近项目上要求做一个翻转的动画效果,由于动画还没有怎么使用过。然后再网上找到一份很实用的翻转动画先用起来,以后再学习下动画相关的。

源码修改后如下:

public class FlipAnimator extends Animation {

private Camera camera;

private float centerX;

private float centerY;

public FlipAnimator() {
setFillAfter(true);
}

@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
camera = new Camera();
this.centerX = width / 2;
this.centerY = height / 2;
}

@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// Angle around the y-axis of the rotation at the given time. It is
// calculated both in radians and in the equivalent degrees.
final double radians = Math.PI * interpolatedTime;
float degrees = (float) (180.0 * radians / Math.PI);

degrees = -degrees;

// Once we reach the midpoint in the animation, we need to hide the
// source view and show the destination view. We also need to change
// the angle by 180 degrees so that the destination does not come in
// flipped around. This is the main problem with SDK sample, it does not
// do this.
if (interpolatedTime >= 0.5f) {
degrees += 180.f;
}

final Matrix matrix = t.getMatrix();

camera.save();
camera.translate(0.0f, 0.0f, (float) (150.0 * Math.sin(radians)));
// camera.rotateX(degrees);
camera.rotateY(degrees);
// camera.rotateZ(degrees);
camera.getMatrix(matrix);
camera.restore();

matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
}
}

        动画通过Camera 和Matrix实现。文中注视的部分camera.rotateX(degrees)和camera.rotateZ(degrees)分别为X和Z轴的旋转,没有用到故先注释。
        使用方法很简单

FlipAnimator flipAnimator = new FlipAnimator();
flipAnimator.setDuration(200);
startAnimation(flipAnimator);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息