これの目的は、SimonSays ゲームを作成し、繰り返す必要があるパターンの生成と表示の背後にあるロジックを実行することです。
sequenceArray には、Random クラスと for ループを使用して、0 から 3 までのランダムな値が既に取り込まれています。その後、sequenceArray 配列の各要素のケースに応じて、4 つのビューのいずれかを変更したいと考えています。アニメーションは 200 ミリ秒続き、エンド ユーザーにボタンを押した感覚 (この場合はボタンの点滅) を与える必要があります。これが私がこれをした方法です:
for(int j = 0; j < sequenceArray.length; j++){
switch(sequenceArray[j]){
case 0:
Drawable backgrounds_yellow[] = new Drawable[2];
Resources res_yellow = getResources();
backgrounds_yellow[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow);
backgrounds_yellow[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow_blink);
TransitionDrawable crossfader_yellow = new TransitionDrawable(backgrounds_yellow);
ImageView ivYellow = (ImageView) findViewById(R.id.iv1);
ivYellow.setImageDrawable(crossfader_yellow);
crossfader_yellow.startTransition(0);
crossfader_yellow.reverseTransition(200);
case 1:
Drawable backgrounds_red[] = new Drawable[2];
Resources res_red = getResources();
backgrounds_red[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_red);
backgrounds_red[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_red_blink);
TransitionDrawable crossfader_red = new TransitionDrawable(backgrounds_red);
ImageView ivRed = (ImageView) findViewById(R.id.iv2);
ivRed.setImageDrawable(crossfader_red);
crossfader_red.startTransition(0);
crossfader_red.reverseTransition(200);
case 2:
Drawable backgrounds_blue[] = new Drawable[2];
Resources res_ = getResources();
backgrounds_blue[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_blue);
backgrounds_blue[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_blue_blink);
TransitionDrawable crossfader_blue = new TransitionDrawable(backgrounds_blue);
ImageView ivBlue = (ImageView) findViewById(R.id.iv3);
ivBlue.setImageDrawable(crossfader_blue);
crossfader_blue.startTransition(0);
crossfader_blue.reverseTransition(200);
case 3:
Drawable backgrounds_green[] = new Drawable[2];
Resources res_green = getResources();
backgrounds_green[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_green);
backgrounds_green[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_green_blink);
TransitionDrawable crossfader_green = new TransitionDrawable(backgrounds_green);
ImageView ivGreen = (ImageView) findViewById(R.id.iv4);
ivGreen.setImageDrawable(crossfader_green);
crossfader_green.startTransition(0);
crossfader_green.reverseTransition(200);
default:
return;
}
以下に示す OnClickListener で同じコードを試してみましたが、うまくいきましたが、何らかの理由で、上記のコードを実行しても何も起こらず、アニメーションの変更も行われません。
その後、sequenceArray をテストしましたが、空ではありませんが、私の理解を超えた何らかの理由で、現在のコードは私が望んでいることを実行しません。以下に示す for ループと上記のロジックの主な違いは何ですか?
iv1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Drawable backgrounds[] = new Drawable[2];
Resources res = getResources();
backgrounds[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow);
backgrounds[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow_blink);
TransitionDrawable crossfader = new TransitionDrawable(backgrounds);
ImageView image = (ImageView) findViewById(R.id.iv1);
image.setImageDrawable(crossfader);
crossfader.startTransition(0);
crossfader.reverseTransition(200);
}
});