我具有以下自定义视图,该视图在两条线之间绘制一条线:
class GridView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private val linePaint = Paint()
init {
linePaint.color = Color.DKGRAY
linePaint.style = Paint.Style.STROKE
linePaint.strokeWidth = 50f //resources.getDimension(R.dimen.line_width)
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
canvas.drawLine(0f, 0f, width.toFloat(), 0f, linePaint)
canvas.drawLine(0f, 200f, width.toFloat(), 200f, linePaint)
}
}
视图具有固定大小:
<com.vgrec.playground.tictactoe.GridView
android:layout_width="300dp"
android:layout_height="300dp" />
结果看起来像这样:
有什么主意,尽管第二行都使用相同的50f
笔画大小,但为什么第二行比第一行更粗?