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

❑ DiscretePathEffect Similar to the DashPathEffect, but with added randomness. Specifi<br />

es the length of each segment and a degree of deviation from the original path to use when<br />

drawing it.<br />

❑ PathDashPathEffect This effect lets you defi ne a new shape (path) to use as a stamp to outline<br />

the original path.<br />

The following effects let you combine multiple Path Effects to a single Paint.<br />

❑ SumPathEffect Adds two effects to a path in sequence, such that each effect is applied to the<br />

original path and the two results are combined.<br />

❑ ComposePathEffect Compose applies fi rst one effect and then applies the second effect to<br />

the result of the fi rst.<br />

Path Effects that modify the shape of the object being drawn will change the area of the affected shape.<br />

This ensures that any fi ll effects being applied to the same shape are drawn within the new bounds.<br />

Path Effects are applied to Paint objects using the setPathEffect method as shown below:<br />

borderPaint.setPathEffect(new CornerPathEffect(5));<br />

The Path Effects API sample gives an excellent guide to how to apply each of these effects.<br />

Changing the Xfermode<br />

Change a Paint’s Xfermode to affect the way it paints new colors on top of what’s already on the Canvas.<br />

Under normal circumstances, painting on top of an existing drawing will layer the new shape on top. If<br />

the new Paint is fully opaque, it will totally obscure the paint underneath; if it’s partially transparent, it<br />

will tint the colors underneath.<br />

The following Xfermode subclasses let you change this behavior:<br />

❑ AvoidXfermode Specifi es a color and tolerance to force your Paint to avoid drawing over (or<br />

only draw over) it.<br />

❑ PixelXorXfermode Applies a simple pixel XOR operation when covering existing colors.<br />

❑ PorterDuffXfermode This is a very powerful transfer mode with which you can use any of<br />

the 16 Porter-Duff rules for image composition to control how the paint interacts with the existing<br />

canvas image.<br />

To apply transfer modes, use the setXferMode method as shown in the sample below:<br />

AvoidXfermode avoid = new AvoidXfermode(Color.BLUE, 10, AvoidXfermode.Mode.AVOID);<br />

borderPen.setXfermode(avoid);<br />

Improving Paint Quality with Anti-<strong>Ali</strong>asing<br />

When you create a new Paint object, you can pass in several fl ags that affect the way the Paint will<br />

be rendered. One of the most interesting is the ANTI_ALIAS_FLAG, which ensures that diagonal lines<br />

drawn with this paint are anti-aliased to give a smooth appearance (at the cost of performance).<br />

379

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

Saved successfully!

Ooh no, something went wrong!