问题描述
我正在尝试使用 canvas.drawLines(...)
绘制折线图,但似乎线条未正确连接.据我了解,使用 Paint.setStrokeJoin
应该使用斜接:
chartLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);chartLinePaint.setStyle(Paint.Style.STROKE);chartLinePaint.setStrokeJoin(Paint.Join.MITER);chartLinePaint.setStrokeWidth(6.0f);
如何解决此问题并使线正确连接?
正如我在评论中告诉您的那样,仅当使用 Path
Paint 对象才被完全应用.>.
在
因此,使用 drawLines
部分应用 Paint
obj的样式(例如颜色),但不应用 strokeJoin
(例如属性). drawPath
似乎全部应用了它们.
如果您遇到性能问题,则可以尝试将结果缓存到某个位置,预先计算动画或尝试使用更简单的动画.
I am trying to draw a line chart using canvas.drawLines(...)
, but it seems that the lines are not properly connected. As I understand using Paint.setStrokeJoin
should use the miter join:
chartLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
chartLinePaint.setStyle(Paint.Style.STROKE);
chartLinePaint.setStrokeJoin(Paint.Join.MITER);
chartLinePaint.setStrokeWidth(6.0f);
How do I fix this problem and make the lines properly joined?
As I told you in the comment, Paint
objects are fully applied only when you draw them with Path
.
In drawLine documentation there is a paragraph with: 'the Style is ignored in the paint' and the same thing is applied to drawLines
method.
To test this, I created a simple custom view:
class CanvasTestView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
private val textPaint1 = Paint(ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
strokeJoin = Paint.Join.MITER
strokeWidth = 12.0f
color = Color.RED
}
private val textPaint2 = Paint(ANTI_ALIAS_FLAG).apply {
style = Paint.Style.STROKE
strokeJoin = Paint.Join.MITER
strokeWidth = 12.0f
color = Color.BLUE
}
@SuppressLint("DrawAllocation")
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas?.apply {
val floatArray = floatArrayOf(250f, 550f, 450f, 200f, 450f, 200f, 650f, 700f)
drawLines(floatArray, textPaint2)
val path = Path()
path.moveTo(200f, 500f)
path.lineTo(400f, 200f)
path.lineTo(600f, 700f)
drawPath(path, textPaint1)
}
}
}
And the result is this:
So using drawLines
partially apply the styles of Paint
obj, like colours, but is not applying strokeJoin
like properties. drawPath
seems to apply all of them instead.
If you have a performance problem maybe you can try to cache the result somewhere, pre-compute the animation or try with a simpler one.
这篇关于Paint.setStrokeJoin不适用于canvas.drawLines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!