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.

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

386<br />

while (rollDegree > 180 || rollDegree < -180)<br />

{<br />

if (rollDegree > 180) rollDegree = -180 + (rollDegree - 180);<br />

if (rollDegree < -180) rollDegree = 180 - (rollDegree + 180);<br />

}<br />

6. Create paths that will fi ll each segment of the circle (ground and sky). The proportion of each<br />

segment should be related to the clamped pitch.<br />

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

skyPath.addArc(innerBoundingBox,<br />

-tiltDegree,<br />

(180 + (2 * tiltDegree)));<br />

7. Spin the canvas around the center in the opposite direction to the current roll, and draw the sky<br />

and ground paths using the Paints you created in Step 4.<br />

canvas.rotate(-rollDegree, px, py);<br />

canvas.drawOval(innerBoundingBox, groundPaint);<br />

canvas.drawPath(skyPath, skyPaint);<br />

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

8. Next is the face marking. Start by calculating the start and end points for the horizontal horizon<br />

markings.<br />

int markWidth = radius / 3;<br />

int startX = center.x - markWidth;<br />

int endX = center.x + markWidth;<br />

9. To make the horizon values easier to read, you should ensure that the pitch scale always starts<br />

at the current value. The following code calculates the position of the interface between the<br />

ground and sky on the horizon face:<br />

double h = innerRadius*Math.cos(Math.toRadians(90-tiltDegree));<br />

double justTiltY = center.y - h;<br />

10. Find the number of pixels that represents each degree of tilt.<br />

float pxPerDegree = (innerBoundingBox.height()/2)/45f;<br />

11. Now iterate over 180 degrees, centered on the current tilt value, to give a sliding scale of possible<br />

pitch.<br />

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

{<br />

double ypos = justTiltY + i*pxPerDegree;<br />

// Only display the scale within the inner face.<br />

if ((ypos < (innerBoundingBox.top + textHeight)) ||<br />

(ypos > innerBoundingBox.bottom - textHeight))<br />

continue;<br />

// Draw a line and the tilt angle for each scale increment.<br />

canvas.drawLine(startX, (float)ypos, endX, (float)ypos, markerPaint);<br />

int displayPos = (int)(tiltDegree - i);

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

Saved successfully!

Ooh no, something went wrong!