1. 程式人生 > >Android 自定義View,虛線縱向、橫向

Android 自定義View,虛線縱向、橫向

虛線在shape中配置還是比較麻煩的,所以自定義一個,使用起來會方便很多。

 虛線支援橫向、縱向兩種方式。並且高寬間隔都可以自定義,使用很靈活。

使用說明:

預設方向:橫向。

橫向時:預設寬度為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>