以相同笔画大小绘制线会产生不同粗细的线

时间:2019-09-01 18:19:13

标签: android android-custom-view

我具有以下自定义视图,该视图在两条线之间绘制一条线:

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

结果看起来像这样:

enter image description here

有什么主意,尽管第二行都使用相同的50f笔画大小,但为什么第二行比第一行更粗?

0 个答案:

没有答案