TransitionDrawable animation in a switch/case statement nested in for loop

179 Views Asked by At

The purpose of this is to create SimonSays game and do the logic behind generating and displaying a pattern that needs to be repeated

sequenceArray is already populated with random values from 0 to 3, using Random class and for loop, then what I want to do is read depending on the case of each element of the sequenceArray array, I want to change one of 4 views in a way that the animation lasts 200 ms and it should give the end user a feeling of pressing a button, or in this case, a button flashing. Here's how I did this:

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;

}

I've tried the same code with OnClickListener shown bellow and it works, but for some reason, when I run the code above, nothing happens, no animation change takes place.

I've tested the sequenceArray afterwards, it isn't empty, but for some reason beyond my comprehension the current code just won't do what I want it to. What's the main difference between the for loop shown below with the logic above?

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);


        }
});
0

There are 0 best solutions below