gpt4 book ai didi

android - 在Android中逐渐绘制路径

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:57:56 27 4
gpt4 key购买 nike

我有一个自定义 View ,我想围绕它绘制一条路径,例如边框。但是边界应该像蛇一样逐渐变大。目的是将其用作玩家在游戏中采取行动的计时器。

我使用 Path 类和方法 lineTo 和 addArc 来绘制边框。

timerPath = new Path();
timerPath.moveTo(getTranslationX() + width / 2, getTranslationY() + 3);
timerPath.lineTo(getTranslationX() + width - 10, getTranslationY() + 3);
timerPath.addArc(new RectF(getTranslationX() + width - 20, getTranslationY() + 3,
getTranslationX() + width - 3, getTranslationY() + 20), -90f, 90f);
...
...
timerPath.lineTo(getTranslationX() + width / 2, getTranslationY() + 3);

timerPaint = new Paint();
timerPaint.setColor(Color.GREEN);
timerPaint.setStyle(Paint.Style.STROKE);
timerPaint.setStrokeWidth(6);

我在 onDraw 中使用 drawPath() 方法:

canvas.drawPath(timerPath, timerPaint);

看起来不错。

现在,我想知道是否有一种方法可以使用百分比(10%、11%、12% .. 等)来绘制路径的一部分。然后我就可以为绘图设置动画了。

如果不可能,是否有另一种动画边框绘制方法? (用作计时器)

感谢您的帮助。

最佳答案

您可以使用 PathMeasure类来做到这一点。从您的路径创建一个 PathMeasure 对象,测量长度,然后使用 getSegment() 返回您可以绘制到 Canvas 的部分路径:

    float percentage = 50.0f; // initialize to your desired percentage

PathMeasure measure = new PathMeasure(timerPath, false);
float length = measure.getLength();
Path partialPath = new Path();
measure.getSegment(0.0f, (length * percentage) / 100.0f, partialPath, true);
partialPath.rLineTo(0.0f, 0.0f); // workaround to display on hardware accelerated canvas as described in docs
canvas.drawPath(partialPath, timerPaint);

关于android - 在Android中逐渐绘制路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29243610/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com