使用渐变为Android SeekBar progressDrawable

Pau*_*eez 5 android gradient styling android-seekbar

目前我正在使用像这样的渐变的自定义SeekBar .我试图用自己的风格实现搜索栏.所以我喜欢这样:

...
<style name="GreenSeekBar.Static.NoThumb">
    <item name="android:progressDrawable">@drawable/sq_seekbar_clipped</item>
    <item name="android:thumb">@null</item>
</style>`
Run Code Online (Sandbox Code Playgroud)

这是sq_seekbar_clipped.xml:

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

<item android:id="@android:id/progress">
    <clip>
        <shape>
            <corners android:radius="2dp"/>
            <gradient
                android:endColor="#00c492"
                android:startColor="#e8e8e8" />
        </shape>
    </clip>
</item>

<item android:id="@android:id/secondaryProgress">
    <shape android:shape="rectangle">
        <size android:height="3dp" />
        <solid android:color="@color/transparent" />
    </shape>
</item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)

所以,除了一件事之外,一切都很有效 - 我收到:

所以,正如你从我所看到的那样 - 渐变被裁剪.我需要渐变从0到[progress_value].那可能吗?

有没有办法像第一个(顶部链接)图像一样绘制渐变?

小智 7

你只需要改变

<clip>
Run Code Online (Sandbox Code Playgroud)

<scale android:scaleWidth="100%">
Run Code Online (Sandbox Code Playgroud)

这是sq_seekbar_clipped.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@android:id/progress">
        <scale android:scaleWidth="100%">
            <shape>
                <corners android:radius="2dp"/>
                <gradient
                    android:endColor="#00c492"
                    android:startColor="#e8e8e8"/>
            </shape>
        </scale>
    </item>

    <item android:id="@android:id/secondaryProgress">
        <shape android:shape="rectangle">
            <size android:height="3dp"/>
            <solid android:color="@color/android:transparent"/>
        </shape>
    </item>
</layer-list>
Run Code Online (Sandbox Code Playgroud)


dhu*_*hun 0

您应该在第二个进度中设置渐变,并在进度中设置纯色。

像这样的东西:

  <layer-list xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:id="@android:id/progress">
  <clip>
     <shape>
         <size android:height="3dp" />
         <solid android:color="@color/transparent" />

     </shape>
  </clip>
 </item>

 <item android:id="@android:id/secondaryProgress">
  <shape android:shape="rectangle">
       <corners android:radius="2dp"/>
         <gradient
             android:endColor="#00c492"
             android:centerColor=""
             android:startColor="#e8e8e8" />
   </shape>
 </item>
 </layer-list>
Run Code Online (Sandbox Code Playgroud)

另外,提供一些中心颜色以获得清晰的视图。