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

问题描述

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

旋转动画

在布局中,ImageView 这样写:

<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" 写成这样:

<?xml version="1.0" encoding="utf-8"?>
<animated-rotate 
    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();

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。