如何在onTouch上启动animationdrawable?

时间:2015-06-09 07:13:00

标签: android android-animation ontouch animationdrawable

我有猫动画跳跃,它有5个drawables,我想触摸猫并让它跳跃。

如果安装了一些东西="假",在触摸猫后它会继续跳跃 永远。

<?xml version="1.0" encoding="utf-8"?>

<item android:drawable="@drawable/cat1" android:duration="250"></item>
<item android:drawable="@drawable/cat2" android:duration="250"></item>
<item android:drawable="@drawable/cat3" android:duration="250"></item>
<item android:drawable="@drawable/cat4" android:duration="250"></item>
<item android:drawable="@drawable/cat5" android:duration="250"></item>
<item android:drawable="@drawable/cat1" android:duration="250"></item>

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page5);
    cat_jump = (ImageView)findViewById(R.id.cat_jump);
    cat_jump.setBackgroundResource(R.drawable.cat_jumping_animation);

    final AnimationDrawable frameAnimation = (AnimationDrawable) cat_jump.getBackground();

    cat_jump.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            frameAnimation.start();
        }
    });

1 个答案:

答案 0 :(得分:1)

创建此oncreate

    ImageView iv = (ImageView)findViewById(R.id.cat_jump);
    AnimationDrawable aw = (AnimationDrawable) iv.getBackground();

    aw.stop();
    aw.start();

然后,

public void Start() {
    aw.stop();
    aw.start();
}

public void Stop() {
    aw.stop();

}