以编程方式切换进度条的颜色

Mic*_*ele 5 android android-layout android-xml

所以我有一个图层列表,其中的项目具有形状和纯色.现在我想在我的代码中更改此颜色.

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
        <clip>
            <shape android:id="@+id/seekbar_shape_color">
                <solid android:color="#FFD00000" />
            </shape>
        </clip>

    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

我尝试获取LayerDrawable并请求第一个项目,现在iam卡住获取clipDrawable并最终得到ShapeDrawable

LayerDrawable sd = (LayerDrawable) v.getResources().getDrawable(R.drawable.seekbar_progress_bg);
((ClipDrawable) sd.getDrawable(0)).get..();
Run Code Online (Sandbox Code Playgroud)

我的另一种方法是获取ShapeDrawable并使用colorFilter设置颜色,但这也不起作用.

ShapeDrawable sd = (ShapeDrawable) v.getResources().getDrawable(R.id.seekbar_shape_color);
sd.setColorFilter(R.color.cold_color,PorterDuff.Mode.MULTIPLY);
Run Code Online (Sandbox Code Playgroud)

编辑:好吧所以我开发了一个新的计划,我试图使用LevelList

 <level-list
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:maxLevel="0" android:drawable="@color/hot_color">
    </item>
    <item android:maxLevel="1" android:drawable="@color/cold_color">
    </item>
</level-list>
Run Code Online (Sandbox Code Playgroud)

我认为Level的默认值是0但我没有使用LevelList显示进度条.另外我不知道我必须在哪个元素上设置setLevel()

        holder.progressbar.getBackground().setLevel(0);
        holder.progressbar.getIndeterminateDrawable().setLevel(0);
        holder.progressbar.getProgressDrawable().setLevel(0);
Run Code Online (Sandbox Code Playgroud)

这个调用给我奇怪的结果,就像只有新的进度条项目获得100%进展着色,闻起来像缓存问题.

希望有人可以告诉我如何在LayerList上正确使用LevelList以获得进度条感谢

Mat*_*lfe 16

我知道这个问题已经有几个月了,但我能够获得一个使用进度条的级别列表.诀窍是将您的等级值分配在0到10,000之间.在我的特定示例中,我有3个级别(红色/黄色/绿色)用于表示电池寿命.

我是这样做的:

RES /抽拉/ progress_horizo​​ntal_yellow.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="5dip" />
            <gradient
                    android:startColor="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    android:endColor="#ff747674"
                    android:angle="270"
            />
        </shape>
    </item>

    <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="#ffffd300"
                        android:centerColor="#ffffb600"
                        android:centerY="0.75"
                        android:endColor="#ffffcb00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

RES /抽拉/ progress_horizo​​ntal_red.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="5dip" />
            <gradient
                    android:startColor="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    android:endColor="#ff747674"
                    android:angle="270"
            />
        </shape>
    </item>

    <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="#ffff0000"
                        android:centerColor="#ff990000"
                        android:centerY="0.75"
                        android:endColor="#ffff0000"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>

</layer-list>
Run Code Online (Sandbox Code Playgroud)

RES /抽拉/ progress_horizo​​ntal_green.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="5dip" />
            <gradient
                    android:startColor="#ff9d9e9d"
                    android:centerColor="#ff5a5d5a"
                    android:centerY="0.75"
                    android:endColor="#ff747674"
                    android:angle="270"
            />
        </shape>
    </item>

    <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="#ff00ff00"
                        android:centerColor="#ff009900"
                        android:centerY="0.75"
                        android:endColor="#ff00ff00"
                        android:angle="270"
                />
            </shape>
        </clip>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

RES /抽拉/ battery_charge_fill.xml

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:maxLevel="2999" android:drawable="@drawable/progress_horizontal_red" />
    <item android:maxLevel="4999" android:drawable="@drawable/progress_horizontal_yellow" />
    <item android:maxLevel="10000" android:drawable="@drawable/progress_horizontal_green" />
</level-list>
Run Code Online (Sandbox Code Playgroud)

然后在布局文件中我的进度条声明是这样的:

<ProgressBar android:max="100" 
    android:progress="60"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:id="@+id/BatteryLevelProgress"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:progressDrawable="@drawable/battery_charge_fill"   />
Run Code Online (Sandbox Code Playgroud)

然后在java中:

//progress is between 0 and 100 so set level of drawable to progress * 100
Drawable batteryProgressD = batteryProgressBar.getProgressDrawable();
batteryProgressD.setLevel(progress*100);
batteryProgressBar.setProgress(progress);
Run Code Online (Sandbox Code Playgroud)