O'Reilly logo

Android 3.0 Animations by Alex Shaw

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Time for action—animating the rolling ball

The children's book company has got back to us and they're not happy with the first page of our interactive book. It says that the ball is rolling, but it isn't!

We're going to fix this by using an ObjectAnimator to move the ball backwards and forwards across the screen.

  1. Open up InteractiveBook.java and add a new import declaration:
    import android.animation.ObjectAnimator;
    

    This should come as no surprise! I already told you that we would be using the ObjectAnimator class.

  2. Next, go to the end of the onCreate() method, and add the following lines:
    View rollingBall = findViewById(R.id.rollingball);
    ObjectAnimator ballRoller = ObjectAnimator.ofFloat(
    rollingBall,
    "TranslationX",
    0,
    400
    );
    
  3. Underneath the code ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required