Write a progressbar with custom progress color and circular rotation (details)

Source: Internet
Author: User

First:


We need to customize the progressbar style.

<Span style = "white-space: pre"> </span> <style name = "self_define_progressbar" parent = "@ Android: style/widget. progressbar. horizontal "> // inherits the android horizontal progressbar style <item name =" Android: indeterminateonly "> false </item> <item name =" Android: progressdrawable "> @ drawable/self_define_progress </item> </style>

<? XML version = "1.0" encoding = "UTF-8"?> <Layer-list xmlns: Android = "http://schemas.android.com/apk/res/android"> <item Android: Id = "@ Android: ID/background"> // defines the background color <shape> <corners Android: radius = "5dip"/> <gradient Android: angle = "270" Android: centercolor = "# efefef" Android: centery = "0.75" Android: endcolor = "# a3a3a3" Android: startcolor = "# c7c7c7"/> </shape> </item> <item Android: Id = "@ Android: ID/secondaryprogress "> // if there are two levels of progress, you need to define this item. Gradient defines gradient, and clip does not, the progress bar is filled with the whole progpolicar <clip> <shape> <corners Android: radius = "5dip"/> <gradient Android: angle = "270" Android: centercolor = "#99cc99" Android: centery = "0.75" Android: endcolor = "#99cc99" Android: startcolor = "#99cc99"/> </shape> </clip> </item> <item Android: Id = "@ Android: ID/progress "> // This defines the level 1 progress. If the level 1 Progress is greater than level 2 and the Level 1 progress is not transparent, you can overwrite the 2-level progress <clip> <shape> <corners Android: radius = "5dip"/> <gradient Android: angle = "270" Android: centercolor = "#4 abbfd" Android: centery = "0.75" Android: endcolor = "#4 abbfd" Android: startcolor = "#4 abbfd"/> </shape> </clip> </item> </layer-List>


Use this in the Code:

private ProgressBar mProgressBar;private Task mTask = new Task();private int mCount = 0;private Handler mHander = new MyHandler();@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);mProgressBar = (ProgressBar)findViewById(R.id.progress);Timer timer = new Timer();timer.schedule(mTask, 1000, 1000);}private class MyHandler extends Handler{@Overridepublic void handleMessage(Message msg) {mCount += 10;mProgressBar.setProgress(mCount);super.handleMessage(msg);}}private class Task extends TimerTask{@Overridepublic void run() {mHander.sendEmptyMessage(10);}}



Progress bar for a circle with a circle Animation: Value of shape (rectagle rectangle, oval, line horizontal line, ring) value of tpye: gradient style liner linear gradient Radial Ring gradient sweep

<? XML version = "1.0" encoding = "UTF-8"?> <Rotate xmlns: Android = "http://schemas.android.com/apk/res/android" Android: fromdegrees = "0" Android: fromdegrees = "0" Android: todegrees = "360"> <shape Android: Shape = "ring" <span style = "font-family: verdana, MS song,, Arial,, Helvetica, sans-serif; color: #666666; "> <span style =" font-size: 14px; line-Height: 18px; "> </span> Android: innerradiusratio = "3" Android: thicknessratio = "8" Android: uselevel = "false"> <size Android: width = "37dip" Android: height = "37dip"/> <gradient Android: TYPE = "sweep" // if the type is sweep, the Android: uselevel = "false" Android: startcolor = "# f7f7f7" Android: centercolor = "# f7f7f7" Android: centery = "0.50" Android: endcolor = "# b3b4b4"/> </shape> </rotate>



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.