/** * 自定义Y轴旋转动画 */ public class ViewYAnimation extends Animation { int centerX, centerY; Camera camera = new Camera(); /** * 获取坐标,定义动画时间 * @param width * @param height * @param parentWidth * @param parentHeight */ @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); //获得中心点坐标 centerX = width / 2; centerY = width / 2; //动画执行时间 自行定义 setDuration(2 * 300); setInterpolator(new DecelerateInterpolator()); } /** * 旋转的角度设置 * @param interpolatedTime * @param t */ @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final Matrix matrix = t.getMatrix(); camera.save(); //中心是Y轴旋转,这里可以自行设置X轴 Y轴 Z轴 camera.rotateY(360 * interpolatedTime); //把我们的摄像头加在变换矩阵上 camera.getMatrix(matrix); //设置翻转中心点 matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX,centerY); camera.restore(); } }
mViewYAnimation = new ViewYAnimation(); mViewYAnimation.setRepeatCount(Animation.INFINITE);//循环 ivDemolition.startAnimation(mViewYAnimation);//开始Y轴动画 ivDemolition是view