通过按下并抬起按钮开始和停止动画,但动画在两者之间的任何帧停止

时间:2015-02-07 10:42:59

标签: java android eclipse animation

通过按下并抬起按钮来启动和停止动画,但动画会在两者之间的任何帧停止。我希望动画能够在第一张图像上精确停止并从第一张图像开始。

以下是我的代码:

  public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            ImageView rocketImage = (ImageView) findViewById(R.id.imageView1);
            rocketImage.setBackgroundResource(R.drawable.ball_animation);
            rocketAnimation = (AnimationDrawable) rocketImage.getBackground();

             mp = MediaPlayer.create(this, R.raw.sound);

        }

        public boolean onTouchEvent(MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                if (rocketAnimation.isRunning()){
                    rocketAnimation.stop();
                }
                rocketAnimation.start();
                return false;
            }
            if (event.getAction() == MotionEvent.ACTION_UP) {
                if(rocketAnimation.isRunning()){
                  rocketAnimation.stop();

                }
              }
            return super.onTouchEvent(event);
        }

    }


<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
    <item android:drawable="@drawable/image1" android:duration="40" />
    <item android:drawable="@drawable/image2" android:duration="40" />
    <item android:drawable="@drawable/image3" android:duration="40" />
    <item android:drawable="@drawable/image4" android:duration="40" />

</animation-list>

1 个答案:

答案 0 :(得分:0)

MotionEvent.ACTION_UP中,将rocketAnimation.stop()替换为rocketAnimation.setOneShot(true)