我想更改进度条的渐变颜色。现在,我可以将其更改为纯色,但我无法将其更改为渐变色。
LayerDrawable progressDrawable = (LayerDrawable) myProgressBar.getProgressDrawable();
// get the color drawable of the progress bar
Drawable primaryColor = progressDrawable.getDrawable(1);
// set the new color.
primaryColor.setColorFilter(ContextCompat.getColor(getContext(), R.color.yellow), PorterDuff.Mode.SRC_OVER);
我想改变渐变色而不是纯色。
编辑我的可绘制文件
<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/gray300"
android:centerColor="@color/gray400"
android:centerY="0.75"
android:endColor="@color/gray500"
android:angle="270"
/>
</shape>
</item>
<item
android:id="@+id/progress">
<clip>
<shape>
<corners
android:radius="5dip" />
**I want to change this gradient color**
<gradient
android:startColor="#33FF33"
android:endColor="#008000"
android:angle="270" />
</shape>
</clip>
</item>
</layer-list>
答案 0 :(得分:1)
试试这个
GradientDrawable bgShape = (GradientDrawable) myProgressBar.getBackground().getCurrent();
bgShape.setColor(Color.YELLOW);
答案 1 :(得分:0)
这是我的实现:绘制一个动画彩虹渐变。很酷,如果我自己这么说的话。它不是基于XML的,但可以做成,并且它确实使用渐变。也许它会给你一些想法。
设定:
pb = (ProgressBar) findViewById(R.id.progressbar_Horizontal);
GradientDrawable rainbow = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[] {Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED});
AnimationDrawable ad = getProgressBarAnimation();
pb.setBackgroundDrawable(ad);
动画:
private AnimationDrawable getProgressBarAnimation(){
GradientDrawable rainbow1 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[] {Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW});
GradientDrawable rainbow2 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[] { Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN});
GradientDrawable rainbow3 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[] { Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE, Color.CYAN });
GradientDrawable rainbow4 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[] { Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA, Color.BLUE });
GradientDrawable rainbow5 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[] { Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED, Color.MAGENTA });
GradientDrawable rainbow6 = new GradientDrawable(Orientation.LEFT_RIGHT,
new int[] {Color.MAGENTA, Color.BLUE, Color.CYAN, Color.GREEN, Color.YELLOW, Color.RED });
GradientDrawable[] gds = new GradientDrawable[] {rainbow1, rainbow2, rainbow3, rainbow4, rainbow5, rainbow6};
AnimationDrawable animation = new AnimationDrawable();
for (GradientDrawable gd : gds){
animation.addFrame(gd, 100);
}
animation.setOneShot(false);
return animation;
}