关于ObjectAnimator的rotationX、rotationY在部分华为设备上消失的问题

吐槽一下HUAWEI,你为什么要这么的秀

高高兴兴的写了个绕Y轴旋转的动画,运行一下完美展示

ObjectAnimator animator = ObjectAnimator.ofFloat(imageView, "rotationY", 0, 360f);
animator.setDuration(3_000);
animator.start();

在这里插入图片描述
你以为这样就完了吗?当然没有,测试MM过来说你看这个’问号’不见了;然后就开启了Debug之旅,然而发现并没有什么用。

机智的我立马跑到度娘中输入"华为手机 RotationY",一看结果原来不是我写的Bug呀真好!

在这里插入图片描述

实际测试中发现rotationXrotationY都没有效果,会使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);

最后还是忍不住在吐槽吐槽HUAWEI能不能不要这么秀?

发布了140 篇原创文章 · 获赞 546 · 访问量 54万+

猜你喜欢

转载自blog.csdn.net/a_zhon/article/details/89517374