Android's Compass (compass) detailed

Source: Internet
Author: User
Tags drawtext getcolor min

Compass (Compass) is a custom view that inherits the view class, overriding the bounds (onmeasure) and content (OnDraw) of the views;

As shown in figure:

The following is the specific design of the compass:

1. Create Compassview class, compass view

Location: Java->package->compassview

Package Mzx.spike.compass.app;  
Import Android.content.Context;  
Import android.content.res.Resources;  
Import Android.graphics.Canvas;  
Import Android.graphics.Paint;  
Import Android.util.AttributeSet;  
Import Android.view.View;  
      
Import android.view.accessibility.AccessibilityEvent; 
 /** * Created by C.l.wang on 14-3-16. * * public class Compassview extends View {private float bearing;//azimuth public void setbearing FL  
        Oat _bearing) {bearing = _bearing;  
    Sendaccessibilityevent (accessibilityevent.type_view_text_changed);  
    public float getbearing () {return bearing;  
    Private Paint Markerpaint;  
    Private Paint Textpaint;  
    Private Paint Circlepaint;  
    Private String northstring;  
    Private String eaststring;  
    Private String southstring;  
    Private String weststring;  
      
    private int textHeight;  
Public Compassview {        Super (context);  
    Initcompassview ();  
        Public Compassview (context, AttributeSet attrs) {Super (context, attrs);  
    Initcompassview ();  Public Compassview (context, AttributeSet attrs, int defaultstyle) {Super (context, attrs,  
        DefaultStyle);  
    Initcompassview ();  
      
        private void Initcompassview () {setfocusable (true);  
      
        Resources R = This.getresources ();  
        Circlepaint = new Paint (Paint.anti_alias_flag);  
        Circlepaint.setcolor (R.getcolor (R.color.background_color)); Circlepaint.setstrokewidth (1);  
      
        Stroke width Circlepaint.setstyle (Paint.Style.FILL_AND_STROKE);  
        northstring = r.getstring (R.string.cardinal_north);  
        eaststring = r.getstring (r.string.cardinal_east);  
        southstring = r.getstring (R.string.cardinal_south);  
weststring = r.getstring (r.string.cardinal_west);      
        Textpaint = new Paint (Paint.anti_alias_flag);  
      
        Textpaint.setcolor (R.getcolor (R.color.text_color));  
      
        TextHeight = (int) textpaint.measuretext ("YY");  
        Markerpaint = new Paint (Paint.anti_alias_flag);  
    Markerpaint.setcolor (R.getcolor (R.color.marker_color)); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {int measure  
        dwidth = measure (Widthmeasurespec);  
      
        int measuredheight = measure (Heightmeasurespec);  
      
        int d = math.min (Measuredwidth, measuredheight);  
    Setmeasureddimension (d, D);  
      
        protected int measure (int measurespec) {int result;  
        int specmode = Measurespec.getmode (Measurespec);  
      
        int specsize = measurespec.getsize (Measurespec);  
        if (Specmode = = measurespec.unspecified) {result = 200; else {result = Specsize;  
        return result;  
        } @Override protected void OnDraw (Canvas Canvas) {int mmeasuredwidth = Getmeasuredwidth ();  
      
        int mmeasuredheight = Getmeasuredheight ();  
        int px = MMEASUREDWIDTH/2;  
      
        int py = MMEASUREDHEIGHT/2;  
      
        int radius = math.min (px, py);  
      
        Canvas.drawcircle (px, py, radius, circlepaint);  
        Canvas.save (); Canvas.rotate (-bearing, px, py);  
        The opposite direction rotates int textWidth = (int) textpaint.measuretext ("W");  
        int cardinalx = PX-TEXTWIDTH/2;  
      
        int cardinaly = Py-radius+textheight;  
      
            for (int i=0; i<24; i++) {canvas.drawline (px, Py-radius, px, py-radius+10, markerpaint);  
            Canvas.save ();  
      
            Canvas.translate (0, textHeight);  
                if (i%6 = = 0) {String dirstring = ""; switch (i) {case (0): {dirstring = northstring;  
                        int arrowy = 2*textheight;  
                        Canvas.drawline (px, arrowy, px-5, 3*textheight, Markerpaint);  
                        Canvas.drawline (px, arrowy, px+5, 3*textheight, Markerpaint);  
                    Break Case (6): dirstring = eaststring;  
                    Break Case (a): Dirstring = southstring;  
                    Break Case (a): Dirstring = weststring;  
                Break  
            } canvas.drawtext (Dirstring, Cardinalx, Cardinaly, Textpaint);  
                else if (i%3 = = 0) {String angle = string.valueof (i*15);  
      
                float angletextwidth = textpaint.measuretext (angle);  
                int angletextx = (int) (PX-ANGLETEXTWIDTH/2);  
                int angletexty = Py-radius+textheight; Canvas.drawtext (ANgle, Angletextx, Angletexty, Textpaint);  
      
            } canvas.restore ();  
        Canvas.rotate (px, py);  
    } canvas.restore ();  
        @Override public boolean dispatchpopulateaccessibilityevent (final Accessibilityevent event) {  
      
        Super.dispatchpopulateaccessibilityevent (event);  
      
            if (Isshown ()) {String bearingstr = string.valueof (bearing);  
      
            Event.gettext (). Add (BEARINGSTR);  
        return true;  
    else return false; }  
}

This class of code more, detailed:

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

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.