如何使用位图绘制路径?

8

我有一个小画图应用程序,想要使用“复杂”形状作为画笔,例如星形。 使用以下代码可以使用简单画笔进行绘制:

remotePath.reset();
remotePath.moveTo(start_x, start_y);

float dx = Math.abs(end_x - start_x);
float dy = Math.abs(end_y - start_y);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
        remotePath.quadTo(start_x, start_y, (end_x + start_x) / 2, (end_y + start_y) / 2);
}

remotePath.lineTo(end_x, end_y);
// commit the path to our offscreen
mCanvas.drawPath(remotePath, remotePaint);
// kill this so we don't double draw
remotePath.reset();
invalidate();

我基本上想使用这个位图实现相同的功能:

Bitmap myBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.brush_star);

目前我的解决方案是使用一组点(坐标)来绘制位图。这种方法的问题在于它只在给定的点上绘制位图,导致每个绘制的位图之间存在间隙。我更希望在绘制时获得类似于简单画笔的平滑线条,而不会出现任何间隙。

用于绘制位图的当前代码:

        protected void onDraw(Canvas canvas) {

        // Make canvas white
        canvas.drawColor(Color.WHITE);

        // Paintable area
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);

        canvas.drawPath(mPath, mPaint);

        for (Point point : points) {
            canvas.drawBitmap(complexBrush, point.x, point.y, p);
        }
    }

什么是最好的方法呢?感谢任何帮助!
1个回答

3
我使用这个Point类:
public class Point  implements Serializable {
float x, y;
float dx, dy;
}

绘制对象:

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(5);
paint.setColor(Color.RED);
paint.setAntiAlias(true);

在画布上绘制:
private void drawCanvas(Canvas canvas, List<Point> pts){
    if (pts.size() > 1){
        Path path = new Path();
        final int SMOOTH_VAL = 6;
        for(int i = pts.size() - 2; i < pts.size(); i++){
            if(i >= 0){
                Point point = pts.get(i);

                if(i == 0){
                    Point next = pts.get(i + 1);
                    point.dx = ((next.x - point.x) / SMOOTH_VAL);
                    point.dy = ((next.y - point.y) / SMOOTH_VAL);
                }
                else if(i == pts.size() - 1){
                    Point prev = pts.get(i - 1);
                    point.dx = ((point.x - prev.x) / SMOOTH_VAL);
                    point.dy = ((point.y - prev.y) / SMOOTH_VAL);
                }
                else{
                    Point next = pts.get(i + 1);
                    Point prev = pts.get(i - 1);
                    point.dx = ((next.x - prev.x) / SMOOTH_VAL);
                    point.dy = ((next.y - prev.y) / SMOOTH_VAL);
                }
            }
        }

        boolean first = true;
        for(int i = 0; i < pts.size(); i++){
            Point point = pts.get(i);
            if(first){
                first = false;
                path.moveTo(point.x, point.y);
            }
            else{
                Point prev = pts.get(i - 1);
                path.cubicTo(prev.x + prev.dx, prev.y + prev.dy, point.x - point.dx, point.y - point.dy, point.x, point.y);
            }
        }
        canvas.drawPath(path, paint);
    } else {
        if (pts.size() == 1) {
            Point point = pts.get(0);
            canvas.drawCircle(point.x, point.y, 2, paint);
        }
    }
}

在位图画布上绘制:
private void drawBitmap(Bitmap bmp, List<Point> pts) {
    Canvas c = new Canvas(bmp);
    drawCanvas(c, pts);
}

我们如何使用多个位图进行绘制?就像我们更改颜色并绘制多条路径一样。 - Muhammad Umair Shafique

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接