android - 使用animate drawable问题显示Animated gif

标签 android

我正在尝试使用以下代码让我的动画 gif 循环播放,但我没有任何运气......

Java

ImageView img = (ImageView)findViewById(R.id.load_img);
img.setBackgroundResource(R.drawable.load_animation);

AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();

frameAnimation.start();

XML

<animation-list android:oneshot="false" xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/load_0000" android:duration="50" />
<item android:drawable="@drawable/load_0001" android:duration="50" />
<item android:drawable="@drawable/load_0002" android:duration="50" />
<item android:drawable="@drawable/load_0003" android:duration="50" />
<item android:drawable="@drawable/load_0004" android:duration="50" />
<item android:drawable="@drawable/load_0005" android:duration="50" />
<item android:drawable="@drawable/load_0006" android:duration="50" />
<item android:drawable="@drawable/load_0007" android:duration="50" />
<item android:drawable="@drawable/load_0008" android:duration="50" />
<item android:drawable="@drawable/load_0009" android:duration="50" />
</animation-list>

有人知道为什么它没有动画吗?

最佳答案

似乎我遇到了同样的问题,当我使用它是 setImageResource(R.drawable.load_animation) 还是 setBackgroundResource(R.drawable.load_animation),图像未显示。

然后我尝试将代码放入我的 XML View 文件中,使用 android:background="@drawable/load_animation 显示了图像,但仍然不是动画,它只显示静态图像。

然后在谷歌搜索了一段时间后,我发现 AnimationDrawable 不能在 onCreate 中工作 我们可以将它放在 onResumeonWindowsFocusChanged 中。所以在这里我尝试这样做:

    public void onWindowFocusChanged(boolean hasFocus) {
    loadingAnimation = (AnimationDrawable)
    findViewById(R.id.img_loading).getBackground();
    if (hasFocus) {
        loadingAnimation.start();
    } 
    else {
        loadingAnimation.stop();
    }
}

终于成功了,gif 既显示又动画了!

关于android - 使用animate drawable问题显示Animated gif,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6107998/

相关文章:

android - 每次使用 GoogleSignInApi 显示帐户选择器

android - 保护Android apk防止反编译、网络嗅探等

android - Kivy ScrollView+ Accordion 错误

android - 使用 ActivityUnitTestCase 和 PreferenceManager 对 Android 进行单元测试

java - 从目标主机获取 JSON 对象。错误 : Target host must not be null

android - 如何设置 Recurring AlarmManager 以每天执行代码

android - 使用 IBM Mobile First Platform 生成 APK

android - 单击不同的按钮时如何更改按钮文本并使其功能相同?安卓

java - "ArrayAdapter requires the resource ID to be a TextView"有时在执行时

java - 如何在AlertDialog中显示数组列表