实现原理
借助LayerDrawable资源实现
编写drawable目录下的xml文件时注意:id指定为@android:id/background
才能将对应的图片设置为seekbar的背景图标;id指定未@android:id/progress
才能将对应的图片设置为seekbar的已完成部分的图标
实现效果
源码:
/res/drawable目录下:special_seek_bar.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"
android:drawable="@drawable/ic_background"/>
<!-- 已完成部分的图像 -->
<item android:id="@android:id/progress"
android:drawable="@drawable/ic_progress"/>
</layer-list>
布局文件:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SpecialSeekBarActivity">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progressDrawable="@drawable/special_seek_bar"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>