Android 使用getSegment逐步绘制路径
我创建了一个自定义视图来绘制一条线,但这是渐进的。我尝试使用PathMeasure和getSegment,但效果不起作用。它只是一直在用最终尺寸画线Android 使用getSegment逐步绘制路径,android,android-studio,android-layout,kotlin,android-canvas,Android,Android Studio,Android Layout,Kotlin,Android Canvas,我创建了一个自定义视图来绘制一条线,但这是渐进的。我尝试使用PathMeasure和getSegment,但效果不起作用。它只是一直在用最终尺寸画线 private val paint = Paint().apply { isAntiAlias = true color = Color.WHITE style = Paint.Style.STROKE strokeWidth = 10f } override fun onDraw(canvas: Canvas?
private val paint = Paint().apply {
isAntiAlias = true
color = Color.WHITE
style = Paint.Style.STROKE
strokeWidth = 10f
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
val path = Path().apply {
moveTo(width/2.toFloat(), height/2.toFloat())
lineTo(width/2.toFloat(), height/4.toFloat())
}
val measure = PathMeasure(path, false)
val length = measure.length
val partialPath = Path()
measure.getSegment(0.0f, length, partialPath, true)
partialPath.rLineTo(0.0f, 0.0f)
canvas!!.drawPath(partialPath, paint)
}
您可以使用DashPathEffect执行此操作
DashPathEffect dashPathEffect = new DashPathEffect(new float[]{1000.0f,9999999},0);
Paint.setPathEffect(dashPathEffect);
将1000更改为您的长度(破折号中的“on”部分)
并将99999999设置为最大值(仪表板中的“关闭”部分)
请播放此参数并阅读此内容
您可以使用DashPathEffect执行此操作
DashPathEffect dashPathEffect = new DashPathEffect(new float[]{1000.0f,9999999},0);
Paint.setPathEffect(dashPathEffect);
将1000更改为您的长度(破折号中的“on”部分)
并将99999999设置为最大值(仪表板中的“关闭”部分)
请播放此参数并阅读此内容
正如@mohandes所解释的,我是这样做的:
private var path = Path()
private var paint = Paint()
private val dashes = floatArrayOf(125f, 125f)
init {
paint = Paint().apply {
isAntiAlias = true
color = Color.WHITE
style = Paint.Style.STROKE
strokeWidth = 10.0f
pathEffect = CornerPathEffect(8f)
}
path = Path().apply {
moveTo(312f, 475f)
lineTo(312f, 375f)
}
val lineAnim = ValueAnimator.ofFloat(100f, 0f)
lineAnim.interpolator = LinearInterpolator()
lineAnim.addUpdateListener {
paint.pathEffect = ComposePathEffect(DashPathEffect(dashes, lineAnim.animatedValue as Float), CornerPathEffect(8f))
invalidate()
}
lineAnim.duration = 1000
lineAnim.start()
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas!!.drawPath(path, paint)
}
正如@mohandes所解释的,我是这样做的:
private var path = Path()
private var paint = Paint()
private val dashes = floatArrayOf(125f, 125f)
init {
paint = Paint().apply {
isAntiAlias = true
color = Color.WHITE
style = Paint.Style.STROKE
strokeWidth = 10.0f
pathEffect = CornerPathEffect(8f)
}
path = Path().apply {
moveTo(312f, 475f)
lineTo(312f, 375f)
}
val lineAnim = ValueAnimator.ofFloat(100f, 0f)
lineAnim.interpolator = LinearInterpolator()
lineAnim.addUpdateListener {
paint.pathEffect = ComposePathEffect(DashPathEffect(dashes, lineAnim.animatedValue as Float), CornerPathEffect(8f))
invalidate()
}
lineAnim.duration = 1000
lineAnim.start()
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
canvas!!.drawPath(path, paint)
}
谢谢你!我用ValueAnimator和DashPathEffect制作了它谢谢你!我用ValueAnimator和DashPathEffect制作了它