Kotlin
/**
* 竖直虚线
*/
class VerticalDashedLineView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : View(context, attrs) {
private val paint = Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = Color.parseColor("#AAAAAA")
strokeWidth = 1f * resources.displayMetrics.density
style = Paint.Style.STROKE
pathEffect = android.graphics.DashPathEffect(
floatArrayOf(
4f * resources.displayMetrics.density,
3f * resources.displayMetrics.density
),
0f
)
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val x = width / 2f
canvas.drawLine(x, 0f, x, height.toFloat(), paint)
}
}
使用:
Kotlin
<com.xx.xx.widget.VerticalDashedLineView
android:layout_width="1dp"
android:layout_height="200dp"
/>
strokeWidth 虚线粗细 1dp
dashWidth 每段实线长度 4dp
dashGap 每段之间的间距 3dp
layout_height 虚线高度 100dp