14.01.2013 Views

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

Android™ Application Development - Bahar Ali Khan

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 11: Advanced Android <strong>Development</strong><br />

364<br />

android:fromYScale=”1.0”<br />

android:toYScale=”0.0”<br />

android:pivotX=”50%”<br />

android:pivotY=”50%”<br />

android:startOffset=”500”<br />

android:duration=”500” /><br />

<br />

<br />

As you can see, it’s generally both easier and more intuitive to create your animation sequences using<br />

an external animation resource.<br />

Applying Tweened Animations<br />

Animations can be applied to any View by calling its startAnimation method and passing in the Animation<br />

or Animation Set to apply.<br />

Animation sequences will run once and then stop, unless you modify this behavior using the<br />

setRepeatMode and setRepeatCount methods on the Animation or Animation Set. You can force an<br />

animation to loop or ping-pong by setting the repeat mode of RESTART or REVERSE. Setting the repeat<br />

count controls the number of times the animation will repeat.<br />

The following code snippet shows an Animation that repeats indefi nitely:<br />

myAnimation.setRepeatMode(Animation.RESTART);<br />

myAnimation.setRepeatCount(Animation.INFINITE);<br />

myView.startAnimation(myAnimation);<br />

Using Animation Listeners<br />

The AnimationListener lets you create an event handler that’s fi red when an animation begins or<br />

ends. This lets you perform actions before or after an animation has completed, such as changing the<br />

View contents or chaining multiple animations.<br />

Call setAnimationListener on an Animation object, and pass in a new implementation of<br />

AnimationListener, overriding onAnimationEnd, onAnimationStart, and onAnimationRepeat as<br />

required.<br />

The following skeleton code shows the basic implementation of an Animation Listener:<br />

myAnimation.setAnimationListener(new AnimationListener() {<br />

public void onAnimationEnd(Animation _animation) {<br />

// TODO Do something after animation is complete.<br />

}<br />

public void onAnimationRepeat(Animation _animation) {

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!