How to Stop animation when button is unpressed



I want two things to happen when button is pressed 1. Two images should play over and over again, giving it an animated and un static look. 2. A sound in a loop.


And when the button is unpressed and the finger goes up 1. The sound should stop 2. The animation should stop.


The method i am using right now is shown in the code:



package com.example.newanimation;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MainActivity extends Activity {

AnimationDrawable rocketAnimation;

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();
}

public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
rocketAnimation.start();
return true;
}
return super.onTouchEvent(event);
}

}


And the XML is like



<animation-list xmlns:android="http://ift.tt/nIICcg"
android:oneshot="false">
<item android:drawable="@drawable/ball01" android:duration="200" />
<item android:drawable="@drawable/ball02" android:duration="200" />
<item android:drawable="@drawable/ball03" android:duration="200" />
</animation-list>


I am keeping android:oneshot="false"> rather than "true" because i want the animation to play back and forth without stopping till the button is pressed, but i need it to stop when the finger is up from the button.


No comments:

Post a Comment