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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

}<br />

Chapter 11: Advanced Android <strong>Development</strong><br />

String displayString = String.valueOf(displayPos);<br />

float stringSizeWidth = textPaint.measureText(displayString);<br />

canvas.drawText(displayString,<br />

(int)(center.x-stringSizeWidth/2),<br />

(int)(ypos)+1,<br />

textPaint);<br />

12. Now draw a thicker line at the earth/sky interface. Change the stroke thickness of the<br />

markerPaint object before drawing the line (then set it back to the previous value).<br />

markerPaint.setStrokeWidth(2);<br />

canvas.drawLine(center.x - radius / 2,<br />

(float)justTiltY,<br />

center.x + radius / 2,<br />

(float)justTiltY,<br />

markerPaint);<br />

markerPaint.setStrokeWidth(1);<br />

13. To make it easier to read the exact roll, you should draw an arrow and display a text string that<br />

shows the exact value.<br />

Create a new Path, and use the moveTo / lineTo methods to construct an open arrow that<br />

points straight up. Draw the path and a text string that shows the current roll.<br />

// Draw the arrow<br />

Path rollArrow = new Path();<br />

rollArrow.moveTo(center.x - 3, (int)innerBoundingBox.top + 14);<br />

rollArrow.lineTo(center.x, (int)innerBoundingBox.top + 10);<br />

rollArrow.moveTo(center.x + 3, innerBoundingBox.top + 14);<br />

rollArrow.lineTo(center.x, innerBoundingBox.top + 10);<br />

canvas.drawPath(rollArrow, markerPaint);<br />

// Draw the string<br />

String rollText = String.valueOf(rollDegree);<br />

double rollTextWidth = textPaint.measureText(rollText);<br />

canvas.drawText(rollText,<br />

(float)(center.x - rollTextWidth / 2),<br />

innerBoundingBox.top + textHeight + 2,<br />

textPaint);<br />

14. Spin the canvas back to upright so that you can draw the rest of the face markings.<br />

canvas.restore();<br />

15. Draw the roll dial markings by rotating the canvas 10 degrees at a time to draw either a mark or<br />

a value. When you’ve completed the face, restore the canvas to its upright position.<br />

canvas.save();<br />

canvas.rotate(180, center.x, center.y);<br />

for (int i = -180; i < 180; i += 10)<br />

{<br />

// Show a numeric value every 30 degrees<br />

if (i % 30 == 0) {<br />

String rollString = String.valueOf(i*-1);<br />

float rollStringWidth = textPaint.measureText(rollString);<br />

387

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

Saved successfully!

Ooh no, something went wrong!