2011-05-24 49 views
2

我试图让我的gif动画上用下面的代码循环动画,但我没有任何运气显示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> 

任何人有任何想法,为什么它是不是动画?

回答

1

尝试拨打setImageResource(R.drawable.load_animation)而不是setBackgroundResource(R.drawable.load_animation)

10

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

然后我试着把代码放到我的XML视图文件中,用android:background="@drawable/load_animation 得到的图像显示出来,但仍然没有动画,它只显示一个静态图像。

然后周围的Googling一段时间后,我发现,AnimationDrawable不能onCreate 工作,我们可以把它放在onResumeonWindowsFocusChanged。所以在这里我试试这个:

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

最后它的工作,gif都显示和动画!

+0

噢,我的上帝有多久了我一直试图让这个该死的东西的工作...谢谢!!!!! – George 2011-12-16 17:43:33

+0

很高兴帮助! @George – richardlin 2012-01-01 12:16:27

+0

你能否提供完整的代码,我真的需要。 – TRonZ 2012-07-16 12:18:06

0

我认为你应该使用img.startxx(frameAnimation)