1
我有一個簡單的多媒體應用程序,覆蓋從上到下的屏幕70%的百分比我有一個ImageView小部件,並在另一個30%的屏幕上我有10個按鈕,每個按鈕都會在ImageView小部件內部觸發一個動畫。防止onClick方法運行一段時間
問題是,如果我點擊例如按鈕1,然後立即按下按鈕2,則按鈕1的動畫將停止,並且按鈕2的動畫將開始。如何在當前動畫播放時禁用10個按鈕?動畫完成後,我怎樣才能啓用按鈕?
我試着讓程序與thread.sleep(animationlenght)和TimeUnit.MILLISECONDS.sleep(animationlenght)一起睡,但它並不妨礙程序運行其他onClick事件。
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();
}
}
謝謝,它只是稍作修改! –
很高興聽到這個,祝你好運:) –