看了标题可能大家有些不理解,简单一点就是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>
收工,在这里总结一下,以后遇到同样问题不需要再花时间来解决,也希望能帮助到某些人。