【发布时间】:2017-09-14 10:54:59
【问题描述】:
我想根据进度条的值更改进度条的颜色。我有一个在 XML 中创建的渐变背景。
<?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="5dp"/>
<gradient
android:endColor="#55850000"<!-- green -->
android:centerColor="#55e6ff00" <!-- yellow -->
android:startColor="#5500c872" <!-- red -->
/>
</shape>
</item>
<item android:id="@android:id/progress">
<clip>
<shape>
<corners android:radius="5dp"/>
<gradient
android:endColor="#c20600"<!-- green %100 alpha -->
android:centerColor="#e6ff00"<!-- yellow %100 alpha-->
android:startColor="#00c872"<!-- red %100 alpha -->
/>
</shape>
</clip>
</item>
</layer-list>
我的问题是这样的,我想根据相同的背景颜色来改变整个进度的颜色。假设我的进度到了%60,整个进度条应该是黄色的。如果我的进度达到 %90,整个进度条应该是红色的。它应该是动态变化的,并且应该与进度的背景颜色相同。
这是我使用的动画颜色,它不起作用!
public class ProgressBarAnimation extends Animation{
private ProgressBar progressBar;
private float from;
private float to;
public ProgressBarAnimation(ProgressBar progressBar, float from, float to) {
super();
this.progressBar = progressBar;
this.from = from;
this.to = to;
}
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
super.applyTransformation(interpolatedTime, t);
float value = from + (to - from) * interpolatedTime;
/*if(value < 25){
progressBar.getIndeterminateDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.MULTIPLY);
}else if(value <=50){
progressBar.getIndeterminateDrawable().setColorFilter(Color.YELLOW, PorterDuff.Mode.MULTIPLY);
}else{
progressBar.getIndeterminateDrawable().setColorFilter(Color.RED, PorterDuff.Mode.MULTIPLY);
}*/
// NEEDS TO CHANGE COLOR IN HERE !
progressBar.setProgress((int) value);
}
}
【问题讨论】:
标签: java android android-view