I created a color background animation that starts on button click (it smoothly changes the background color over and over again). My problem is that I can't stop the animation (if I click the button again it increases the animation speed). I already tried to create a while-loop but then the animation did't work anymore. My goal is that the animation starts if I press the image Btn, and if I press it again it should stop.
Code:
imageButton_info.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
// Handler and runnable to run the animation in devices sdk below honeycomb.
final Handler handler = new Handler();
Runnable ChangeBackgroundRunnable = new Runnable() {
@Override
public void run() {
number++;
float[] hsvColor = {0, 1, 1};
hsvColor[0] = 360f * number / 100;
color.setBackgroundColor(Color.HSVToColor(hsvColor));
handler.postDelayed(this, 80);
if (number >=100)
number = 1;
}
};
number = 0;
handler.removeCallbacks(ChangeBackgroundRunnable);
handler.postDelayed(ChangeBackgroundRunnable, 0);
}
});