Jon Doe
Jon Doe

Reputation: 25

Running animation after animation, android studio

I am new at android programming and I have a problem to run animation after animation in android studio. The thing is that I don't have specified number of animations I need, it depends on counter.

package com.example.smartpc.memorygame;

import android.graphics.drawable.TransitionDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.Random;

public class MemoryGame extends AppCompatActivity {

    ImageView imgRed;
    ImageView imgGreen;
    ImageView imgBlue;
    Button btnStart;

    ArrayList<Integer>array=new ArrayList<>();
    int counter=3;
    int i=0;
    Boolean flag=false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_memory_game);

        imgRed=(ImageView)findViewById(R.id.imgRed);
        imgGreen=(ImageView)findViewById(R.id.imgGreen);
        imgBlue=(ImageView)findViewById(R.id.imgBlue);
        btnStart=(Button)findViewById(R.id.btnStart);

        final RotateAnimation animation=new RotateAnimation(0f,360f, RotateAnimation.RELATIVE_TO_SELF,0.5f,
                                                            RotateAnimation.RELATIVE_TO_SELF,0.5f);
        animation.setDuration(1000);
//        animation.setRepeatCount(1);

        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                ;
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                if (flag==true) {
                    i++;
                    Random rand = new Random();
                    int number = Math.abs(rand.nextInt() % 3);
                    btnStart.setText(Integer.toString(i) + " " + Integer.toString(number));
                    if (number == 0) {
                        imgRed.startAnimation(animation);
                    } else if (number == 1) {
                        imgGreen.startAnimation(animation);
                    } else if (number==2){
                        imgBlue.startAnimation(animation);
                    }
                    if (i == counter - 1) {
                        i = 0;
                        flag=false;
                        return;
                    }
                }
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                ;
            }
        });

        imgRed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgRed.startAnimation(animation);
                flag=false;
            }
        });

        imgGreen.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgGreen.startAnimation(animation);
                flag=false;
            }
        });

        imgBlue.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imgBlue.startAnimation(animation);
                flag=false;
            }
        });

        btnStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//              imgGreen.setImageResource(R.drawable.transition);
//              ((TransitionDrawable) imgGreen.getDrawable()).startTransition(3000);
                flag=true;
                Random rand = new Random();
                int number = Math.abs(rand.nextInt() % 3);

                if (number == 0) {
                    imgRed.startAnimation(animation);
                }
                else if (number == 1) {
                    imgGreen.startAnimation(animation);
                }
                else if (number==2) {
                    imgBlue.startAnimation(animation);
                }
//                btnStart.setText(Integer.toString(i) + " " + Integer.toString(number));
            }
        });
    }
}

In method onAnimationEnd() is problem. It generates number and starts good animaton, but it also starts another one (random, there's no rule which one). Does anyone have idea how to solve this? Also, counter is not fixed, I want to change it in code later.

Upvotes: 1

Views: 1480

Answers (1)

Jason
Jason

Reputation: 106

I will try my best to explain it.

When the process run into onAnimationEnd(), it does mean the animation is finished. It just like to wait for the onAnimationEnd(). And then you call startAnimation(sameAnimation), we can see the method source code.

/**
 * Start the specified animation now.
 *
 * @param animation the animation to start now
 */
public void startAnimation(Animation animation) {
    animation.setStartTime(Animation.START_ON_FIRST_FRAME);
    setAnimation(animation);
    invalidateParentCaches();
    invalidate(true);
}

Notice this code : animation.setStartTime(Animation.START_ON_FIRST_FRAME);

If you set the start time with -1, the animation we have run previously will be reset. Oops, because we said the animation is not really finished, it will rerun as the last time.

Run your progress, if you see there are two images rotating at the same time, one of them must be the last one which has rotated.

If you want to run animation after animation, you may not use the same animation in onAnimationEnd(). Or you can use View.clearAnimation() to clear all animation which attach to the view.

I'm glad if it could help you. And if there is anything wrong, please add a comment.

Upvotes: 1

Related Questions