Android 自定义View,虚线纵向、横向

虚线在shape中配置还是比较麻烦的,所以自定义一个,使用起来会方便很多。

Android 自定义View,虚线纵向、横向

 虚线支持横向、纵向两种方式。并且高宽间隔都可以自定义,使用很灵活。

使用说明:

默认方向:横向。

横向时:默认宽度为40,默认高度为View高度

纵向时:默认宽度为View的宽度,默认高度为40

直接在xml中配置:

    <org.quick.component.widget.DashedLineView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        app:dashLineHeight="2dp"
        app:dashLineWidth="10dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="@+id/selectedTv0" />

    <org.quick.component.widget.DashedLineView
        android:layout_width="30dp"
        android:layout_height="match_parent"
        app:dashLineColor="@color/colorPrimary"
        app:dashLineHeight="10dp"
        app:dashLineWidth="2dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:orientation="vertical" />

下面看一下源码:

/**
 * 虚线
 * * Created by Administrator on 2016/8/1.
 * @author chrisZou
 * @from https://github.com/SpringSmell/quick.library
 * @email [email protected]
 */
class DashedLineView(context: Context, attrs: AttributeSet) : View(context, attrs) {

    private var dashLineColor: Int
    private var dashLineHeight: Float
    private var dashLineWidth: Float
    private var paint = Paint()
    private var orientation = 0x1

    enum class Orientation(var value: Int) {
        HORIZONTAL(0x1), VERTICAL(0x2)
    }

    init {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.DashedLineView)
        dashLineColor = ta.getColor(R.styleable.DashedLineView_dashLineColor, Color.DKGRAY)
        dashLineHeight = ta.getDimension(R.styleable.DashedLineView_dashLineHeight, -1f)
        dashLineWidth = ta.getDimension(R.styleable.DashedLineView_dashLineWidth, -1f)
        orientation = ta.getInt(R.styleable.DashedLineView_orientation, Orientation.HORIZONTAL.value)
        ta.recycle()
        paint.style = Paint.Style.STROKE
        paint.color = dashLineColor
        paint.isAntiAlias = true
    }


    @SuppressLint("DrawAllocation")
    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val path = Path()
        paint.pathEffect =
                if (orientation == Orientation.HORIZONTAL.value) {
                    if (dashLineWidth == -1f) dashLineWidth = 40f
                    if (dashLineHeight == -1f) dashLineHeight = height.toFloat()
                    paint.strokeWidth = dashLineHeight
                    path.moveTo(0f, height / 2f)
                    path.lineTo(width.toFloat(), height / 2f)
                    DashPathEffect(floatArrayOf(dashLineWidth, dashLineWidth, dashLineWidth, dashLineWidth), 0f)
                } else {
                    if (dashLineWidth == -1f) dashLineWidth = width.toFloat()
                    if (dashLineHeight == -1f) dashLineHeight = 40f
                    paint.strokeWidth = dashLineWidth
                    path.moveTo(width / 2f, 0f)
                    path.lineTo(width / 2f, height.toFloat())
                    DashPathEffect(floatArrayOf(dashLineHeight, dashLineHeight, dashLineHeight, dashLineHeight), 0f)
                }
        canvas.drawPath(path, paint)
        path.reset()
        path.close()
    }
}

属性:

<!--虚线-->
    <declare-styleable name="DashedLineView">
        
        <attr name="dashLineHeight" format="dimension|integer" />
        <attr name="dashLineColor" format="color|integer" />
        <attr name="dashLineWidth" format="dimension|integer" />
        <!--线条方向-->
        <attr name="orientation" format="enum">
            <enum name="horizontal" value="0x1" />
            <enum name="vertical" value="0x2" />
        </attr>
    </declare-styleable>