Android seekbar progressDrawable局部渐变解决方案

看了标题可能大家有些不理解,简单一点就是seekbar拖动到哪里就哪个区域渐变,我以前的做法直接是这样的,之前代码:

<?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>
            <solid android:color="#cc999999"/>
            <size android:height="6dp"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <solid android:color="#ffe0e0e0"/>
                <size android:height="6dp"/>
                <corners android:radius="10dp"/>
                <gradient
                    android:angle="0"
                    android:endColor="#FFE81E"
                    android:startColor="#F09819"
                    />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#ffbd74"/>
                <size android:height="6dp"/>
                <corners android:radius="10dp"/>
                <gradient
                    android:angle="0"
                    android:endColor="#FFE81E"
                    android:startColor="#F09819"
                    />
            </shape>
        </clip>
    </item>
</layer-list>

然后在stackoverflow中找到了一个答案,具体如下:

https://stackoverflow.com/questions/29234088/using-gradient-for-android-seekbar-progressdrawable

学习到了一个新的属性:

Percentage(百分比)缩放的宽度,以百分比的方式表示drawable的缩放。形式例如:100%,12.5%。

还有其它的属性可以在官网查询。

我修改后的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>
            <solid android:color="@color/setting_volume_seekbar_background"/>
            <size android:height="6dp"/>
            <corners android:radius="10dp"/>
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <scale android:scaleWidth="100%">
            <shape>
                <solid android:color="#ffe0e0e0"/>
                <size android:height="6dp"/>
                <corners android:radius="10dp"/>
                <gradient
                    android:angle="0"
                    android:endColor="@color/setting_volume_seekbar_progress_end_color"
                    android:startColor="@color/setting_volume_seekbar_progress_start_color"
                    />
            </shape>
        </scale>
    </item>
    <item android:id="@android:id/progress">
        <scale android:scaleWidth="100%">
            <shape>
                <solid android:color="#ffbd74"/>
                <size android:height="6dp"/>
                <corners android:radius="10dp"/>
                <gradient
                    android:angle="0"
                    android:endColor="@color/setting_volume_seekbar_progress_end_color"
                    android:startColor="@color/setting_volume_seekbar_progress_start_color"
                    />
            </shape>
        </scale>
    </item>
</layer-list>

收工,在这里总结一下,以后遇到同样问题不需要再花时间来解决,也希望能帮助到某些人。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android SeekBar 是一种用户界面控件,可以让用户通过滑动来选择数值。可以使用样式来自定义 SeekBar 的外观,以下是一些常见的样式: 1. 改变 SeekBar 前景和背景颜色: ``` <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:progressDrawable="@drawable/seekbar_progress" android:thumb="@drawable/seekbar_thumb" android:background="@drawable/seekbar_background" /> ``` 其中,`seekbar_progress` 是 SeekBar 的前景,`seekbar_thumb` 是拖动条的图标,`seekbar_background` 是 SeekBar 的背景。 2. 改变 SeekBar 的高度和宽度: ``` <SeekBar android:id="@+id/seekbar" android:layout_width="200dp" android:layout_height="10dp" android:progressDrawable="@drawable/seekbar_progress" android:thumb="@drawable/seekbar_thumb" android:background="@drawable/seekbar_background" /> ``` 3. 改变 SeekBar 的圆角半径: ``` <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:progressDrawable="@drawable/seekbar_progress" android:thumb="@drawable/seekbar_thumb" android:background="@drawable/seekbar_background" android:radius="10dp" /> ``` 其中,`radius` 属性定义了 SeekBar 的圆角半径。 4. 改变 SeekBar 的滑动条颜色: ``` <SeekBar android:id="@+id/seekbar" android:layout_width="match_parent" android:layout_height="wrap_content" android:progressDrawable="@drawable/seekbar_progress" android:thumb="@drawable/seekbar_thumb" android:background="@drawable/seekbar_background" android:secondaryProgressTint="@color/secondaryProgressColor" android:progressTint="@color/progressColor" /> ``` 其中,`secondaryProgressTint` 属性定义了 SeekBar 次要进度的颜色,`progressTint` 属性定义了 SeekBar 主要进度的颜色。 以上是一些常见的 SeekBar 样式,你可以根据需要自行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

泡在网上的蜘蛛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值