安卓画球轨迹

2022-09-04 04:33:00

我的应用程序中有球只是飞过显示屏。他们随心所欲地画画。但现在我想画出他们身后的踪迹。

我所能做的就是通过如下图画来绘制:canvas.drawPath

I have it now

但这不是我想要的。它应该有尖的尾巴和渐变的颜色,如下所示:

I want that

我不知道如何做到这一点。尝试过 - 无法做正确的事情。请帮忙。BitmapShader

法典:

首先,有显示位置的类:Point

class Point {
    float x, y;
    ...
}

而 trail 被存储为队列:Point

private ConcurrentLinkedQueue<Point> trail;

它如何填充并不重要,只要知道它有大小限制:

trail.add(position);
if(trail.size() > TRAIL_MAX_COUNT) {
    trail.remove();
}

绘制发生在方法上:DrawTrail

private void DrawTrail(Canvas canvas) {
    trailPath.reset();
    boolean isFirst = true;
    for(Point p : trail) {
        if(isFirst) {
            trailPath.moveTo(p.x, p.y);
            isFirst = false;
        } else {
            trailPath.lineTo(p.x, p.y);
        }
    }
    canvas.drawPath(trailPath, trailPaint);
}

顺便说一句,只是真的很胖的油漆:)trailPaint

trailPaint = new Paint();
trailPaint.setStyle(Paint.Style.STROKE);
trailPaint.setColor(color);
trailPaint.setStrokeWidth(radius * 2);
trailPaint.setAlpha(150);

答案 1

我看到你想在球道上看到一个渐变,你可以使用这样的东西

 int x1 = 0, y1 = 0, x2 = 0,  y2 = 40;
 Shader shader = new LinearGradient(0, 0, 0, 40, Color.WHITE, Color.BLACK, TileMode.CLAMP);
trailPaint = new Paint(); 
trailPaint.setShader(shader); 

这是您应该将trailPaint更改为的内容,看看它是否有效。

从这里提供。


答案 2

我找到了解决方案。但仍然认为它不是最好的一个。

首先,有我的类字段用于该任务。

static final int TRAIL_MAX_COUNT = 50; //maximum trail array size
static final int TRAIL_DRAW_POINT = 30; //number of points to split the trail for draw

private ConcurrentLinkedQueue<Point> trail;
private Paint[] trailPaints;
private float[][] trailPoss, trailTans;
private Path trailPath;

此外,对于对象,我使用对象将路径拆分为多个相等的部分。trailPathPathMeasure

填充尾随数组对象后增加了调用尾随计算功能。

lastTrailAdd = now;
trail.add(pos.Copy());
if (trail.size() > TRAIL_MAX_COUNT) {
    trail.remove();
}
FillTrail();

然后是我的函数。FillTrail

private void FillTrail() {
    trailPath.reset();
    boolean isFirst = true;
    for(Point p : trail) {
        if(isFirst) {
            trailPath.moveTo(p.x, p.y);
            trailPoss[0][0] = p.x;
            trailPoss[0][1] = p.y;
            isFirst = false;
        } else {
            trailPath.lineTo(p.x, p.y);
        }
    }
    PathMeasure path = new PathMeasure(trailPath, false);
    float step = path.getLength() / TRAIL_DRAW_POINT;
    for(int i=0; i<TRAIL_DRAW_POINT; i++) {
        path.getPosTan(step * i, trailPoss[i], trailTans[i]);
    }
}

它与绘图线程分离。下一个代码是绘图函数。

private void DrawTrail(Canvas canvas) {
    if(trail.size() > 1) {
        float prevWidthHalfX = 0f, prevWidthHalfY = 0f, prevX = 0f, prevY = 0f;
        Path trailStepRect = new Path();
        boolean isFirst = true;
        for (int i = 0; i < TRAIL_DRAW_POINT; i++) {
            float currWidthHalf = (float) (radius) * i / TRAIL_DRAW_POINT / 2f,
                    currWidthHalfX = currWidthHalf * trailTans[i][1],
                    currWidthHalfY = currWidthHalf * trailTans[i][0],
                    currX = trailPoss[i][0], currY = trailPoss[i][1];
            if (!isFirst) {
                trailStepRect.reset();
                trailStepRect.moveTo(prevX - prevWidthHalfX, prevY + prevWidthHalfY);
                trailStepRect.lineTo(prevX + prevWidthHalfX, prevY - prevWidthHalfY);
                trailStepRect.lineTo(currX + currWidthHalfX, currY - currWidthHalfY);
                trailStepRect.lineTo(currX - currWidthHalfX, currY + currWidthHalfY);
                canvas.drawPath(trailStepRect, trailPaints[i]);
            } else {
                isFirst = false;
            }
            prevX = currX;
            prevY = currY;
            prevWidthHalfX = currWidthHalfX;
            prevWidthHalfY = currWidthHalfY;
        }
    }
}

这一点的要点是用不同油漆的零件绘制痕迹。靠近球 - 更宽的小径。我想我会优化它,但这是一项准备就绪的工作。

如果你想看它的外观,只需从谷歌播放安装我的应用程序


推荐