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

382<br />

As the previous image is limited to black and white, you’ll need to create the control in order to see the<br />

full effect.<br />

1. Start by modifying the colors.xml resource fi le to include color values for the border gradient,<br />

the glass compass shading, the sky, and the ground. Also update the colors used for the border<br />

and the face markings.<br />

<br />

<br />

#FFFF<br />

#F000<br />

#FFFF<br />

#7AAA<br />

#FF444444<br />

#FF323232<br />

#FF414141<br />

#FFFFFFFF<br />

#FFA52A2A<br />

#FFFFC125<br />

#FF5F9EA0<br />

#FF00008B<br />

<br />

2. The Paint and Shader objects used for the sky and ground in the artifi cial horizon are created<br />

based on the size of the current View, so they’re not static like the Paint objects you created in<br />

Chapter 4. Instead of creating Paint objects, construct the gradient arrays and colors they use.<br />

int[] borderGradientColors;<br />

float[] borderGradientPositions;<br />

int[] glassGradientColors;<br />

float[] glassGradientPositions;<br />

int skyHorizonColorFrom;<br />

int skyHorizonColorTo;<br />

int groundHorizonColorFrom;<br />

int groundHorizonColorTo;<br />

3. Update the CompassView’s initCompassView method to initialize the variables created in Step<br />

2 using the resources from Step 1. The existing method code can be left largely intact, with some<br />

changes to the textPaint, circlePaint, and markerPaint variables, as highlighted below:<br />

protected void initCompassView() {<br />

setFocusable(true);<br />

// Get external resources<br />

Resources r = this.getResources();<br />

circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);<br />

circlePaint.setColor(R.color.background_color);<br />

circlePaint.setStrokeWidth(1);<br />

circlePaint.setStyle(Paint.Style.STROKE);<br />

northString = r.getString(R.string.cardinal_north);

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

Saved successfully!

Ooh no, something went wrong!