一、业务需求
需要对一个button进行动画以Y轴为中心旋转的动画效果
二、动画效果
三、业务需求分析
1. 如上图实现的一种3D效果的旋转效果,常规无法实现,我们需要自定义一个动画;
2. 中心坐标轴为图的中心,可根据自己的需求进行设置,最好为Y轴对称控件,画View控件完成一次动画的时间为3秒;
3. 设置旋转轴和旋转角度,并启动动画;
4. 使用动画;
四、代码
(1)一个自定义动画的类:
import android.graphics.Camera;
import android.graphics.Matrix;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Transformation;
/**
* 自定义Y轴旋转动画
* Created by Administrator on 2017/2/10.
*/
public class MyYAnimation 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(3 * 1000);
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();
}
}
(2)控件对其动画的使用:
MyYAnimation myYAnimation = new MyYAnimation();
myYAnimation.setRepeatCount(Animation.INFINITE); //旋转的次数(无数次)
iv_enterh5.startAnimation(myYAnimation);
上面我直接初始化,让他进入页面就开始旋转,旋转的次数的无数次。根据开发者实际情况,我们也可以设置为若干次动画。或者执行的开始有点击事件触发,需要根据开发者需求来具体布置;
五、好了,测试一下吧,相信你要的功能就这么愉快的实现了。