Custom ring progress bar and ring progress bar

Source: Internet
Author: User
Tags getcolor

Custom ring progress bar and ring progress bar
:

Package com. qiao. circleprogress_forexample; import android. app. activity; import android. OS. bundle; import android. view. menu; import android. view. menuItem;/*** home page ** @ author a little cooler **/public class MainActivity extends Activity {private RoundProgressBar progressBar1; private int progress = 0; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); progressBar1 = (RoundProgressBar) findViewById (R. id. progressBar1); new Thread (new Runnable () {@ Overridepublic void run () {while (progress <= 59) {progress + = 1; progressBar1.setProgress (progress); try {Thread. sleep (100);} catch (InterruptedException e) {// TODO Auto-generated catch blocke. printStackTrace ();}}}}). start () ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}@ Overridepublic boolean onOptionsItemSelected (MenuItem item) {// Handle action bar item clicks here. the action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml.int id = item. getItemId (); if (id = R. id. action_settings) {return true;} return super. onOptionsItemSelected (item );}}

 

Custom page

Package com. qiao. circleprogress_forexample; import android. content. context; import android. content. res. typedArray; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. graphics. rectF; import android. graphics. typeface; import android. util. attributeSet; import android. util. log; import android. view. view;/*** similar to the progress bar of the iphone, thread-safe View, you can directly update the progress in the process * @ author A little cooler **/public class RoundProgressBar extends View {/*** Paint object reference */private paint Paint;/*** ring color */private int roundColor; /* the color of the ring progress */private int roundProgressColor;/* the color of the string with the intermediate progress percentage */private int textColor; /*** font of the string with the intermediate progress percentage */private float textSize;/*** ring width */private float roundWidth; /* ** maximum progress */private int max;/* current progress */private int progress;/* Indicates whether to display the progress in 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 RoundProgressBar (Context context) {this (context, null);} public RoundProgressBar (Context context, AttributeSet attrs) {this (context, attrs, 0 );} public RoundProgressBar (Context context, AttributeSet attrs, int defStyle) {super (context, attrs, defStyle); paint = new Paint (); TypedArray mTypedArray = context. obtainStyledAttributes (attrs, R. styleable. roundProgressBar); // get custom attributes and default value roundColor = mTypedArray. getColor (R. styleable. roundProgressBar_roundColor, Color. WHITE); roundProgressColor = mTypedArray. getColor (R. styleable. roundProgressBar_roundProgressColor, Color. BLUE); textColor = mTypedArray. getColor (R. styleable. roundProgressBar_textColor, Color. BLACK); textSize = mTyp EdArray. getDimension (R. styleable. roundProgressBar_textSize, 15); roundWidth = mTypedArray. getDimension (R. styleable. roundProgressBar_roundWidth, 5); max = mTypedArray. getInteger (R. styleable. roundProgressBar_max, 100); textIsDisplayable = mTypedArray. getBoolean (R. styleable. roundProgressBar_textIsDisplayable, true); style = mTypedArray. getInt (R. styleable. roundProgressBar_style, 0); mTypedArray. recycle ();} @ Overrideprotected void onDraw (Canvas canvas) {super. onDraw (canvas);/*** draw the outermost ring */int center = getWidth ()/2; // obtain the x coordinate int radius = (int) of the center) (center-roundWidth/2); // circle radius paint. setColor (roundColor); // set the color of the ring to paint. setStyle (Paint. style. STROKE); // set the hollow paint. setStrokeWidth (roundWidth); // set the ring width to paint. setAntiAlias (true); // removes the Sawtooth canvas. drawCircle (center, center, radius, paint); // draw the ring Log. e ("log", cen Tre + "");/*** painting progress percentage */paint. setStrokeWidth (0); paint. setColor (textColor); paint. setTextSize (textSize); paint. setTypeface (Typeface. DEFAULT_BOLD); // set the font int percent = (int) (float) progress/(float) max) * 100); // The progress percentage in the middle, convert to float before division. Otherwise, all values are 0 float textWidth = paint. measureText (percent + "%"); // measure the font width. Set the font width to the center of the ring if (textIsDisplayable & percent! = 0 & style = STROKE) {canvas. drawText (percent + "%", center-textWidth/2, center + textSize/2, paint); // draw progress percentage}/*** draw an arc, draw the progress of the ring * // set whether the progress is solid or hollow paint. setStrokeWidth (roundWidth); // set the ring width to paint. setColor (roundProgressColor); // set the progress color RectF oval = new RectF (center-radius, center-radius, center + radius, center + radius ); // The border switch (style) {case STROKE: {paint. setStyle (Paint. sty Le. STROKE); canvas. drawArc (oval, 0,360 * progress/max, false, paint); // draw an arc break Based on the progress;} case FILL: {paint. setStyle (Paint. style. FILL_AND_STROKE); if (progress! = 0) canvas. drawArc (oval, 0,360 * progress/max, true, paint); // draw an arc break Based on the progress; }}public synchronized int getMax () {return max ;} /*** set the maximum progress value * @ param max */public synchronized void setMax (int max) {if (max <0) {throw new IllegalArgumentException ("max not less than 0");} this. max = max;}/*** get progress. you need to synchronize * @ return */public synchronized int getProgress () {return progress;}/*** to set the progress. This is a thread-safe control. Due to the multi-line problem, to synchronize the * refresh interface, call postInvalidate () to refresh the * @ param progress */public synchronized void setProgress (int progress) {if (progress <0) {throw new IllegalArgumentException ("progress not less than 0");} if (progress> max) {progress = max;} if (progress <= max) {this. progress = progress; postInvalidate () ;}} public int getCricleColor () {return roundColor;} public void setCricleColor (int cricleColor) {this. roundColor = cricleColor;} public int getCricleProgressColor () {return roundProgressColor;} public void setCricleProgressColor (int cricleProgressColor) {this. roundProgressColor = cricleProgressColor;} public int getTextColor () {return textColor;} 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 ;}}

Layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >    <com.qiao.circleprogress_forexample.RoundProgressBar        android:id="@+id/progressBar1"        android:layout_width="100dip"        android:layout_height="100dip"        /></LinearLayout>

Attribute

<?xml version="1.0" encoding="UTF-8"?><resources>    <declare-styleable name="RoundProgressBar">          <attr name="roundColor" format="color"/>        <attr name="roundProgressColor" format="color"/>        <attr name="roundWidth" 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>

 


How does java GUI customize the circular progress bar?

Overwrite the painting event, calculate the proportion by yourself, and draw slices. Or use an image to display

How to customize a circular progress bar in android

Download a desktop Assistant
Or a variety of desktop software
 

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.