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