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 4: Creating User Interfaces<br />

98<br />

}<br />

canvas.drawLine(px, arrowY, px+5, 3*textHeight,<br />

markerPaint);<br />

break;<br />

}<br />

case(6) : dirString = eastString; break;<br />

case(12) : dirString = southString; break;<br />

case(18) : dirString = westString; break;<br />

}<br />

canvas.drawText(dirString, cardinalX, cardinalY, textPaint);<br />

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

// Draw the text every alternate 45deg<br />

String angle = String.valueOf(i*15);<br />

float angleTextWidth = textPaint.measureText(angle);<br />

int angleTextX = (int)(px-angleTextWidth/2);<br />

int angleTextY = py-radius+textHeight;<br />

canvas.drawText(angle, angleTextX, angleTextY, textPaint);<br />

}<br />

canvas.restore();<br />

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

}<br />

canvas.restore();<br />

}<br />

7. To view the compass, modify the main.xml layout resource and replace the TextView reference<br />

with your new CompassView. This process is explained in more detail in the next section.<br />

<br />

<br />

<br />

<br />

8. Run the Activity, and you should see the CompassView displayed. See Chapter 10 to learn how<br />

to bind the CompassView to the device’s compass.<br />

Using Custom Controls<br />

Having created you own custom Views, you can use them within code and layouts as you would any<br />

other Android control. The snippet below shows how to add the CompassView created in the above<br />

example to an Activity, by overriding the onCreate method:<br />

@Override<br />

public void onCreate(Bundle icicle) {

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

Saved successfully!

Ooh no, something went wrong!