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

❑ Consider Size and Orientation When you’re designing your Views and Overlays, be sure to<br />

consider (and test!) how they will look at different resolutions and sizes.<br />

❑ Create Static Objects Once Object creation in Android is particularly expensive. Where possible,<br />

create drawing objects like Paint objects, Paths, and Shaders once, rather than recreating<br />

them each time the View is invalidated.<br />

❑ Remember onDraw Is Expensive Performing the onDraw method is an expensive process<br />

that forces Android to perform several image composition and bitmap construction operations.<br />

Many of the following points suggest ways to modify the appearance of your Canvas without<br />

having to redraw it:<br />

❑ Use Canvas Transforms Use canvas transforms like rotate and translate to simplify<br />

complex relational positioning of elements on your canvas. For example, rather<br />

than positioning and rotating each text element around a clock face, simply rotate the<br />

canvas 22.5 degrees, and draw the text in the same place.<br />

❑ Use Animations Consider using Animations to perform pre-set transformations of<br />

your View rather than manually redrawing it. Scale, rotation, and translation Animations<br />

can be performed on any View within an Activity and provide a resource-effi cient<br />

way to provide zoom, rotate, or shake effects.<br />

❑ Consider Using Bitmaps and 9 Patches If your Views feature static backgrounds, you<br />

should consider using a Drawable like a bitmap or scalable 9 patch rather than manually<br />

drawing it.<br />

Advanced Compass Face Example<br />

Early in Chapter 4, you created a simple compass. In the last chapter, you returned to it, extending it to<br />

display the pitch and roll using the accelerometer hardware.<br />

The UI of the View used in those examples was kept simple to keep the code in those chapters as clear<br />

as possible.<br />

In the following example, you’ll make some signifi cant changes to the CompassView’s onDraw method<br />

to change it from a simple, fl at compass into a dynamic artifi cial horizon, as shown in Figure 11-2.<br />

Figure 11-2<br />

381

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

Saved successfully!

Ooh no, something went wrong!