gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 15:39:42 26 4
gpt4 key购买 nike

我正在尝试使用以下代码让我的动画 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com