2010-10-12 94 views
1

我正在使用框架动画来显示一些图像。但它只能在按钮操作中使用。我想在程序启动时调用这个函数。如何通过一个按钮来实现这一点?Android中的框架动画?

我用下面的代码为动画:

public class FrameAnimationActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState){ 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     this.setupButton(); 
    } 
    private void setupButton(){ 
     Button b = (Button)this.findViewById(R.id.startFAButtonId); 
     b.setOnClickListener(
      new Button.OnClickListener(){ 
      public void onClick(View v){ 
       parentButtonClicked(v); 
      } 
      }); 
    } 
    private void parentButtonClicked(View v){ 
     animate(); 
    } 
    private void animate(){ 
     ImageView imgView = (ImageView)findViewById(R.id.imageView); 
     imgView.setVisibility(ImageView.VISIBLE); 
     imgView.setBackgroundResource(R.drawable.frame_animation); 
     AnimationDrawable frameAnimation = (AnimationDrawable) imgView.getBackground(); 
     if (frameAnimation.isRunning()){ 
      frameAnimation.stop(); 
     } 
     else{ 
      frameAnimation.stop(); 
      frameAnimation.start(); 
     } 
    } 
} 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> 
    <item android:drawable="@drawable/colored-ball1" android:duration="50" /> 
    <item android:drawable="@drawable/colored-ball2" android:duration="50" /> 
    <item android:drawable="@drawable/colored-ball3" android:duration="50" /> 
    <item android:drawable="@drawable/colored-ball4" android:duration="50" /> 
    <item android:drawable="@drawable/colored-ball5" android:duration="50" /> 
    <item android:drawable="@drawable/colored-ball6" android:duration="50" /> 
    <item android:drawable="@drawable/colored-ball7" android:duration="50" /> 
    <item android:drawable="@drawable/colored-ball8" android:duration="50" /> 
</animation-list> 
+0

也许你应该查看一下API演示,它来自你的SDK的例子。当我是一个noob时,对我来说是一个很大的帮助,甚至现在.. – Shouvik 2010-10-12 04:47:59

+0

@ Shouvik:我从API演示中获得了这些代码,但是它没有在按钮操作 – Jeff 2010-10-12 04:58:59

回答

4

在Android的文档,你可以发现如下:

“要注意的是不能将活动的onCreate()方法中调用呼吁AnimationDrawable的start()方法是很重要的,因为AnimationDrawable尚未完全附加到窗口上如果您想要立即播放动画而不需要交互,那么您可能需要从Activity中的onWindowFocusChanged()方法中调用该方法,这会在Android带来窗口时调用它聚焦“。

希望有帮助!

3

当您点击此按钮,出现这种情况的唯一的事情就是animate()方法被调用。您是否尝试过将

animate(); 

onCreate()内,并从parentButtonClicked(View v)删除它,使其调用动画创建活动时,而不是当按下按钮?不明白为什么这不适合你。

然后,你也会有一个无用的按钮。 :)