Android Dynamic Change Seekbar progress bar color and slider color instance code _android

Source: Internet
Author: User


Encountered a dynamic change Seekbar progress bar color and slider color requirements, some according to different progress changes to different colors.



How do you do that? We all know set progressdrawable and thumb can, but this set is OK, to dynamic changes need to be implemented in the code.



Use Shape progress bar and slider



Seekbar settings



Dynamically set setprogressdrawable and Setthumb in code



Drawing graphics, we are more familiar with, background is a background map, secondaryprogress second progress bar, progress progress bar:


<layer-list xmlns: android = "http://schemas.android.com/apk/res/android">
<item android: id = "@ android: id / background">
<shape>
<corners android: radius = "5dip" />
<gradient
android: startColor = "@ color / gray_cc"
android: centerColor = "@ color / gray_cc"
android: centerY = "0.75"
android: endColor = "@ color / gray_cc"
android: angle = "270" />
</ shape>
</ item>
<!-I have no second background, so the second background image is not drawn->
<item android: id = "@ android: id / secondaryProgress">
<clip>
<shape>
<corners android: radius = "5dip" />
<gradient
android: startColor = "# 80ffd300"
android: centerColor = "# 80ffb600"
android: centerY = "0.75"
android: endColor = "# a0ffcb00"
android: angle = "270" />
</ shape>
</ clip>
</ item>
<item android: id = "@ android: id / progress">
<clip>
<shape>
<corners android: radius = "5dip" />
<gradient
android: startColor = "@ color / gray_cc"
android: centerColor = "@ color / gray_cc"
android: centerY = "0.75"
android: endColor = "@ color / gray_cc"
android: angle = "270" />
</ shape>
</ clip>
</ item>
</ layer-list> 


And then paint the slider:


<selector xmlns:android= "Http://schemas.android.com/apk/res/android" >
<item android:drawable= "@ Drawable/seekbar_thumb_gray "android:state_focused=" true "android:state_pressed=" true "/>
<item android: drawable= "@drawable/seekbar_thumb_gray" android:state_focused= "false" android:state_pressed= "false"/>
<item android:drawable= "@drawable/seekbar_thumb_gray" android:state_focused= "true" android:state_pressed= " False "/>
<item android:drawable=" @drawable/seekbar_thumb_gray android:state_focused= "true"/>
</selector>


In the last XML set:


android:progressdrawable= "@drawable/seekbar_light"
android:thumb= "@drawable/seekbar_thumb"


Here's a reminder that the slide block slides to 0 or the maximum may not be finished, the slider only shows half, and the background is too high, so these need to be set up:


android:maxheight= "5DP"
android:minheight= "5DP"
android:paddingleft= "10DP"
android:paddingright= " 10DP "


The above maxheight and minheight can make the background not so high, change the value to control the height.



Paddingleft and Paddingright are best to be half the width of the slider, so that they can be fully displayed.



The following goes to the point where the code dynamically sets the color.



We can use getprogressdrawable to get a progress background in our code, so we can change it.



Due to the layerdrawable, there are several levels, of course, we only painted three levels of background, not sure, you can loop to get the ID to judge:


Gets the Seerbar level drawable object
layerdrawable layerdrawable = (layerdrawable) sb.getprogressdrawable ();
How many levels (up to three)
int layers = Layerdrawable.getnumberoflayers ();
drawable[] Drawables = new Drawable[layers];
for (int i = 0; i < layers i++) {
switch (Layerdrawable.getid (i)) {
//If it is Seekbar background case
Android. R.id.background:
drawables[i] = layerdrawable.getdrawable (0);
break;
If you are dragging the first progress case
Android. R.id.progress:
//Here for the dynamic color value
drawables[i] = new paintdrawable (progresscolor);
Drawables[i].setbounds (layerdrawable.getdrawable (0). getbounds ());
break;
...
}
}
Sb.setprogressdrawable (New layerdrawable (Drawables));
Sb.setthumb (thumb);
Sb.invalidate ();


The above can get a different background, and then change the settings dynamically, the above code can complete the top of the diagram, because the background does not need to change color, so the background image does not change, and then get the progress bar background we use is paintdrawable painting color, Of course you can use the other constructs a background drawable, then set up the style that you need. The slider can also be a background picture, or it can be reset with Getthumb to the background, just as the progress background is bad.



I mentioned in the previous article that Android changed the color of the solid color background picture, not clear can point I go in to see Android change the color of a solid background picture.



Because we are also pure color here, it is better to do, simpler:


Gets the Seerbar level drawable object
layerdrawable layerdrawable = (layerdrawable) sb.getprogressdrawable ();
Because the second progress background picture is not drawn when drawing the background drawing, it is directly 1
drawable drawable = layerdrawable.getdrawable (1);
Drawable.setcolorfilter (Progresscolor, PorterDuff.Mode.SRC);
Gets the slider background
drawable thumb = sb.getthumb ();
Thumb.setcolorfilter (Thumbcolor, PorterDuff.Mode.SRC);
Sb.invalidate ();


From the above we can use the change of the pure color method to get the background color, of course, if your background image is also a solid color.



It's fun to play again.



Attention



The first scheme can draw a variety of styles, mainly to see your use of the drawable, but if the background map, if the progress of the two ends of the arc, dynamic drawing time to set an extra arc, the estimated implementation is not simple, or the background is not a circular arc.



The second scheme can be used for pure colors, and it is difficult to make sense for complex and diverse types.



The above is a small set to introduce the Android dynamic change Seekbar progress bar color and slider Color example code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!


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.