Android Drawing Compass

Source: Internet
Author: User

1. Inadvertently see a compass UI, here is a simple imitation of a bit. is actually some of the changes in the canvas.

The others are:

  

3. Simply say the idea:

1) First, draw a black disc

2) Then draw the scale on the disc (that is, some transformations on the canvas)

3) Text add

4. Directly on the code:

  

1  Public classCompassviewextendsView {2     PrivatePaint Circlepaint, Tickpaint;3     PrivateTextpaint Textpaint;4     //Specify control width and height for adaptive5     Private floatvwidth, vheight;6     //the radius of the disc7     Private floatCompassradiu;8     //length of the scale segment9     Private floatTickheight;Ten     //font height and width One     Private floattextHeight, TextWidth; A  -      PublicCompassview (Context context) { -         Super(context); the Initpaint (context); -     } -  -      PublicCompassview (Context context, AttributeSet attrs) { +         Super(context, attrs); - Initpaint (context); +     } A  at     Private voidInitpaint (Context context) { -         //Initialize the picture disc -Circlepaint =NewPaint (Paint.anti_alias_flag |paint.dither_flag); - Circlepaint.setcolor (color.black); - Circlepaint.setstyle (Paint.Style.FILL); -  in         //initialize a scale brush -Tickpaint =NewPaint (paint.anti_alias_flag); to Tickpaint.setcolor (color.red); +Tickpaint.setstrokewidth (3); -  the         //Initialize the brush of a word *Textpaint =NewTextpaint (paint.anti_alias_flag); $ Textpaint.setcolor (color.white);Panax NotoginsengTextpaint.settextsize (20); -  the     } +  A     //self-adapting to do it here the @Override +     protected voidOnsizechanged (intWintHintOLDW,intOLDH) { -         //Gets the width and height of the control $Vwidth =W; $Vheight =h; -Compassradiu = Math.min (W, h)/2; -Tickheight = (1/12f) *Compassradiu; theTextHeight = Textpaint.descent ()-textpaint.ascent (); - Wuyi     } the  - @Override Wu     protected voidOnDraw (canvas canvas) { - Canvas.drawcolor (Color.cyan); About         //Black Disc $ canvas.drawcircle (Compassradiu, Compassradiu, Compassradiu, circlepaint); -         //draw a red scale -         intdegress; -         floatTextWidth; A  +          for(inti = 0; I < 24; i++) { the Canvas.save (); - canvas.translate (Compassradiu, Compassradiu); $             //Current canvas rotation angle thedegress = i * 15; theCanvas.rotate (15 *i); the  theCanvas.drawline (0,-compassradiu, 0,-compassradiu +Tickheight, - tickpaint); in             Switch(degress) { the              Case0: theTextWidth = Textpaint.measuretext ("45"); AboutDrawText (Canvas, "45", textWidth); the                  Break; the  the              Case45: +TextWidth = Textpaint.measuretext ("East"); -DrawText (Canvas, "East", textWidth); the                  Break;Bayi              Case90: theTextWidth = Textpaint.measuretext ("135"); theDrawText (Canvas, "135", textWidth); -                  Break; -              Case135: theTextWidth = Textpaint.measuretext ("South"); theDrawText (Canvas, "South", textWidth); the                  Break; the              Case180: -TextWidth = Textpaint.measuretext ("225"); theDrawText (Canvas, "225", textWidth); the                  Break; the              Case225:94TextWidth = Textpaint.measuretext ("West"); theDrawText (Canvas, "West", textWidth); the                  Break; the              Case270:98TextWidth = Textpaint.measuretext ("315"); AboutDrawText (Canvas, "315", textWidth); -                  Break;101              Case315:102TextWidth = Textpaint.measuretext ("North");103DrawText (Canvas, "North", textWidth);104Canvas.drawline (0, the-compassradiu + tickheight + textHeight + 10,106-TEXTWIDTH/3,-compassradiu + tickheight +TextHeight107+ 30, tickpaint);108Canvas.drawline (0,109-compassradiu + tickheight + textHeight + 10, theTEXTWIDTH/3,-compassradiu + tickheight +TextHeight111+ 30, tickpaint); the 113                  Break; the             default: the                  Break; the             }117 Canvas.restore ();118         }119  -     }121 122     Private voidDrawText (canvas canvas, String text,floattextWidth) {123 124Canvas.drawtext (text,-(TEXTWIDTH/2),-compassradiu +Tickheight the+textHeight, textpaint);126 127     } -}

After the operation is:

  

SOURCE download

Android Drawing Compass

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.