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

本文介绍了一种自定义DashedLineView的方法,使得在XML中配置虚线更加方便。该视图支持横向和纵向两种方向,并允许自定义线宽和间隔,提供高度灵活性。通过设置不同属性,可以轻松创建不同尺寸和颜色的虚线。源码中展示了如何实现这一功能,包括获取属性值和在onDraw()方法中绘制虚线的逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

e25c20204e0ccb5823ab605874b6829a.png

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

使用说明:

默认方向:横向。

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

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

直接在xml中配置:

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" />

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 chrisSpringSmell@gmail.com

*/

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()

}

}

属性:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值