2011-11-30 107 views
1

我想在应用程序启动后不久运行我的android动画。
我在thunder.xml中使用动画列表框架动画。 我的代码如下。在onCreate方法上运行android动画

ImageView img = (ImageView) findViewById(R.id.animlist);  
img.setBackgroundResource(R.drawable.thunder); 
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground(); 
frameAnimation.start(); 

但是,这是运行,但没有显示动画。 如果我创建一个按钮并将其设置为该按钮的onClick事件,那么它的工作正常。 但我想在应用程序启动后立即运行我的android动画。 请帮我.. 在此先感谢

回答

0

尝试在onStart()方法中移动您的代码。我猜你的动画正在运行,但直到活动开始,你的动画动画才结束。

+0

我搬到我的代码在onStart()方法,还是一样problem.Can ANY1帮助。有没有其他的方式来实现这个? – yshak

+0

@yshak您已接受此答案,并评论说“仍然是同样的问题”。它是否解决问题? – rds

0

onWindowFocusChanged()是在帧动画上调出start()的正确方法。

1

试试这个代码,它可以帮助你 -

@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    animation = (ImageView)findViewById(R.id.imageAnimation); 
    animation.setBackgroundResource(R.drawable.animation);  
} 

public void onWindowFocusChanged (boolean hasFocus) 
{ 
    super.onWindowFocusChanged(hasFocus); 
    AnimationDrawable frameAnimation = 
     (AnimationDrawable) animation.getBackground(); 
    if(hasFocus) { 
     frameAnimation.start(); 
    } else { 
     frameAnimation.stop(); 
    } 
} 
相关问题