1

I have a simple multimedia application, covering 70% percent of the screen from top to bottom I have an ImageView widget, and on the other 30% of the screen I have 10 buttons, each button triggers an an animation inside the ImageView widget.

The problem is that if I click for example button 1 and then press button 2 right away, the animation from button 1 will be stopped and the animation from button 2 will start. How can I disable the 10 buttons while the current animation is playing? How could I enable the buttons after the animation is finished?

I tried putting the program to sleep with thread.sleep(animationlenght) and with TimeUnit.MILLISECONDS.sleep(animationlenght), but it doesn't prevent the program from running other onClick events.

package com.example.ticutu.croco;
import android.app.Activity;enter code here
import android.graphics.drawable.AnimationDrawable;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v7.app.ActionBarActivity;

import com.example.ticutu.juego.R;

import java.util.concurrent.TimeUnit;

public class Juego extends Activity
{
    private AnimationDrawable animacion;
    private MediaPlayer miPlayer;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_juego);
        ImageView video = (ImageView)findViewById(R.id.secuencia);
        video.setBackgroundResource(R.drawable.animation_drawable_start);
        animacion = (AnimationDrawable)video.getBackground();
        animacion.start();
        try
        {
            TimeUnit.MILLISECONDS.sleep(5000);//here is the error
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
    public void onClickButton2(View any)
    {
        ImageView video = (ImageView)findViewById(R.id.secuencia);
        video.setBackgroundResource(R.drawable.animation_drawable_boton_1);
        animacion = (AnimationDrawable)video.getBackground();
        miPlayer = MediaPlayer.create(Juego.this,R.raw.sonido_boton_1);
        animacion.start();
        miPlayer.start();
    }
    public void onClickButton3(View any)
    {
        ImageView video = (ImageView)findViewById(R.id.secuencia);
        video.setBackgroundResource(R.drawable.animation_drawable_boton_2);
        animacion = (AnimationDrawable)video.getBackground();
        miPlayer = MediaPlayer.create(Juego.this,R.raw.sonido_boton_2);
        animacion.start();
        miPlayer.start();
    }
    public void onClickButton4(View any)
    {
        ImageView video = (ImageView)findViewById(R.id.secuencia);
        video.setBackgroundResource(R.drawable.animation_drawable_boton_3);
        animacion = (AnimationDrawable)video.getBackground();
        miPlayer = MediaPlayer.create(Juego.this,R.raw.sonido_boton_3);
        animacion.start();
        miPlayer.start();
    }
    public void onClickButton5(View any)
    {
        ImageView video = (ImageView)findViewById(R.id.secuencia);
        video.setBackgroundResource(R.drawable.animation_drawable_boton_4);
        animacion = (AnimationDrawable)video.getBackground();
        animacion.start();
    }
    public void onClickButton6(View any)
    {
        ImageView video = (ImageView)findViewById(R.id.secuencia);
        video.setBackgroundResource(R.drawable.animation_drawable_boton_5);
        animacion = (AnimationDrawable)video.getBackground();
        miPlayer = MediaPlayer.create(Juego.this,R.raw.sonido_boton_5);
        animacion.start();
        miPlayer.start();
    }
    public void onClickButton7(View any)
    {
        ImageView video = (ImageView)findViewById(R.id.secuencia);
        video.setBackgroundResource(R.drawable.animation_drawable_boton_6);
        animacion = (AnimationDrawable)video.getBackground();
        miPlayer = MediaPlayer.create(Juego.this,R.raw.sonido_boton_6);
        animacion.start();
        miPlayer.start();
    }
    public void onClickButton8(View any)
    {
        ImageView video = (ImageView)findViewById(R.id.secuencia);
        video.setBackgroundResource(R.drawable.animation_drawable_boton_7);
        animacion = (AnimationDrawable)video.getBackground();
        miPlayer = MediaPlayer.create(Juego.this,R.raw.sonido_boton_7);
        animacion.start();
        miPlayer.start();
    }
    public void onClickButton9(View any)
    {
        ImageView video = (ImageView)findViewById(R.id.secuencia);
        video.setBackgroundResource(R.drawable.animation_drawable_boton_8);
        animacion = (AnimationDrawable)video.getBackground();
        miPlayer = MediaPlayer.create(Juego.this,R.raw.sonido_boton_8);
        animacion.start();
        miPlayer.start();
    }
    public void onClickButton10(View any)
    {
        ImageView video = (ImageView)findViewById(R.id.secuencia);
        video.setBackgroundResource(R.drawable.animation_drawable_boton_9);
        animacion = (AnimationDrawable)video.getBackground();
        miPlayer = MediaPlayer.create(Juego.this,R.raw.sonido_boton_9);
        animacion.start();
        miPlayer.start();
    }
    public void onClickButton11(View any)
    {
        ImageView video = (ImageView)findViewById(R.id.secuencia);
        video.setBackgroundResource(R.drawable.animation_drawable_boton_10);
        animacion = (AnimationDrawable)video.getBackground();
        miPlayer = MediaPlayer.create(Juego.this,R.raw.sonido_boton_10);
        animacion.start();
        miPlayer.start();
    }
}
Puértolas Luis
  • 235
  • 1
  • 2
  • 13

1 Answers1

0

You should disable all buttons while animation is running, so create a reference to your buttons in the Activity and disable them when needed.

You can use a method such as this to check when the animation is over, and re-enable the buttons:

 private void checkIfAnimationDone(AnimationDrawable anim){
    final AnimationDrawable a = anim;
    int timeBetweenChecks = 300;
    Handler h = new Handler();
    h.postDelayed(new Runnable(){
        public void run(){
            if (a.getCurrent() != a.getFrame(a.getNumberOfFrames() - 1)){
                checkIfAnimationDone(a);
            } else{
                //here re-enable your buttons
            }
        }
    }, timeBetweenChecks);
};

These 2 answers might also help you:

https://stackoverflow.com/a/6641321/500105

https://stackoverflow.com/a/15856260/500105

Community
  • 1
  • 1
SuperFrog
  • 7,631
  • 9
  • 51
  • 81