Simple example sharing _android for making progress boxes and circular progress bars in Android

Source: Internet
Author: User
Tags getcolor

Progress Box

Import Android.content.Context; 
Import Android.graphics.Canvas; 
Import Android.graphics.Color; 
Import Android.graphics.Paint; 
Import Android.util.AttributeSet; 
 
Import Android.view.View; 


Import Java.util.Random; 
  public class Obliqueprogressbar extends View {private Paint mpaint; 
 
  private float mprogress; 
  Public Obliqueprogressbar {This (context, NULL); 
  Public Obliqueprogressbar (context, AttributeSet attrs) {This (context, attrs, 0); Obliqueprogressbar (context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, defst 
 
    YLEATTR); 
    Mpaint = new Paint (); 
  Mpaint.setantialias (TRUE); 
 
    } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); 
 
    if (mprogress = = 0) return; 
    Debris Rain Mpaint.setcolor (Color.parsecolor ("#a96ecb")); 
    Mpaint.setstrokewidth (3); 
    Random Random = new Random (); 
    int SX, SY; for (int i = 0; I &lT 200; 
      i++) {sx = Random.nextint (getwidth () + 10); 
sy = Random.nextint (getheight () + 10); 
      Canvas.drawline (SX, SY, Sx+random.nextint (5), Sy+random.nextint (5), mpaint); 
    Canvas.drawcircle (SX, SY, Random.nextint (5) + 1, mpaint); 
    }//Progress Mpaint.setcolor (Color.parsecolor ("#6AFFFFFF")); 
    Mpaint.setstrokewidth (15); 
    float x = mprogress * GetWidth (); 
    for (int i = 0; i < x i + +) {canvas.drawline (i-30, -10, i +, getheight () +, mpaint); 
    } public void Setprogress (float progress) {this.mprogress = progress; 
  Invalidate (); 


 } 
}

Circular progress bar

Let's take a look at the effect:

The following is directly on the code:

Ckage Com.stone.circleprogressbar.view; 
Import Android.content.Context; 
Import Android.content.res.TypedArray; 
Import Android.graphics.Bitmap; 
Import Android.graphics.Canvas; 
Import Android.graphics.Color; 
Import android.graphics.LinearGradient; 
Import Android.graphics.Paint; 
Import Android.graphics.RectF; 
Import Android.graphics.Shader; 
Import Android.util.AttributeSet; 
 
Import Android.view.View; 

Import COM.STONE.CIRCLEPROGRESSBAR.R; 
 public class Circleprogressbar extends View {private float mprogress; 
 private int mbarcolor; 
 private int mtextcolor; 
 
 private float mtextsize; 
 Public Circleprogressbar {This (context, NULL); 
 Public Circleprogressbar (context, AttributeSet attrs) {This (context, attrs, 0); Circleprogressbar (context, AttributeSet attrs, int defstyleattr) {Super (context, Attrs, Defstylea 
 
  TTR); TypedArray array = context.obtainstyledattributes (Attrs, r.styleable.circleprogressbar); 
  Mbarcolor = Array.getcolor (R.styleable.circleprogressbar_barcolor, Color.gray); 
  Mtextcolor = Array.getcolor (R.styleable.circleprogressbar_textcolor, Color.gray); 
 mprogress = array.getfloat (r.styleable.circleprogressbar_progress, 0); @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {super.onmeasure (widthmeasures 
 PEC, Heightmeasurespec); 
  public void setprogress (float count) {mprogress = count; 
 Invalidate (); 
  } @Override protected void OnDraw (Canvas Canvas) {int w = getwidth (); 
  int h = getheight (); 
  int strokewidth = 20; 
  int radius = W/2-strokewidth/2;//Circle radius Bitmap barbitmap = Bitmap.createbitmap (W, H, Bitmap.Config.ARGB_8888); 
  Paint Paint = new Paint (); Paint.setantialias (TRUE); Anti-aliasing paint.setstrokewidth (strokewidth); Stroke width Paint.setdither (true); 
  Anti-jitter * * Outer frame/Canvas Circlecanvas = new Canvas (BARBITMAP); 
  RECTF rect = new RECTF (0, 0, W, h); Paint.setcolor (ColOr.argb (0x11, 0XCC, 0XCC, 0XCC)); Circlecanvas.drawrect (rect, paint); 
  It's no use. Just look at the outer frame/* Inner circle/Paint.setcolor (Color.cyan); 
  Paint.setshader (New lineargradient (0, 0, W, H, Color.Red, Color.green, Shader.TileMode.CLAMP)); 
  Circlecanvas.drawcircle (W/2, H/2, RADIUS-STROKEWIDTH/2, paint); 
 
  Paint.setshader (NULL); 
  /* Outer Circle * * Paint.setcolor (Mbarcolor); 
  Paint.setstyle (Paint.Style.STROKE); 
 
  Circlecanvas.drawcircle (W/2, H/2, radius, paint); * * Arc/Paint.setshader (new lineargradient (0, 0, W, H, New Int[]{color.green, Color.magenta, Color.cyan, color.re 
  D}, New float[]{0.2f, 0.5f, 0.7f, 1.0f}, Shader.TileMode.CLAMP); 
  Float cur = mprogress * 360 * 0.01F;  Circlecanvas.drawarc (New RECTF (STROKEWIDTH/2, STROKEWIDTH/2, W-STROKEWIDTH/2, H-STROKEWIDTH/2), 360-45, 
  Cur, false, paint); 
 
  Paint.setshader (NULL); 
  /* Text */Paint.setcolor (Mtextcolor); 
  if (mtextsize = = 0) {calctextsize (Paint, W, strokewidth); }else {paint.settextsize (mtextsize); 
  } paint.settextalign (Paint.Align.LEFT);//default String percent = mprogress + "%"; 
  Paint.setstyle (Paint.Style.FILL); 
 
  Circlecanvas.drawtext (Percent, W/2-paint.measuretext (percent)/2, H/2 + paint.gettextsize ()/2, paint); 
 Canvas.drawbitmap (barbitmap, 0, 0, paint); /** * Calculates and sets the most suitable textsize * * @param paint * @param max Max width * @param offset offset/private void CA 
  Lctextsize (Paint Paint, int max, int offset) {Float width = paint.measuretext ("99.99%"); 
   while (Width < Max * 3/5) {paint.settextsize (paint.gettextsize () + 5); 
  width = Paint.measuretext ("92.88%") + OFFSET/2; 
 } mtextsize = Paint.gettextsize (); 

 } 
 
}

Set Progress Refresh display   call   setprogress ().

Related Article

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.