以编程方式切换进度条的颜色 [英] Switching Color of a Progressbar programmatically

查看:57
本文介绍了以编程方式切换进度条的颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

so i have a Layer-List with an Item with a shape and a solid color. Now i want to change this color inside my code.

<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>

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

i tryed getting the LayerDrawable and requesting the first item, now iam stuck getting the clipDrawable and finally get the ShapeDrawable

LayerDrawable sd = (LayerDrawable) v.getResources().getDrawable(R.drawable.seekbar_progress_bg);
((ClipDrawable) sd.getDrawable(0)).get..();

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

my other approach was to get the ShapeDrawable and setting the color with colorFilter but that doesnt work either.

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


编辑:好,所以我制定了一个新计划 我试图使用像这样的LevelList


edit: ok so i developed a new plan i tryed to use a LevelList like

 <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>

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

i think the default value for Level is 0 but i dont get the progressbar displayed with the LevelList. Also i dont know on which element i have to set the setLevel()

        holder.progressbar.getBackground().setLevel(0);
        holder.progressbar.getIndeterminateDrawable().setLevel(0);
        holder.progressbar.getProgressDrawable().setLevel(0);

这给我带来了奇怪的结果,例如只有新鲜的进度条项目才获得100%的进度着色,闻起来像是缓存问题.

this calls give me strange results like only fresh progressbar items getting 100% progress coloration, smells like caching problem.

希望有人能启发我如何正确使用ProgressList的LayerList上的LevelList 谢谢

hope somebody can enlighten me how to use the LevelList properly on an LayerList for a Progressbar thanks

推荐答案

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

I know this question is a few months old but I was able to get a level-list working with a progress bar. The trick is to assign your levels values between 0 and 10,000 In my particular example I had 3 levels (red/yellow/green) used to represent battery life.

这是我的做法:

res/drawable/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>

res/drawable/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>

res/drawable/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>

res/drawable/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>

然后在布局文件中,我的进度条声明如下:

Then in the layout file my progress bar declaration was like this:

<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"   />

然后用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);

这篇关于以编程方式切换进度条的颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆