更改onProgressChanged一个搜索栏的颜色 [英] Change the color of a seekbar on onProgressChanged
问题描述
我有自定义搜索栏,我可以通过设置新的进展绘制更改onProgressChanged的颜色:
seek.setProgressDrawable(res.getDrawable(R.drawable.seekbar_bg1));
但我想一个搜索栏至极有着坚实的颜色从绿色变为红色视进展情况。 有没有办法使用像一个渐变颜色,所以我不需要像创造100可绘制呢?
< XML版本=1.0编码=UTF-8&GT?;
<层列表的xmlns:机器人=http://schemas.android.com/apk/res/android>
<项目机器人:ID =@机器人:ID /背景>
<形状>
[固体机器人:颜色=@机器人:彩色/透明/>
< /形状>
< /项目>
<项目机器人:ID =@ + ID / progressshape>
<夹>
<形状>
[固体机器人:颜色=@色/ custom_yellow/>
< /形状>
< /剪辑>
< /项目>
< /层列表>
对于那些谁感兴趣的解决方案。我用下面的code:
此方法来设置颜色:
公共无效setProgressBarColor(INT newColor){
LayerDrawable LD =(LayerDrawable)getProgressDrawable();
ClipDrawable D1 =(ClipDrawable)ld.findDrawableByLayerId(R.id.progressshape);
d1.setColorFilter(newColor,PorterDuff.Mode.SRC_IN);
}
和在onProgressChanged:
如果(进步与LT = 50){
seek.setProgressBarColor(Color.rgb(255 - (百分之二百五十五*(100 - 进展* 2)),255,0));
}其他{
seek.setProgressBarColor(Color.rgb(255,255 - (百分之二百五十五*(进展 - 50)* 2),0));
}
我做这样的事情用这个方法:
公共无效setProgressBarColor(进度进度,INT newColor){
LayerDrawable LD =(LayerDrawable)progressBar.getProgressDrawable();
ClipDrawable D1 =(ClipDrawable)ld.findDrawableByLayerId(R.id.progressshape);
d1.setColorFilter(newColor,PorterDuff.Mode.SRC_IN);
}
然后,当你更新进度,你只需要改变所需的颜色。
I have custom seekbar and i can change the the color in the onProgressChanged by setting a new progress drawable:
seek.setProgressDrawable(res.getDrawable(R.drawable.seekbar_bg1));
But i would like a seekbar wich has a solid color from green to red depending on the progress. Is there a way to use something like a gradient color so i dont need to create like 100 drawables this?
<?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>
<solid android:color="@android:color/transparent" />
</shape>
</item>
<item android:id="@+id/progressshape">
<clip>
<shape>
<solid android:color="@color/custom_yellow" />
</shape>
</clip>
</item>
</layer-list>
For those who are interested in the solution. I use the following code:
this method to set the color:
public void setProgressBarColor(int newColor){
LayerDrawable ld = (LayerDrawable) getProgressDrawable();
ClipDrawable d1 = (ClipDrawable) ld.findDrawableByLayerId(R.id.progressshape);
d1.setColorFilter(newColor, PorterDuff.Mode.SRC_IN);
}
and in the onProgressChanged:
if(progress <= 50){
seek.setProgressBarColor(Color.rgb( 255 - (255/100 * (100 - progress*2)), 255, 0));
}else{
seek.setProgressBarColor(Color.rgb( 255, 255 - (255/100 * (progress - 50)*2), 0));
}
I do something like this using this method:
public void setProgressBarColor(ProgressBar progressBar, int newColor){
LayerDrawable ld = (LayerDrawable) progressBar.getProgressDrawable();
ClipDrawable d1 = (ClipDrawable) ld.findDrawableByLayerId(R.id.progressshape);
d1.setColorFilter(newColor, PorterDuff.Mode.SRC_IN);
}
Then, when you update the progress, you just have to change the desired color.
这篇关于更改onProgressChanged一个搜索栏的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!