Android中点击按钮时的帧动画

Android中点击按钮时的帧动画,android,Android,我的问题是我有一些图像&我使用帧动画在单击按钮事件时显示这些图像,但如果我第一次单击按钮,图像将按顺序显示&如果我在另一次单击该按钮,则图像不会显示。下面是我的代码 Animation.java文件:- public class Animation extends Activity { Button mBtnOK; AnimationDrawable frameAnimation; ImageView imgView; /** Called when the

我的问题是我有一些图像&我使用帧动画在单击按钮事件时显示这些图像,但如果我第一次单击按钮,图像将按顺序显示&如果我在另一次单击该按钮,则图像不会显示。下面是我的代码

Animation.java文件:-

public class Animation extends Activity {

    Button mBtnOK;
    AnimationDrawable frameAnimation;
    ImageView imgView;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mBtnOK = (Button) findViewById(R.id.mBtnOK);
        mBtnOK.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                animate();
            }
        });

    }

    private void animate() {
        imgView = (ImageView) findViewById(R.id.simple_anim);
        imgView.setVisibility(ImageView.VISIBLE);
        imgView.setBackgroundResource(R.anim.simple_animation);
        AnimationDrawable frameAnimation = (AnimationDrawable) imgView
                .getBackground();
        frameAnimation.start();
        frameAnimation.setOneShot(true);
    }
}
动画文件:-

<?xml version="1.0" encoding="utf-8"?>
 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" id="selected" android:oneshot="false">
  <item android:drawable="@drawable/monkey_1" android:duration="50" />
  <item android:drawable="@drawable/monkey_2" android:duration="50" />
  <item android:drawable="@drawable/monkey_3" android:duration="50" />
  <item android:drawable="@drawable/monkey_4" android:duration="50" />
  <item android:drawable="@drawable/monkey_5" android:duration="50" />
  <item android:drawable="@drawable/monkey_6" android:duration="50" />
  <item android:drawable="@drawable/monkey_7" android:duration="50" />
  <item android:drawable="@drawable/monkey_8" android:duration="50" />
  <item android:drawable="@drawable/monkey_9" android:duration="50" />
  <item android:drawable="@drawable/monkey_10" android:duration="50" />
 </animation-list>

@Dipak

我用和你一样的方法制作了动画。尝试添加此代码,希望您的错误得到解决。还有一件事是使用线程来运行动画。这肯定会运行得很好

if(frameAnimation.isRunning()) {
    frameAnimation.stop();
    frameAnimation.start();
}

重新启动帧动画的唯一方法是使用
setVisible()
,它包含一个标志,用于强制动画重置为第一帧。如果这样修改代码的动画部分: AnimationDrawable frameAnimation=(AnimationDrawable)imgView.getBackground(); frameAnimation.setOneShot(true); frameAnimation.setVisible(true,true); frameAnimation.start()

动画应始终从第一帧开始,并在每次单击按钮时一直运行到完成。动画也可以通过切换绘图表本身的可见性而不是包含它的
ImageView
来重置

HTH

frameAnimation.setVisible(true,true);frameAnimation.start();运行此操作将导致帧动画运行两次。您只需要setVisible(true,true):第一个true设置可见,第二个true重新启动动画,就像第一次运行一样。