■実現したいこと
Android Studioを使用して、Androidアプリを作成しています。
下記の動きをするサイコロアプリを作成しています。
・ボタンを押すと、サイコロを振る音が鳴り(1秒)、
その間サイコロの目が(一~六で)ぐるぐると回る。
・ボタンが押されてから1秒後、ランダムなサイコロの目が表示される。
・ボタンの目が1,6のときメッセージを表示する。
■問題
ボタンが押されてから1秒後に処理を行う際、HandlerとRunnableを使っているのですが、
ボタンを連打際に以下の動きになってしまいます。
・ボタン連打している間、途中で何度もメッセージが表示されてしまう。
(メッセージはボタンが最後に押されてから、ボタンの目が確定したときにはじめて表示したい)
■解決策・実現したいこと
・ボタンが連打されたとき、最後に押された時から1秒後にHandlerの処理を実行したい。
または
・ボタンが連打されないようにしたい。(Handlerの処理が終るまでボタンを無効にする)
該当のソースコード
Java
1public class MainActivity extends AppCompatActivity { 2 private AnimationDrawable animation; 3 private SoundPool soundPool; 4 private int sound; 5 TextView textView; 6 int delay = 0; 7 8 9 @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP) 10 protected void onCreate(Bundle savedInstanceState){ 11 super.onCreate(savedInstanceState); 12 setContentView(R.layout.activity_main); 13 14 textView = findViewById(R.id.text_view); 15 textView.setText(""); 16 17 //--------------------------------------------------------------------------------- 18 AudioAttributes audioAttributes = new AudioAttributes.Builder() 19 // USAGE_MEDIA 20 // USAGE_GAME 21 .setUsage(AudioAttributes.USAGE_GAME) 22 // CONTENT_TYPE_MUSIC 23 // CONTENT_TYPE_SPEECH, etc. 24 .setContentType(AudioAttributes.CONTENT_TYPE_SPEECH) 25 .build(); 26 27 soundPool = new SoundPool.Builder() 28 .setAudioAttributes(audioAttributes) 29 // ストリーム数に応じて 30 .setMaxStreams(2) 31 .build(); 32 33 // one.wav をロードしておく 34 sound = soundPool.load(this, R.raw.sound, 1); 35 36 37 // load が終わったか確認する場合 38 soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { 39 @Override 40 public void onLoadComplete(SoundPool soundPool, int sampleId, int status) { 41 Log.d("debug","sampleId="+sampleId); 42 Log.d("debug","status="+status); 43 } 44 }); 45 //--------------------------------------------------------------------------------- 46 47 48 ImageView imgView = findViewById(R.id.image_view); 49 imgView.setBackgroundResource(R.drawable.animation_list); 50 animation = (AnimationDrawable)imgView.getBackground(); 51 52 53 Button button = findViewById(R.id.button); 54 button.setOnClickListener(new View.OnClickListener(){ 55 @RequiresApi(api = Build.VERSION_CODES.M) 56 public void onClick(View view) { 57 58 delay = 1000; 59 60 // one.wav の再生 61 // play(ロードしたID, 左音量, 右音量, 優先度, ループ,再生速度) 62 soundPool.play(sound, 1.0f, 1.0f, 0, 0, 1); 63 64 ImageView imgView = findViewById(R.id.image_view); 65 imgView.setBackgroundResource(R.drawable.animation_list); 66 animation = (AnimationDrawable) imgView.getBackground(); 67 68 if (animation.isRunning()) { 69 // animation.stop(); 70 } else { 71 animation.start(); 72 } 73 74 new Handler().postDelayed(new Runnable() { 75 @Override 76 public void run() { 77 // 遅延後に行う処理 78 if (animation.isRunning()) { 79 animation.stop(); 80 diceView(); 81 } 82 } 83 }, delay); 84 } 85 }); 86 } 87 88 private void diceView(){ 89 Random random = new Random(); 90 int dice = random.nextInt(6); 91 int diceNum = 2; 92 ImageView imgView = findViewById(R.id.image_view); 93 94 switch(dice){ 95 case 0: 96 imgView.setBackgroundResource(R.drawable.dice2_1); 97 diceNum = 0; 98 break; 99 case 1: 100 imgView.setBackgroundResource(R.drawable.dice2_2); 101 break; 102 case 2: 103 imgView.setBackgroundResource(R.drawable.dice2_3); 104 break; 105 case 3: 106 imgView.setBackgroundResource(R.drawable.dice2_4); 107 break; 108 case 4: 109 imgView.setBackgroundResource(R.drawable.dice2_5); 110 break; 111 case 5: 112 default: 113 imgView.setBackgroundResource(R.drawable.dice2_6); 114 diceNum = 1; 115 break; 116 } 117 diceCheck(diceNum); 118 119 } 120 121 private void diceCheck(int diceNum){ 122 // テキストを設定して表示 123 if(diceNum == 0){ 124 textView.setTextColor(Color.YELLOW); 125 textView.setBackgroundColor(Color.RED); 126 textView.setText(R.string.hit); 127 }else if(diceNum == 1){ 128 textView.setTextColor(Color.YELLOW); 129 textView.setBackgroundColor(Color.BLUE); 130 textView.setText(R.string.miss); 131 }else{ 132 textView.setTextColor(Color.parseColor("#00000000")); 133 textView.setBackgroundColor(Color.parseColor("#00000000")); 134 textView.setText(""); 135 } 136 } 137 138}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/03/18 05:04
2019/03/18 05:29
2019/03/18 05:35