正如我在评论中告诉你的,Paint
仅当您使用以下命令绘制对象时,对象才会被完全应用Path
.
In drawLine https://developer.android.com/reference/android/graphics/Canvas#drawLine(float,%20float,%20float,%20float,%20android.graphics.Paint)文档中有一个段落:'样式在绘画中被忽略' 同样的事情也适用于drawLines
method.
为了测试这一点,我创建了一个简单的自定义视图:
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)
}
}
}
结果是这样的:
所以使用drawLines
部分应用样式Paint
obj,喜欢颜色,但没有应用strokeJoin
喜欢属性。drawPath
似乎应用了所有这些。
如果您遇到性能问题,也许您可以尝试将结果缓存在某处,预先计算动画或尝试使用更简单的动画。
请记住,如果您没有特殊要求,则可以这样
很棒的图书馆:MPAndroid图表 https://github.com/PhilJay/MPAndroidChart已经有一些内置动画