防止onClick方法运行一段时间

时间:2015-11-21 23:47:42

标签: android

我有一个简单的多媒体应用程序,从上到下覆盖70%的屏幕我有一个ImageView小部件,另外30%的屏幕我有10个按钮,每个按钮在ImageView内触发一个动画小部件。

问题在于,如果我点击例如按钮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();
    }
}

1 个答案:

答案 0 :(得分: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);
};

这2个答案也可能对您有所帮助:

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

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