Custom Android Hexagon progress bar (with source code) _android

Source: Internet
Author: User
Tags getcolor

This example describes the Android custom round progress bar, shared for everyone to reference. Specifically as follows:

You can also refer to these two articles to learn: "Custom Android Round progress bar (with source code) " " Android with progress of the circular progress bar"

The screenshot of the running effect is as follows:

Main code:

Package com.sxc.hexagonprogress;
Import Java.util.Random;
Import Android.content.Context;
Import android.content.res.ColorStateList;
Import android.content.res.Resources;
Import Android.content.res.TypedArray;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Paint;
Import Android.graphics.PaintFlagsDrawFilter;
Import Android.graphics.Path;
Import Android.graphics.RectF;
Import Android.graphics.Typeface;
Import Android.util.AttributeSet;
Import Android.util.Log;


Import Android.view.View;
  /** * Hexagon Belt progress bar, thread safe view, can update progress directly in the thread * * @author Sunxunchao * */public class Hexagonprogress extends View {/**

 * Reference to the Brush object/Private Paint Paint, Mpaint;

 /** * Brush Path * * Private path path, MPath;

 /** * Ring Color * * private int roundcolor;

 /** * The color of the progress of the ring * * private int roundprogresscolor;

 /** * The color of the string of the intermediate progress percentage * * private int textcolor;

 /** * The font of the string for the intermediate progress percentage * * private float textsize; /** * Ring Width * * Private float ROUNDWIdth;

 /** * Max progress/private double Max;
 /** * Current progress/private double progress;

 /** * Whether to show the progress of the middle * * Private Boolean textisdisplayable;

 /** * Progress style, solid or hollow */private int style;
 public static final int STROKE = 0;

 public static final int FILL = 1;
 Public Hexagonprogress {This (context, NULL);
 Public hexagonprogress (context, AttributeSet attrs) {This (context, attrs, 0);

  Public hexagonprogress (context, AttributeSet attrs, int defstyle) {Super (context, attrs, Defstyle);
  Paint = new paint ();

  Mpaint = new Paint ();

  TypedArray Mtypedarray = context.obtainstyledattributes (Attrs, R.styleable.hexagonprogressbar);
  Gets the custom attribute and default value Roundcolor = Mtypedarray.getcolor (R.styleable.hexagonprogressbar_hexagoncolor, color.red);
  Roundprogresscolor = Mtypedarray.getcolor (R.styleable.hexagonprogressbar_hexagonprogresscolor, Color.GREEN); TextColor = Mtypedarray.getcolor (r.styleable.hexagonprogresSbar_textcolor, Color.green);
  Textsize = Mtypedarray.getdimension (r.styleable.hexagonprogressbar_textsize, 15);
  Roundwidth = Mtypedarray.getdimension (R.styleable.hexagonprogressbar_hexagonwidth, 5);
  max = Mtypedarray.getinteger (R.styleable.hexagonprogressbar_max, 100);

  textisdisplayable = Mtypedarray.getboolean (r.styleable.hexagonprogressbar_textisdisplayable, true);
 Mtypedarray.recycle ();

  } @Override protected void OnDraw (Canvas Canvas) {Super.ondraw (Canvas); /** * Draw hexagon/INT centre = getwidth ()/2;//Center coordinate int radius = (int) (CENTRE-ROUNDWIDTH/2);//hexagonal edge length Mpain T.setcolor (Roundcolor); Set the color of the ring Mpaint.setstyle (Paint.Style.STROKE); Setting Hollow Mpaint.setstrokewidth (Roundwidth); Sets the width of the ring Mpaint.setantialias (true); anti-aliasing MPath = new Path ()//SET path//First point coordinate (Centre-radius, getheight ()/2)//second point coordinate (Centre-radius/2,getheight ()/2-ma TH.SQRT (3) *RADIUS/2)//Third point coordinates (Centre+radius/2,getheight ()/2-math.sqrt (3) *RADIUS/2)//Fourth point sitSuperscript (Centre+radius,getheight ()/2)//fifth point coordinates (CENTRE+RADIUS/2,MATH.SQRT (3) *radius/2+getheight ()/2)//sixth point coordinates ( CENTRE-RADIUS/2,MATH.SQRT (3) *radius/2+getheight ()/2) Mpath.moveto (Centre-radius, centre); A Mpath.lineto (CENTRE-RADIUS/2, (float) (CENTRE-MATH.SQRT (3) * RADIUS/2))//B Mpath.lineto (centre + Radius /2, (float) (CENTRE-MATH.SQRT (3) * RADIUS/2))//C Mpath.lineto (centre + RADIUS, centre);/D Mpath.lineto (centre + RADIUS/2, (float) ((MATH.SQRT (3) * RADIUS/2) + centre))//E Mpath.lineto (CENTRE-RADIUS/2, (float) ((MATH.SQRT) (3
  ) * RADIUS/2) + centre);//F mpath.close ();

  Canvas.drawpath (MPath, mpaint);
  /** * Draw percent of progress * * * mpaint.setstrokewidth (0);
  Mpaint.setcolor (TextColor);
  Mpaint.settextsize (TEXTSIZE); Mpaint.settypeface (Typeface.default_bold); Sets the font int percent = (int) ((float) progress/(FLOAT) max) * 100); In the middle of the progress percentage, first converted to float in the division operation, otherwise all 0 float textWidth = mpaint.measuretext (Percent + "%"); To measure the font width, we need to set the width of the font to the circleRing Middle if (textisdisplayable && style = = STROKE) {Canvas.drawtext (percent + "%", CENTRE-TEXTWIDTH/2, Centr E + TEXTSIZE/2, mpaint);
  Draw a percentage of progress}/** * Draw hexagonal progress/path = new Path (); Paint.setstrokewidth (Roundwidth); Set the width of the ring Paint.setcolor (Roundprogresscolor);

  Sets the color of the Progress Paint.setantialias (TRUE);

  Double k= (progress*6)/MAX;
   Paint.setstyle (Paint.Style.STROKE);
    if (k <= 1| | k==0) {Path.moveto (centre + RADIUS, centre);
   Path.lineto ((float) (CENTRE+RADIUS-K*RADIUS/2), (float) (CENTRE+K*RADIUS*MATH.SQRT (3)/2)); 
    else if (k>1&&k<=2) {Path.moveto (centre + RADIUS, centre);
    Path.lineto (centre + RADIUS/2, (float) ((MATH.SQRT (3) * RADIUS/2) + centre));
   Path.lineto ((float) (Centre+1.5*radius-k*radius), (float) (CENTRE+0.5*MATH.SQRT (3) *radius)); 
    }else if (k>2&&k<=3) {Path.moveto (centre + RADIUS, centre); Path.lineto (centre + RADIUS/2, (float) ((MATH.SQRT (3) * Radius/2) + centre);
    Path.lineto (CENTRE-RADIUS/2, (float) (MATH.SQRT (3) * RADIUS/2) + centre)); Path.lineto ((float) (centre+0.5*radius-0.5*radius*k), (float) (CENTRE+1.5*MATH.SQRT (3) *radius-0.5*k*radius*
   MATH.SQRT (3))); 
    }else if (k>3&&k<=4) {Path.moveto (centre + RADIUS, centre);
    Path.lineto (centre + RADIUS/2, (float) ((MATH.SQRT (3) * RADIUS/2) + centre));
    Path.lineto (CENTRE-RADIUS/2, (float) (MATH.SQRT (3) * RADIUS/2) + centre));
    Path.lineto (Centre-radius, centre);
   Path.lineto ((float) (Centre-radius+0.5*k*radius-1.5*radius), (float) (centre-0.5* (k-3) *radius*math.sqrt (3)); 
    }else if (k>4&&k<=5) {Path.moveto (centre + RADIUS, centre);
    Path.lineto (centre + RADIUS/2, (float) ((MATH.SQRT (3) * RADIUS/2) + centre));
    Path.lineto (CENTRE-RADIUS/2, (float) (MATH.SQRT (3) * RADIUS/2) + centre));
    Path.lineto (Centre-radius, centre); Path.lineto (CENTRE-RADIUS/2, (float) (CENTRE-MATH.SQRT (3) * RADIUS/2));
   Path.lineto ((float) (k-4) *radius+centre-0.5*radius), (float) (CENTRE-MATH.SQRT (3) * radius/2)); 
    }else if (k>5&&k<6) {Path.moveto (centre + RADIUS, centre);
    Path.lineto (centre + RADIUS/2, (float) ((MATH.SQRT (3) * RADIUS/2) + centre));
    Path.lineto (CENTRE-RADIUS/2, (float) (MATH.SQRT (3) * RADIUS/2) + centre));
    Path.lineto (Centre-radius, centre);
    Path.lineto (CENTRE-RADIUS/2, (float) (CENTRE-MATH.SQRT (3) * radius/2));
    Path.lineto (centre + RADIUS/2, (float) (CENTRE-MATH.SQRT (3) * radius/2)); Path.lineto ((float) (centre+0.5*radius+0.5* (k-5) *radius), (float) (CENTRE-0.5*MATH.SQRT (3) *radius+0.5*math.sqrt (3
   ) * (k-5) *radius)); 
    }else {Path.moveto (centre + RADIUS, centre);
    Path.lineto (centre + RADIUS/2, (float) ((MATH.SQRT (3) * RADIUS/2) + centre));
    Path.lineto (CENTRE-RADIUS/2, (float) (MATH.SQRT (3) * RADIUS/2) + centre));
    Path.lineto (Centre-radius, centre); Path.lineto (centRE-RADIUS/2, (float) (CENTRE-MATH.SQRT (3) * radius/2));
    Path.lineto (centre + RADIUS/2, (float) (CENTRE-MATH.SQRT (3) * radius/2));
    Path.lineto (centre + RADIUS, centre);
   Path.close ();

 } canvas.drawpath (path, paint);
 Public synchronized Double Getmax () {return max;  /** * Set the maximum rate of progress * * @param max/public synchronized void Setmax (int max) {if (Max < 0) {throw new
  IllegalArgumentException ("Max not less than 0");
 } This.max = max;
 /** * Get progress. Need Sync * * @return/public synchronized Double getprogress () {return progress; /** * Set Progress, this is a thread-safe control, due to the problem of multiple lines, need synchronous Refresh interface Call Postinvalidate () can refresh on non-UI thread * * @param progress/Public Synchronize d void setprogress (double progress) {if (Progress < 0) {throw new IllegalArgumentException ("Progress not less th
  an 0 ");
  } if (Progress > Max) {progress = max;
   } if (Progress <= max) {this.progress = progress;
  Postinvalidate ();

}

 } public int Getcriclecolor () {return roundcolor;
 The public void Setcriclecolor (int criclecolor) {this.roundcolor = Criclecolor;
 public int Getcricleprogresscolor () {return roundprogresscolor;
 The public void Setcricleprogresscolor (int cricleprogresscolor) {this.roundprogresscolor = Cricleprogresscolor;
 public int GetTextColor () {return textcolor;
 The public void SetTextColor (int textcolor) {this.textcolor = TextColor;
 public float GetTextSize () {return textsize;
 public void Settextsize (float textsize) {this.textsize = Textsize;
 public float Getroundwidth () {return roundwidth;
 public void Setroundwidth (float roundwidth) {this.roundwidth = Roundwidth;

 }

}

Create a new Attrs.xml file in values:

<?xml version= "1.0" encoding= "UTF-8"?>
<resources>
 <declare-styleable name= " Hexagonprogressbar "> 
  <attr name=" hexagoncolor "format=" Color "/> <attr-name=
  " Hexagonprogresscolor "format=" color "/> <attr name=" hexagonwidth "format=" Dimension "
  ></attr>
  <attr name= "textcolor" format= "color"/> <attr name= 
  "textsize" format= "Dimension"/> 
  < attr name= "Max" format= "integer" ></attr> 
  <attr name= "Textisdisplayable" format= "boolean" ></ Attr>
  <!--<attr name= "style" >
   <enum name= "STROKE" value= "0" ></enum>
   < Enum name= "FILL" value= "1" ></enum>
  </attr>-->
 </declare-styleable> 
</ Resources>

Free download of the project: Android Hexagon progress bar

I hope this article will help you learn about Android software programming.

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.