问题描述
我试图对循环用下面的code我的动画GIF动画,但我没有任何运气...
I'm attempting to have my animated gif animate on loop with the following code but I'm not having any luck...
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>
任何人有任何想法,为什么它没有动画?
Anyone have any idea as to why it isn't animating?
推荐答案
看来我是遇到了同样的问题,当我用无论是 setImageResource(R.drawable.load_animation)
或 setBackgroundResource(R.drawable.load_animation)
,图像不显示。
Seems that I was experiencing the same problem, when I used whether it is setImageResource(R.drawable.load_animation)
orsetBackgroundResource(R.drawable.load_animation)
, the image wasn't displaying.
然后我尝试把code。在我的XML视图文件,用安卓背景=@可绘制/ load_animation
得到了图像显示,但仍然没有动画,它只是显示静态图像的。
then I try to put the code in my XML view file, with android:background="@drawable/load_animation
got the image showed, but still not animated, it was only displaying a static image.
再经过一段时期的我发现, AnimationDrawable
不能的onCreate
工作的谷歌搜索我们可以把它放在 onResume
或 onWindowsFocusChanged
。所以在这里我尝试用这样的:
then after googling around a while I found that AnimationDrawable
can not work in onCreate
we can either put it in onResume
or onWindowsFocusChanged
. So here I try with this:
public void onWindowFocusChanged(boolean hasFocus) {
loadingAnimation = (AnimationDrawable)
findViewById(R.id.img_loading).getBackground();
if (hasFocus) {
loadingAnimation.start();
}
else {
loadingAnimation.stop();
}
}
最后它的工作原理时,GIF均显示和动画!
finally it works, the gif both showed and animated!
这篇关于显示动态的GIF动画使用绘制的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!