这样做的目的是创建SimonSays游戏并执行生成和显示需要重复的模式的逻辑
sequenceArray已经填充了从0到3的随机值,使用Random类和for循环,然后根据sequenceArray数组的每个元素的情况读取我想要做的事情,我想要更改4个视图中的一个以动画持续200毫秒的方式,它应该给最终用户按下按钮的感觉,或者在这种情况下,按钮闪烁。我是这样做的:
cats
我尝试使用OnClickListener显示相同的代码,但是由于某些原因,当我运行上面的代码时,没有任何反应,也没有动画更改。
之后我测试了sequenceArray,它不是空的,但由于某种原因,超出我的理解,当前的代码不会做我想要的。下面显示的for循环与上面的逻辑之间的主要区别是什么?
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;
}