Some time did not come to write, next continue to share the girl wrote the level progress bar, hope to help beginners ~ ~ ~
mainactivity Class
Package Com.lanzx.customprogressbar;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.os.Handler;
Import Android.os.Message;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.widget.TextView;
public class
mainactivityExtends Activity {
Private TextView Textnumber;
private int num;
private int progress=0;
Private message message;
Customprogressbar Bar;
Private Handler handler=new Handler () {
@Override
public void Handlemessage (Message msg) {
Super.handlemessage (msg);
int p=msg.what;
Mpbar.setprogress (P);
Bar.setprogress (P);
}
};
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Textnumber = (TextView) Findviewbyid (R.id.textnumber);
num = (int) (Math.random () *100);
Textnumber.settext (string.valueof (num+ "/100"). toString ());
Get a custom progress bar
Bar = (Customprogressbar) Findviewbyid (R.id.item_progress_seekbar);
Set the maximum value of the progress bar
Bar.setmax (100);
Set Progress values
Bar.setprogress (60);
Open Thread
New Thread (yuanlirunnable). Start ();
}
Runnable yuanlirunnable=new Runnable () {
@Override
public void Run () {
Message=handler.obtainmessage ();
try {
for (int i = 0; I <=; i++) {
/*
* Control the progress here
*/
int x=++progress;
int randomnumberprogress= (int) (Math.random () *100);
int x=randomnumberprogress;
int x=num;
Message.what=x;
Handler.sendemptymessage (Message.what);
Thread.Sleep (100);
}
} catch (Interruptedexception e) {
E.printstacktrace ();
}
}
};
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Getmenuinflater (). Inflate (R.menu.main, menu);
return true;
}
@Override
public boolean onoptionsitemselected (MenuItem item) {
int id = item.getitemid ();
if (id = = r.id.action_settings) {
return true;
}
return super.onoptionsitemselected (item);
}
}
Customprogressbar class
Package Com.lanzx.customprogressbar;
Import Android.content.Context;
Import Android.graphics.drawable.shapes.RoundRectShape;
Import Android.graphics.drawable.shapes.Shape;
Import Android.util.AttributeSet;
Import Android.widget.ProgressBar;
public classCustomprogressbarExtends ProgressBar {
Public Customprogressbar (Context context) {
Super (context);
}
Public Customprogressbar (context context, AttributeSet Attrs) {
This (context, attrs, 0);
}
Public Customprogressbar (context context, AttributeSet attrs, int defstyle) {
This (context, attrs, defstyle,0);
}
Public Customprogressbar (context context, AttributeSet attrs, int defstyle, int styleres) {
Super (context, attrs, Defstyle);
}
@Override
protected synchronized void onmeasure (int widthmeasurespec,int heightmeasurespec) {
Super.onmeasure (Widthmeasurespec, Heightmeasurespec);
}
progress bar Circle Angle
public final int roundcorners = 15;
Shape Getdrawableshape () {
Final float[] roundedcorners = new float[] {0, 0, 0, 0, 0, 0, 0, 0};
for (int i=0;i<roundedcorners.length;i++) {
Roundedcorners[i] = dp2px (GetContext (), roundcorners);
}
return new Roundrectshape (roundedcorners, NULL, NULL);
}
/**DP conversion into px*/
public static float dp2px (context context, float DP) {
Final float scale = context.getresources (). Getdisplaymetrics (). density;
return DP * SCALE;
}
}
activity_main.xml Layout
<relativelayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
Android:layout_width= "Match_parent"
android:layout_height= "Match_parent"
Android:background= "@android: Color/black"
android:paddingbottom= "@dimen/activity_vertical_margin"
android:paddingleft= "@dimen/activity_horizontal_margin"
android:paddingright= "@dimen/activity_horizontal_margin"
android:paddingtop= "@dimen/activity_vertical_margin"
tools:context= "Com.lanzx.customprogressbar.MainActivity" >
<com.lanzx.customprogressbar.customprogressbar
Android:id= "@+id/item_progress_seekbar"
Style= "? Android:attr/progressbarstylehorizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "30DP"
android:layout_marginleft= "30DP"
android:layout_marginright= "30DP"
android:layout_margintop= "100DP"
Android:indeterminateonly= "false"
android:max= "100"
android:progress= "60"
android:progressdrawable= "@drawable/progressbar_drawable"
android:visibility= "Visible"/>
<textview
Android:id= "@+id/textnumber"
Android:layout_width= "Wrap_content"
android:layout_height= "20dip"
Android:layout_alignbottom= "@+id/item_progress_seekbar"
android:layout_alignright= "@+id/item_progress_seekbar"
Android:layout_marginbottom= "40DP"
android:text= "/100"
Android:textcolor= "#FFFFFF"
Android:textsize= "15dip"/>
</RelativeLayout>
Progressbar_drawable.xml (in drawable file)
<?xml version= "1.0" encoding= "UTF-8"?>
<layer-list xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:id= "@android: Id/background" >
<shape>
<corners
Android:bottomleftradius= "15DP"
Android:bottomrightradius= "15DP"
Android:topleftradius= "15DP"
android:toprightradius= "15DP"/>
<solid android:color= "#C9C7C7"/> <!--#bfbfbf Here is the color of the progress bar--
<stroke
Android:dashwidth= "2dip"
Android:width= "2dip"
Android:color= "#79E911"/><!--strokes--
</shape>
</item>
<!--fill progress color--
<item
Android:id= "@android: Id/progress"
android:drawable= "@drawable/red" >
</item>
</layer-list>
Main.xml (in the menu file)
<menu xmlns:android= "Http://schemas.android.com/apk/res/android"
Xmlns:tools= "Http://schemas.android.com/tools"
tools:context= "Com.lanzx.customprogressbar.MainActivity" >
<item
Android:id= "@+id/action_settings"
android:orderincategory= "100"
Android:showasaction= "Never"
android:title= "@string/action_settings"/>
</menu>
As follows:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Horizontal progress bar ProgressBar