我正在尝试更改每个特定 tickMark 的颜色。
例如:1号刻度线-黄色,2号刻度线-红色,3号刻度线-白色,等等...
这是我的SeekBar:
<SeekBar
android:id="@+id/tareaSeekBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorAccent"
android:padding="8dp"
android:max="4"
android:progress="1"
android:thumbTint="@color/colorPrimary"
android:progressTint="@color/colorPrimary"
android:progressBackgroundTint="@color/colorSecondary"
android:tickMark="@drawable/tick_mark"
/>
还有TickMark可绘制对象:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<size android:width="4dp"
android:height="4dp"/>
<solid android:color="@color/colorPrimaryDark"/>
</shape>
答案 0 :(得分:0)
这是我更改第一个刻度的解决方案:
class FirstTickSeekBar @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : AppCompatSeekBar(context, attrs, defStyleAttr) {
private val firstTickId: Int
private val otherTicksId: Int
init {
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.FirstTickSeekBar, 0, 0)
firstTickId = typedArray.getResourceId(R.styleable.FirstTickSeekBar_firstTick, 0)
otherTicksId = typedArray.getResourceId(R.styleable.FirstTickSeekBar_otherTicks, 0)
typedArray.recycle()
}
override fun onDraw(canvas: Canvas?) {
canvas?.let {
drawTickMarks(canvas)
}
super.onDraw(canvas)
}
private fun drawTickMarks(canvas: Canvas) {
val firstTickMark = resources.getDrawable(firstTickId, null)
val tickMark = resources.getDrawable(otherTicksId, null)
val count = max
if (count > 1) {
val w = tickMark.intrinsicWidth
val h = tickMark.intrinsicHeight
val halfW = if (w >= 0) w / 2 else 1
val halfH = if (h >= 0) h / 2 else 1
tickMark.setBounds(-halfW, -halfH, halfW, halfH)
firstTickMark.setBounds(-halfW, -halfH, halfW, halfH)
val spacing = (width - paddingLeft - paddingRight) / count.toFloat()
val saveCount = canvas.save()
canvas.translate(paddingLeft.toFloat(), (height / 2).toFloat())
for (i in 0..count) {
if (i == 0) firstTickMark.draw(canvas) else tickMark.draw(canvas)
canvas.translate(spacing, 0f)
}
canvas.restoreToCount(saveCount)
}
}
}
attrs.xml
<declare-styleable name="FirstTickSeekBar">
<attr name="firstTick"/>
<attr name="otherTicks"/>
</declare-styleable>
用法:
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mypackage.FirstTickSeekBar
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="@dimen/margin_bigger"
android:layout_marginEnd="@dimen/margin_bigger"
android:background="@drawable/bg_seekbar"
android:max="4"
android:progress="100"
android:progressDrawable="@color/transparent"
app:tickMark="@color/transparent"
app:firstTick="@drawable/bg_seekbar_tick_first"
app:otherTicks="@drawable/bg_seekbar_tick_mark"
android:thumb="@drawable/bg_seekbar_thumb"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>