我的应用程序中有球只是飞过显示器。他们画出我想要的。但现在我想在他们身后画出一条道路。
我只能通过canvas.drawPath
绘制如下图所示的内容:
但这不是我想要的。它应该有尖尾和渐变颜色,如下所示:
我不知道如何制作它。试过BitmapShader
- 无法做出正确的事情。求助。
代码:
首先,展示位置的Point
类:
class Point {
float x, y;
...
}
并且路径存储为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);
答案 0 :(得分:2)
我看到你想看到球道上的渐变,你可以使用这样的东西
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并查看它是否有效。
由here提供。
答案 1 :(得分:0)
我找到了解决方案。但仍然认为它不是最好的。
首先,我的课程字段用于该任务。
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;
除trailPath
对象外,我使用PathMeasure
对象将路径分割为多个相等的部分。
填充跟踪数组对象后添加了跟踪计算功能的调用。
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;
}
}
}
这个要点是用不同颜料绘制的部分。更接近球 - 更宽的路径。我想我会优化它,但它已经完成了。
如果您想观看它的外观,只需安装my app from google play。