Customizing the horizontal progress bar

Source: Internet
Author: User

That is true:

Next, paste the code for everyone:

The first is the custom ProgressBar:

public class Myprogress extends ProgressBar {

String text;

Paint Mpaint;


Public myprogress (Context context) {

Super (context);

TODO auto-generated Constructor stub

System.out.println ("1");

Inittext ();

}


Public myprogress (context context, AttributeSet attrs, int defstyle) {

Super (context, attrs, Defstyle);

TODO auto-generated Constructor stub

System.out.println ("2");

Inittext ();


}


Public myprogress (context context, AttributeSet Attrs) {

Super (context, attrs);

TODO auto-generated Constructor stub

System.out.println ("3");

Inittext ();

}


@Override

Public synchronized void setprogress (int progress) {

TODO auto-generated Method Stub

SetText (progress);


Super.setprogress (progress);


}


@Override

protected synchronized void OnDraw (canvas canvas) {

TODO auto-generated Method Stub

Super.ondraw (canvas);

This.settext ();

Rect rect = new rect ();

This.mPaint.getTextBounds (this.text, 0, This.text.length (), rect);


int x = (getwidth ()/1)-Rect.centerx ()-18;

int y = (getheight ()/2)-rect.centery ();

Canvas.drawtext (This.text, x, Y, this.mpaint);

}


Initialize, brush

private void Inittext () {

This.mpaint = new Paint ();

This.mPaint.setColor (Color.White);


}


private void SetText () {

SetText (This.getprogress ());


}


Set text content

private void SetText (int progress) {

int i = (Progress * +)/This.getmax ();

This.text = string.valueof (i) + "%";


}


}


The other is the code inside the actiivty:

public class Progresstest extends Activity {


Private Button btn_go = null;

Private myprogress myprogress = null;

Private Handler Mhandler;

Public TextView Baifen;


/** called when the activity is first created. */

@Override

public void OnCreate (Bundle savedinstancestate) {

Super.oncreate (savedinstancestate);

Setcontentview (R.layout.main);

Findview ();

SetParam ();

AddListener ();


Mhandler = new Handler (new Callback () {


@Override

public boolean handlemessage (Message msg) {

TODO auto-generated Method Stub

Myprogress.setprogress (Msg.what);

return false;

}

});


}


private void Findview () {

Btn_go = (Button) Findviewbyid (R.ID.BTN_GO);

Myprogress = (myprogress) Findviewbyid (R.id.pgsbar);

Baifen = (TextView) Findviewbyid (R.id.baifen);

}


private void SetParam () {

Btn_go.settext ("Start");

}


private void AddListener () {

Btn_go.setonclicklistener (New Onclicklistener () {


@Override

public void OnClick (View v) {

TODO auto-generated Method Stub

New Thread (New Runnable () {

@Override

public void Run () {

TODO auto-generated Method Stub

for (int i = 0; I <=; i++) {

Mhandler.sendemptymessage (i * 2);

try {

Thread.Sleep (100);

} catch (Interruptedexception e) {

TODO auto-generated Catch block

E.printstacktrace ();

}

}

}

}). Start ();

}

});

}

}

There is a need to create a drawable folder under the Res folder; *****.xml, the code is as follows:

<?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:radius= "3dip"/>


<gradient

Android:angle= "270"

android:centery= "0.75"

Android:endcolor= "#999999"

Android:startcolor= "#999999"/>

</shape>

</item>

<item android:id= "@android: Id/secondaryprogress" >

<clip>

<shape>

<corners android:radius= "0dip"/>


<gradient

Android:angle= "270"

android:centery= "0.75"

Android:endcolor= "#23b7a1"

Android:startcolor= "#23b7a1"/>

</shape>

</clip>

</item>

<item android:id= "@android: Id/progress" >

<clip>

<shape>

<corners android:radius= "5dip"/>


<gradient

Android:angle= "270"

android:centery= "0.75"

Android:endcolor= "#23b7a1"

Android:startcolor= "#23b7a1"/>

</shape>

</clip>

</item>


</layer-list>


Under the Values folder, add the code stytles.xml:

<!--custom progress bar--

<style name= "Progressbar_mini" parent= "@android: Style/widget.progressbar.horizontal" >

<item name= "Android:maxheight" >50dip</item>

<item name= "Android:minheight" >12dip</item>

<item name= "Android:indeterminateonly" >false</item>

<item name= "android:indeterminatedrawable" > @android:d Rawable/progress_indeterminate_horizontal</item >

<item name= "android:progressdrawable" > @drawable/progressbar_mini</item>

</style>



Basic all the code I have pasted here, there will be some color values, this you can define your own.

If useful, not clear how to write, you can add me qq:2268214831 or: hanxinghui0817 private chat Me Oh

I have insufficient experience, if there is any mistake also ask the great God a lot of advice, here thank you! Hope this blog to help some people cry!


Customizing the 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.