Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up an AnimationListener for an ObjectAnimator

Tags:

java

android

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?

like image 533
TeamLiquid1210 Avatar asked Jul 14 '16 15:07

TeamLiquid1210


People also ask

How do I create an animation listener?

AnimationListener() { @Override public void onAnimationStart(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationRepeat(Animation animation) { // TODO Auto-generated method stub } @Override public void onAnimationEnd(Animation animation) { // We start the Activity } }); } });

What are the two different types of view animations?

There are two types of animations that you can do with the view animation framework: Tween animation: Creates an animation by performing a series of transformations on a single image with an Animation. Frame animation: or creates an animation by showing a sequence of images in order with an AnimationDrawable .

How do I animate a view in Android?

You can use the view animation system to perform tweened animation on Views. Tween animation calculates the animation with information such as the start point, end point, size, rotation, and other common aspects of an animation.


1 Answers

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){
}
like image 196
Bill Avatar answered Sep 20 '22 10:09

Bill