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

380<br />

Anti-aliasing is particularly important when drawing text, as anti-aliased text can be signifi cantly easier<br />

to read. To create even smoother text effects, you can apply the SUBPIXEL_TEXT_FLAG, which will<br />

apply subpixel anti-aliasing.<br />

You can also set both of these fl ags manually using the setSubpixelText and setAnti<strong>Ali</strong>as methods,<br />

as shown below:<br />

myPaint.setSubpixelText(true);<br />

myPaint.setAnti<strong>Ali</strong>as(true);<br />

Hardware Acceleration for 2D Graphics<br />

In a boon for 2D graphics enthusiasts everywhere, Android lets you request that your application<br />

always be rendered using hardware acceleration.<br />

If hardware acceleration is available on the device, setting this fl ag will cause every View within the<br />

Activity to be rendered using hardware. This has the side effect of dramatically improving the speed of<br />

your graphics while reducing the load on the system processor.<br />

Turn it on by applying the Window.FEATURE_OPENGL fl ag to your Activity using the<br />

requestWindowFeature method, as shown below:<br />

myActivity.requestWindowFeature(Window.FEATURE_OPENGL);<br />

Unfortunately, Good Things seldom come for free, and this is no exception.<br />

Not all the 2D drawing primitives available in Android are supported by hardware (notably most of the<br />

Path Effects described previously).<br />

Also, as your entire Activity is being effectively rendered as a single Canvas, invalidate requests on any<br />

View will cause the whole Activity to be redrawn.<br />

Canvas Drawing Best Practice<br />

2D owner-draw operations tend to be expensive in terms of processor use; ineffi cient drawing routines<br />

can block the GUI thread and have a detrimental effect on application responsiveness. This is particularly<br />

true in a resource-constrained environment with a single, limited processor.<br />

You need to be aware of the resource drain and CPU-cycle cost of your onDraw methods, to ensure you<br />

don’t end up with an attractive application that’s completely unresponsive.<br />

A lot of techniques exist to help minimize the resource drain associated with owner-drawn controls.<br />

Rather than focus on general principles, I’ll describe some Android specifi c considerations for ensuring<br />

that you can create activities that look good and remain interactive (note that this list is not exhaustive):<br />

Consider Hardware Acceleration<br />

❑ OpenGL hardware acceleration support for 2D graphics is<br />

a Good Thing, so you should always consider if it’s suitable for your Activity. Good candidates<br />

are Activities consisting of a single View with rapid, time-consuming updates. Be sure that the<br />

primitives you use are supported by hardware.

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

Saved successfully!

Ooh no, something went wrong!