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 />

370<br />

}<br />

if (super.onKeyDown(_keyCode, _event))<br />

return true;<br />

if (_event.getAction() == KeyEvent.ACTION_DOWN){<br />

switch (_keyCode) {<br />

case (KeyEvent.KEYCODE_DPAD_LEFT):<br />

movePosition(textPosition, TextPosition.Left); return true;<br />

case (KeyEvent.KEYCODE_DPAD_RIGHT):<br />

movePosition(textPosition, TextPosition.Right); return true;<br />

case (KeyEvent.KEYCODE_DPAD_UP):<br />

movePosition(textPosition, TextPosition.Top); return true;<br />

case (KeyEvent.KEYCODE_DPAD_DOWN):<br />

movePosition(textPosition, TextPosition.Bottom); return true;<br />

}<br />

}<br />

return false;<br />

Running the application now will show a screen displaying “Center”; pressing any of the four directions<br />

will slide out this text and display the appropriate new position.<br />

As an extra step, you could wire up the accelerometer sensor rather than relying on pressing the D-pad.<br />

Animating Layouts and View Groups<br />

A LayoutAnimation is used to animate View Groups, applying a single Animation (or Animation Set)<br />

to each child View in a predetermined sequence.<br />

Use a LayoutAnimationController to specify an Animation (or Animation Set) that’s applied to each<br />

child View in a View Group. Each View it contains will have the same animation applied, but you can<br />

use the Layout Animation Controller to specify the order and start time for each View.<br />

Android includes two LayoutAnimationController classes.<br />

❑ LayoutAnimationController Lets you select the start offset of each View (in milliseconds)<br />

and the order (forward, reverse, and random) to apply the animation to each child View.<br />

❑ GridLayoutAnimationController Is a derived class that lets you assign the animation<br />

sequence of the child Views using grid row and column references.<br />

Creating Layout Animations<br />

To create a new Layout Animation, start by defi ning the Animation to apply to each child view. Then<br />

create a new LayoutAnimation, either in code or as an external animation resource, that references the<br />

animation to apply and defi nes the order and timing in which to apply it.<br />

The following XML snippets show the defi nition of a simple animation stored as popin.xml in the res/<br />

anim folder, and a layout animation stored as popinlayout.xml.<br />

The Layout Animation applies a simple “pop-in” animation randomly to each child View of any View<br />

Group it’s assigned to.

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

Saved successfully!

Ooh no, something went wrong!