Custom progress bar (round, horizontal progress bar)

Source: Internet
Author: User
Tags getcolor

Customize the progress bar to achieve a general flow
1. Custom attribute declaration (attrs file)
2. Custom Attribute Acquisition
3. Measurement (onmeasure)
4. Drawing (OnDraw)

Code:

Attrs file:

  

<!--Custom declaration--
<attr name= "Progress_unreach_color" format= "Color" ></attr>
<attr name= "Progress_unreach_height" format= "Dimension" ></attr>
<attr name= "Progress_reach_color" format= "Color" ></attr>
<attr name= "Progress_reach_height" format= "Dimension" ></attr>
<attr name= "Progress_text_color" format= "Color" ></attr>
<attr name= "Progress_text_size" format= "Dimension" ></attr>
<attr name= "Progress_text_offset" format= "Dimension" ></attr>
<!--Custom Use-
<declare-styleable name= "Progressbarview" >
<attr name= "Progress_unreach_color" ></attr>
<attr name= "Progress_unreach_height" ></attr>
<attr name= "Progress_reach_color" ></attr>
<attr name= "Progress_reach_height" ></attr>
<attr name= "Progress_text_color" ></attr>
<attr name= "Progress_text_size" ></attr>
<attr name= "Progress_text_offset" ></attr>
</declare-styleable>

Layout file:

<scrollview xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Xmlns:hyman= "Http://schemas.android.com/apk/res/com.Horizontal.horizontalprogressbar"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent" >

<linearlayout
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:orientation= "Vertical" >

<com. Horizontal.view.ProgressbarView
Android:id= "@+id/progress01"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
android:layout_margintop= "30dip"
Hyman:progress_reach_color= "#f00"
Hyman:progress_text_color= "#f00"
Hyman:progress_unreach_color= "#000"
Android:background= "#d1d3d7"
Android:padding= "10dip"
android:progress= "0"/>

<com. Horizontal.view.RoundProgressbar
Android:id= "@+id/progress02"
Android:layout_width= "Match_parent"
android:layout_height= "Wrap_content"
Hyman:progress_reach_color= "#f3d4e3"
Hyman:progress_text_color= "#f00"
Hyman:progress_unreach_color= "#000"
android:layout_margintop= "30DP"
hyman:rudio= "80DP"
Android:padding= "10dip"
android:progress= "0"
/>
</LinearLayout>

</ScrollView>

Progressbarview file:

Package com. Horizontal.view;

Import com. HORIZONTAL.HORIZONTALPROGRESSBAR.R;

Import Android.content.Context;
Import Android.content.res.TypedArray;
Import Android.graphics.Canvas;
Import Android.graphics.Paint;
Import Android.graphics.PaintFlagsDrawFilter;
Import Android.util.AttributeSet;
Import Android.util.TypedValue;
Import Android.widget.ProgressBar;

public class Progressbarview extends ProgressBar {
private static final int default_text_size = 10;//font Size SP
private static final int default_text_color = 0XFC00D1;
private static final int default_reach_height = 3;//Left progress bar height DP
private static final int default_reach_color = Default_text_color;
private static final int default_unreach_height = 2;//Right progress bar height DP
private static final int default_unreach_color = 0XD3D6D1;
private static final int default_text_offset = 10;//The distance between the progress bar and the text DP

protected int mtextsize = sp2px (default_text_size);
protected int mtextcolor = Default_text_color;
protected int mreachheight = dp2px (default_reach_height);
protected int mreachcolor = Default_reach_color;
protected int munreachheight = dp2px (default_unreach_height);
protected int munreachcolor = Default_unreach_color;
protected int mtextoffset = dp2px (Default_text_offset);

protected Paint Mpaint = new paint ();
Width of the control
private int mrealwidth;

Public Progressbarview (Context context) {
This (context, NULL);
}

Public Progressbarview (context context, AttributeSet Attrs) {
This (context, attrs, 0);
}

Public Progressbarview (context context, AttributeSet attrs, int defstyle) {
Super (context, attrs, Defstyle);
Obtainstyledattrs (ATTRS);
}

/** Gets the custom property **/
private void Obtainstyledattrs (AttributeSet attrs) {
//Get attr in Styleable file (Progressbarview)
TypedArray ta = GetContext (). Obtainstyledattributes (Attrs,
R.styleable.progressbarview);
Get custom properties and default values
Mtextsize = (int) ta.getdimension (
R.styleable.progressbarview_progress_text_size, mtextsize);
Mtextcolor = Ta.getcolor (
R.styleable.progressbarview_progress_text_color, mtextcolor);
Mreachheight = (int) Ta
. Getdimension (
R.styleable.progressbarview_progress_reach_height,
Mreachheight);
Mreachcolor = Ta.getcolor (
R.styleable.progressbarview_progress_reach_color, mreachcolor);
Munreachheight = (int) ta.getdimension (
R.styleable.progressbarview_progress_unreach_height,
MUnReachHeight );
Munreachcolor = Ta.getcolor (
R.styleable.progressbarview_progress_unreach_color,
Munreachcolor);
Mtextoffset = (int) ta.getdimension (
R.styleable.progressbarview_progress_text_offset, mtextoffset);

Ta.recycle ();
Mpaint.settextsize (mtextsize);
}

/** Measurement of width and height **/
@Override
protected synchronized void onmeasure (int widthmeasurespec,
int Heightmeasurespec) {
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
Take the width of the pattern
int widthmode = Measurespec.getmode (Widthmeasurespec);
The value of width (horizontal precision bar to give definite value, so do not need to judge)
int widthval = measurespec.getsize (Widthmeasurespec);
int heigth = Measurespecheigth (Heightmeasurespec);
Setmeasureddimension (Widthval, heigth);
The width of the actual painting
Mrealwidth = Getmeasuredwidth ()-Getpaddingleft ()-getpaddingright ();
}

/** Height Measurement **/
private int measurespecheigth (int heightmeasurespec) {
int result = 0;
int mode = Measurespec.getmode (HEIGHTMEASURESPEC);
int size = Measurespec.getsize (HEIGHTMEASURESPEC);
The exactly parent node requires that the size of its child nodes be specified as an exact value. Its child nodes and other descendants need to be adapted to that size.
if (mode = = measurespec.exactly) {
result = size;
} else {
int textheigth = (int) (Mpaint.descent ()-mpaint.ascent ());
result = Getpaddingtop ()
+ Getpaddingbottom ()
+ Math.max (Math.max (Mreachheight, Munreachheight),
Math.Abs (textheigth));
The At_most parent node requires that its child nodes not be larger than a certain maximum, and that the size of its child nodes and other descendants must be less than this value
if (mode = = Measurespec.at_most) {
result = Math.min (result, size);
}
}

return result;
}

/** Drawing Controls **/
@Override
protected synchronized void OnDraw (canvas canvas) {
Super.ondraw (canvas);
Canvas.save ();
Anti-aliasing
Canvas.setdrawfilter (New Paintflagsdrawfilter (0, Paint.anti_alias_flag
| Paint.filter_bitmap_flag));
Set the origin of the canvas
Canvas.translate (Getpaddingleft (), GetHeight ()/2);
/draw Reach
Boolean Noneedunreach = false;
String text = getprogress () + "%";
int textWidth = (int) mpaint.measuretext (text);
FLOAT radio = getprogress () * 1.0f/getmax ();
FLOAT PROGRESSX = Radio * MREALWIDTH;
if (TextWidth + progressx > Mrealwidth) {
PROGRESSX = Mrealwidth-textwidth;
Noneedunreach = true;
}

float endx = PROGRESSX-MTEXTOFFSET/2;
if (EndX > 0) {
Mpaint.setcolor (Mreachcolor);
Mpaint.setstrokewidth (Mreachheight);
Canvas.drawline (0, 0, endx, 0, Mpaint);
}

Draw text
Mpaint.setcolor (Mtextcolor);
int y = (int) (-(Mpaint.descent () + mpaint.ascent ())/2);
Canvas.drawtext (text, progressx, y, mpaint);
Draw Unreach Bar
if (!noneedunreach) {
float start = progressx + MTEXTOFFSET/2 + textWidth;
Mpaint.setcolor (Munreachcolor);
Mpaint.setstrokewidth (Munreachheight);
Canvas.drawline (Start, 0, mrealwidth, 0, Mpaint);
}

Canvas.restore ();
}

/** dp turn PX **/
protected int dp2px (int dpval) {
return (int) typedvalue.applydimension (Typedvalue.complex_unit_dip,
Dpval, Getresources (). Getdisplaymetrics ());
}

/** sp turn px **/
protected int sp2px (int spval) {
return (int) typedvalue.applydimension (typedvalue.complex_unit_sp,
Spval, Getresources (). Getdisplaymetrics ());
}
}

Mainactivity:

Package com. Horizontal.horizontalprogressbar;

Import com. Horizontal.view.ProgressbarView;
Import com. Horizontal.view.RoundProgressbar;

Import Android.os.Bundle;
Import Android.os.Handler;
Import android.app.Activity;
Import Android.view.Menu;

public class Mainactivity extends Activity {
Private Progressbarview Progressbarview;
Private Roundprogressbar Roundprogressbar;
private static final int msg_update = 0x110;

@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Progressbarview = (Progressbarview) Findviewbyid (R.ID.PROGRESS01);
Roundprogressbar= (Roundprogressbar) Findviewbyid (R.ID.PROGRESS02);
Handler.sendemptymessage (msg_update);
}

Private Handler Handler = new Handler () {
public void Handlemessage (Android.os.Message msg) {
int progress = progressbarview.getprogress ();
Roundprogressbar.setprogress (++progress);
Progressbarview.setprogress (++progress);
if (Progress >= 100) {
Handler.removemessages (msg_update);
}
Handler.sendemptymessagedelayed (msg_update, 100);
};
};

}

SOURCE Download: Link: http://pan.baidu.com/s/1hrByCV6 Password: whs7

Custom progress bar (round, horizontal progress bar)

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.