吐槽一下HUAWEI,你为什么要这么的秀
高高兴兴的写了个绕Y轴旋转的动画,运行一下完美展示
ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotationY", 0, 360f);
animator.setDuration(3_000);
animator.start();
你以为这样就完了吗?当然没有,测试MM过来说你看这个’问号’不见了;然后就开启了Debug之旅,然而发现并没有什么用。
机智的我立马跑到度娘中输入"华为手机 RotationY"
,一看结果原来不是我写的Bug呀真好!
实际测试中发现rotationX
,rotationY
都没有效果,会使View直接消失,而rotation
是可以的
接下来就是一顿搜索找个代替的方法让它动起来,如下代码:
public class RotationYAnimation extends Animation {
private int centerX, centerY;
private Camera camera = new Camera();
/**
* 获取坐标,定义动画时间
*/
@Override
public void initialize(int width, int height, int parentWidth, int parentHeight) {
super.initialize(width, height, parentWidth, parentHeight);
//获得中心点坐标
centerX = width / 2;
centerY = width / 2;
}
/**
* 旋转的角度设置
*/
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final Matrix matrix = t.getMatrix();
camera.save();
//设置camera的位置
camera.setLocation(0,0,180);
//中心是Y轴旋转,这里可以自行设置X轴 Y轴 Z轴
camera.rotateY(360 * interpolatedTime);
//把我们的摄像头加在变换矩阵上
camera.getMatrix(matrix);
//设置翻转中心点
matrix.preTranslate(-centerX, -centerY);
matrix.postTranslate(centerX, centerY);
camera.restore();
}
}
使用方法如下:
RotationYAnimation rotationY = new RotationYAnimation();
rotationY.setDuration(3_000);
imageView.startAnimation(rotationY);