0

这样做的目的是创建 SimonSays 游戏并执行生成和显示需要重复的模式背后的逻辑

sequenceArray 已经填充了从 0 到 3 的随机值,使用 Random 类和 for 循环,然后我想做的是根据 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);


        }
});
4

0 回答 0