关于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能不能不要这么秀?
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Code-Porter

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值