显示动态的GIF动画使用绘制的问题动画、动态、问题、GIF

2023-09-07 09:26:54 作者:爱到深处难以自拔,

我试图对循环用下面的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) or setBackgroundResource(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!