android 旋转动画失效,[杂记] 华为手机上旋转动画不动的问题

在华为P9等部分手机上,使用XML定义的旋转动画在ImageView上无法正常显示,但在Java代码中动态创建并应用旋转动画后,问题得到解决。此问题可能与设备特定的Android版本或EMUI版本有关。
摘要由CSDN通过智能技术生成

问题描述

假设有一个 ImageView, 想给它设置一个旋转动画,效果如图:

bb9a696bee51?utm_campaign=maleskine&utm_content=note&utm_medium=seo_notes&utm_source=recommendation

旋转动画

在布局中,ImageView 这样写:

android:id="@+id/loading"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:src="@drawable/loading_player"

android:scaleType="fitXY"

android:visibility="visible"/>

其中 "@drawable/loading_player" 写成这样:

xmlns:android="http://schemas.android.com/apk/res/android"

android:drawable="@drawable/loading_player"

android:duration="100"

android:pivotX="50%"

android:pivotY="50%"/>

这本来是一个简单的动画,在大部分手机上都能旋转,但是在华为部分手机(例如p9,android7.0, emui5.0)上就是不动,不知道原因。

解决方法

于是只好在 Java 代码中创建动画,

int fromDegrees = 0;

int toDegrees = 360;

float pivotX = 0.5f;

float pivotY = 0.5f;

mLoadingAnimation = new RotateAnimation(fromDegrees, toDegrees,

Animation.RELATIVE_TO_SELF, pivotX, Animation.RELATIVE_TO_SELF, pivotY);

mLoadingAnimation.setDuration(1000);

mLoadingAnimation.setRepeatCount(Animation.INFINITE);

mLoadingAnimation.setInterpolator(new LinearInterpolator());

mLoadingView.setAnimation(mLoadingAnimation);

mLoadingAnimation.startNow();

然后华为手机上,这个旋转动画就动了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值