动态修改progress bar的颜色

原创
2014/09/12 18:07
阅读数 1.7K

1,增加三个文件

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

2、根据需求设定不同颜色对应的阈值:

res/drawable/lifebar.xml

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:maxLevel="5000" android:drawable="@drawable/progress_horizontal_green" />
    <item android:maxLevel="8000" android:drawable="@drawable/progress_horizontal_yellow" />
    <item android:maxLevel="10000" android:drawable="@drawable/progress_horizontal_red" />
</level-list>

3、程序中动态设置即可

layout下的progress定义

<ProgressBar android:max="100" 
    android:progress="60"
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"
    android:id="@+id/LifeProgressBar"
    style="@android:style/Widget.ProgressBar.Horizontal"
    android:progressDrawable="@drawable/battery_charge_fill"   />

程序中:

//progress is between 0 and 100 so set level of drawable to progress * 100
Drawable progressDrawable = lifeProgressBar.getProgressDrawable();
progressDrawable.setLevel(progress*100);
lifeProgressBar.setProgress(progress);



展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部