Android level signal status recognition view Draw

Source: Internet
Author: User

1. Preface

Level signal Status View in today's Android system is common. The status of the icon is very classic, there are several states, to the time when the power is not fast some will blink prompting the user to charge, there are some maps app GPS signal strength hints. WiFi signal strength is also some, anyway the application scenario is probably the case.

2. Achieve the goal

Don't say so much, just go on dry and talk to the picture.


What I'm doing here is a view of 4 states.

1, no intermediate display bar of the empty state

2, there is a small red circle signal low status

3, there is a yellow oval representation of the general state

4, full of green bar to indicate good state

In this view type only gives three levels of settings when set to a level. That is, the display only has a red view, then this red ball flashes once in a while, prompting the user.

The other level is a simple drawing display.

3. Realization of Ideas

In fact, the implementation process is very easy, just a simple drawing can be, the canvas class Canvas.drawroundrect method can draw a rounded rectangle, so you can use this method to draw a white background box. Then draw in two, a yellow and green, just to control the size and fill pattern. Red with a canvas.drawcircle fix, and then left this flashing, just started not very good how to do, in fact, we open a thread in the background, and then every other time to update the state of the view and then once again draw the effect can be achieved.

4. Implementation code

Package Com.spring.circlview;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.graphics.paint.style;import Android.graphics.rectf;import Android.os.asynctask;import Android.util.attributeset;import Android.util.typedvalue;import Android.view.view;public class Gpssigntview extends View {public Gpssigntview (Context Context, AttributeSet attrs, int defstyleattr) {Super (context, attrs, defstyleattr); init ();} Public Gpssigntview (context context, AttributeSet Attrs) {Super (context, attrs); init ();} Public Gpssigntview (Context context) {super (context); init ();} private void Init () {mpaint = new Paint ();//brush smooth, otherwise edge unsightly mpaint.setantialias (true); Mpaint.setcolor (color.white);// The set brush does not fill mpaint.setstyle (style.stroke);//Stroke size mpaint.setstrokewidth (strokewidth);//mpaint.setalpha (0x88);// Three drawn rectangular range RECTF = new RECTF (Strokewidth, Strokewidth, Width-strokewidth, height-strokewidth); yellow = new RECTF ( STROKEWIDTH*2,HEIGHT*2/5, WIDth-strokewidth*2, height-strokewidth*2); green = new RECTF (strokewidth*2,strokewidth*2, Width-strokewidth*2, height-strokewidth*2);//new twinkletask (). Execute (0);} Private Paint mpaint;p rivate rectf rectf;p rivate RECTF yellow;p rivate rectf green;private int width = (int) typedvalue. Applydimension (Typedvalue.complex_unit_dip, Ten, Getresources (). Getdisplaymetrics ());p rivate int height = (int) Typedvalue.applydimension (Typedvalue.complex_unit_dip, Getresources (). Getdisplaymetrics ());p rivate float Radus = width/2;private Float strokewidth = typedvalue.applydimension (Typedvalue.complex_unit_dip, 1, getResources (). Getdisplaymetrics ());p rivate int level1_color = color.red;private int level2_color = color.yellow;private int level3_        color = color.green;private int status = 1; Private Boolean run = True;public int getStatus () {return status;} public void setStatus (int status) {this.status = status;}         int Interval = 500; When the view is attached, call @overrideprotected void Onattachedtowindow () {Super.onattachedtowindow (); new Twinkletask (). Execute (0);} When the view is detached, call @overrideprotected void Ondetachedfromwindow () {Super.ondetachedfromwindow (); run = false;} @Overrideprotected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (Widthmeasurespec, HEIGHTMEASURESPEC);//Resets the size of the view setmeasureddimension (width, height);} Heart jumper thread, used to update the status regularly class Twinkletask extends Asynctask<integer, integer, integer>{@Overrideprotected integer Doinbackground (Integer ... params) {while (run) {try {thread.sleep (Interval);//Not at level below does not update if (status<=1) Publishprogress (0);}                     catch (Interruptedexception e) {e.printstacktrace ();}} return 0;} @Overrideprotected void Onprogressupdate (Integer ... values) {super.onprogressupdate (values);//update Bounce State, The view switches between level 0 and level status = (status+1)%2;invalidate ();}} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);//black background is not necessarily required canvas.drawcolor (color.black); Mpaint.setcolor (Color.White); Mpaint.setstyle (style.stroke);//OutermostWhite fillet square canvas.drawroundrect (RECTF, Radus, Radus, mpaint);//status Draw switch (status) {case 0:{}break;case 1:{mpaint.setcolor (level1_color);//Set Brush fill Mpaint.setstyle (Style.fill);//Draw a small red circle canvas.drawcircle (THIS.WIDTH/2, HEIGHT-THIS.WIDTH/2 , radus-strokewidth*2, mpaint);} Break;case 2:{mpaint.setcolor (Level2_color);//Set Brush to fill Mpaint.setstyle (Style.fill);//Draw Yellow square canvas.drawroundrect ( Yellow, Radus, Radus, mpaint);} Break;case 3:{mpaint.setcolor (Level3_color);//Set Brush fill Mpaint.setstyle (Style.fill);//Fill green Box Canvas.drawroundrect ( Green, Radus, Radus, mpaint);} Break;}}}
The core code of view is the top, very brief. See if you can read it.

Layout code:

<?

XML version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" Match_parent "android:layout_height=" match_parent "android:orientation=" vertical " > <com.spring.circlview.gpssigntview android:id= "@+id/level1" android:layout_width= "20DP" Android : Layout_centerinparent= "true" android:layout_height= "100DP"/> <com.spring.circlview.gpssigntview an Droid:id= "@+id/level2" android:layout_torightof= "@+id/level1" android:layout_centerinparent= "true" Androi D:layout_width= "20DP" android:layout_height= "100DP"/> <com.spring.circlview.gpssigntview android:id = "@+id/level3" android:layout_width= "20DP" android:layout_torightof= "@+id/level2" android:layout_centerinparent= " True "android:layout_height=" 100DP "/> </RelativeLayout>


Main file calling code

Gpssigntview Level2 = (gpssigntview) This.findviewbyid (R.ID.LEVEL2); Level2.setstatus (2); Gpssigntview level3 = (gpssigntview) This.findviewbyid (R.ID.LEVEL3); Level3.setstatus (3);

The project file will not be uploaded. Directly paste the view code into the project can be used.





Copyright notice: This article blog original articles, blogs, without consent, may not be reproduced.

Android level signal status recognition view Draw

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.