在Android中逐渐绘制路径

在Android中逐渐绘制路径,第1张

概述我有一个自定义视图,我想要绘制一个路径,如边框.但是边界应该逐渐消失,就像蛇的大小一样.目的是将其用作玩家在游戏中移动的计时器.我使用Path类和方法lineTo和addArc来绘制边框.timerPath=newPath();timerPath.moveTo(getTranslationX()+width/2,getTranslationY()+

我有一个自定义视图,我想要绘制一个路径,如边框.
但是边界应该逐渐消失,就像蛇的大小一样.
目的是将其用作玩家在游戏中移动的计时器.

我使用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()返回可以绘制到画布的部分路径:

    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中逐渐绘制路径所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1114908.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存