Paint.setStrokeJoin不适用于canvas.drawLines [英] Paint.setStrokeJoin doesn't work with canvas.drawLines

查看:56
本文介绍了Paint.setStrokeJoin不适用于canvas.drawLines的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 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 似乎全部应用了它们.

如果您遇到性能问题,则可以尝试将结果缓存到某个位置,预先计算动画或尝试使用更简单的动画.

请记住,如果您没有特殊要求,则需要很棒的库: MPAndroidChart 已经有一些内置动画

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.

Remember that if you don't have particular requirements there is this awesome library: MPAndroidChart which already has some built-in animations

这篇关于Paint.setStrokeJoin不适用于canvas.drawLines的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆