生成渐变进度条

udi*_*dit 7 android

渐变图像

我使用下面的代码在进度条中显示渐变.那么如何创建如上图所示的渐变进度条呢?我尝试了很多解决方案,但还没有成功.

  <?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="@color/gray"
                android:centerColor="@color/gray"
                android:centerY="0.75"
                android:endColor="@color/gray"
                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="@color/dark_yellow"
                    android:endColor="@color/dark_yellow"
                    android:angle="270" />
            </shape>
        </clip>
    </item>

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

pxs*_*xsx 20

在你的情况下@android:id/secondaryProgress是没有必要的.此外,android:angle=270将从顶部到底部旋转渐变,因此它与所需的垂直方向.

所有你需要的是:

绘制/ gradient_progress.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"/>
            <solid android:color="#fff"/>
        </shape>
    </item>

    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dp"/>
                <gradient
                    android:endColor="@android:color/holo_red_dark"
                    android:startColor="@android:color/holo_orange_light"
                    />
            </shape>
        </clip>
    </item>

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

布局/ any_layout_file.xml

<ProgressBar
    xmlns:tools="http://schemas.android.com/tools"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="10dp"
    android:progressDrawable="@drawable/gradient_progress"
    tools:progress="60"
    />
Run Code Online (Sandbox Code Playgroud)

  • 需要像下面一样添加&lt;clip&gt;&lt;/clip&gt;,否则进度值将不被考虑。```&lt;item android:id="@android:id/progress"&gt; &lt;clip&gt; &lt;shape&gt; &lt;corners android:radius="5dp" /&gt; &lt;gradient android:endColor="@android:color/holo_red_dark" android :startColor="@android:color/holo_orange_light"/&gt; &lt;/shape&gt; &lt;/clip&gt; &lt;/item&gt;``` (2认同)