我正在尝试使用以下代码让我的动画 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
中工作
我们可以将它放在 onResume
或 onWindowsFocusChanged
中。所以在这里我尝试这样做:
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/