Setting up an AnimationListener for an ObjectAnimator

Tags: ,



In my program, I have an ObjectAnimator which moves an ImageView from left to right. I am trying to set up a listener which will execute a task when the ObjectAnimator is finished running. Here is the relevant section of code which I am currently using to try to accomplish this:

  if (num == 350) {
        nAnim = ObjectAnimator.ofFloat(gamePiece, "translationX", 0, num);
        nAnim.setDuration(2125);
        nAnim.start();
        nAnim.addListener(new AnimationListener() {
            @Override
            public void onAnimationEnd(Animator a) {
                startGame(level);
            }

            @Override
            public void onAnimationStart(Animator a) {

            }

            @Override
            public void onAnimationCancel(Animator a) {

            }

            @Override
            public void onAnimationRepeat(Animator a) {

            }

        });

When I try to run this in Android Studio, I am getting the error: MainActivity is not abstract and does not override abstract method onAnimationStart() in MainActivity. What do I have to do to fix this error?

Answer

Since you implemented AnimatorListener in your MainActivity, you must include all its abstract methods, and change nAnim.addListener(new Animat.... to nAnim.addListener(this)

@Override
public void onAnimationStart(Animator animation){
}

@Override
public void onAnimationEnd(Animator animation){
    startGame(level)
}

@Override
public void onAnimationRepeat(Animator animation){
}

@Override
public void onAnimationCancel(Animator animation){
}


Source: stackoverflow