2015-11-21 11 views
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(); 
    } 
} 

回答

0

您應該在動畫運行時禁用所有按鈕,因此請在活動中創建對按鈕的引用,並在需要時將其禁用。

可以使用的方法,如該檢查當動畫結束,並重新啓用按鈕:

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

這兩個答案也可以幫助你:

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

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

+0

謝謝,它只是稍作修改! –

+0

很高興聽到這個,祝你好運:) –