Android Level Signal status identification View rendering

Source: Internet
Author: User

1. Preface

View of the Level Signal status is very common in the current Android system. For example, the icon of the battery status in the upper-right corner of the mobile phone is very classic and has several statuses, when there is no power available, some will flash and prompt the user to charge. There are some GPS signal strength prompts for map apps and Wifi signal strength prompts, the application scenario is probably like this.

2. Goals

Don't talk so much nonsense, just look at the pictures and talk about it.


Why dynamic graphs cannot be moved ~ _~! <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + pgltzybzcm9 "http://www.2cto.com/uploadfile/Collfiles/20140518/201405180927524.png" alt = "\">

I implement four statuses of views.

1. If there is no intermediate display bar, it indicates the null status.

2. There is a low red circle Signal

3. There is a yellow oval representing the normal state

4. The green lines indicate a good state.

In this View type, only three levels are provided. When it is set to a level, only one red View is displayed, and the red ball flashes every other time, prompt the user. Other levels are a simple graphic display.

3. Implementation ideas

In fact, the implementation process is very simple, as long as a simple drawing on the line, painting the canvas class. the drawRoundRect method can draw a rectangle with rounded corners, so you can use this method to draw a white background box, and then draw two, a yellow and green, as long as you control the size and fill mode. Use a canvas in red. drawCircle is done, and the rest is flickering. We didn't really want to figure it out at the beginning. In fact, we opened a thread in the background, then, update the View status at a time and re-draw the View to achieve the beating effect.

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 (); // the Paint brush is smooth, otherwise the edge is unsightly. setAntiAlias (true); mPaint. setColor (Color. WHITE); // set the paint brush to not fill the mPaint. setStyle (Style. STROKE); // the size of the STROKE mPaint. setStrokeWidth (strokeWidth); // mPaint. setAlpha (0x88); // 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(cmd.exe cute (0);} private Paint mPaint; private RectF rectF; private RectF yellow; private RectF green; private int width = (int) TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_DIP, 10, getResources (). getDisplayMetrics (); private int height = (int) TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_DIP, 20, getResources (). getDisplayMetrics (); private float radus = width/2; private float strokeWidth = TypedValue. applyDimension (TypedValue. COMPLEX_UNIT_DIP, 1, getResources (). getDisplayMetrics (); private int levelsponcolor = Color. RED; private int level2_color = Color. YELLOW; private int level3_color = Color. GREEN; private int status = 1; public int getStatus () {return status;} public void setStatus (int status) {this. status = status;} int Interval = 500; @ Overrideprotected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {super. onMeasure (widthMeasureSpec, heightMeasureSpec); // reset the View Size setMeasuredDimension (width, height);} // heartbeat thread, used to regularly update the status class TwinkleTask extends AsyncTask
 
  
{@ Overrideprotected Integer doInBackground (Integer... params) {while (true) {try {Thread. sleep (Interval); // if (status <= 1) publishProgress (0);} catch (InterruptedException e) {e. printStackTrace () ;}}@ Overrideprotected void onProgressUpdate (Integer... values) {super. onProgressUpdate (values); // update the beating status. However, the status of the View is changed between level 0 and Level 1 (status + 1) % 2; invalidate ();}} @ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas); // The black background is not necessarily a required canvas. drawColor (Color. BLACK); mPaint. setColor (Color. WHITE); mPaint. setStyle (Style. STROKE); // The White rounded square canvas on the outermost layer. drawRoundRect (rectF, radus, radus, mPaint); // draw the switch (status) {case 0 :{} break; case 1: {mPaint. setColor (levelsponcolor); // you can specify the paint brush to fill the 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 the brush to fill the mPaint. setStyle (Style. FILL); // draw a yellow square canvas. drawRoundRect (yellow, radus, radus, mPaint);} break; case 3: {mPaint. setColor (level3_color); // set the brush to fill the mPaint. setStyle (Style. FILL); // FILL the canvas in the green box. drawRoundRect (green, radus, radus, mPaint) ;}break ;}}}
 
The core code of VIew is the above, which is very short. You should be able to understand it.

Layout code:

 
        
      
      
    
 

Main file call code

GpsSigntView level2 = (GpsSigntView) this.findViewById(R.id.level2);level2.setStatus(2);GpsSigntView level3 = (GpsSigntView) this.findViewById(R.id.level3);level3.setStatus(3);

Project files do not need to be uploaded. You can directly paste the View code to the project.





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.