在Andorid画布中快速移动手指时如何使用路径绘制画笔或形状用户快速移动手指时会生成缺失点

问题描述

我真的很困惑我该如何在android中绘制专业画笔,当用户在屏幕上移动手指时我会使用路径绘制圆,但是当用户缓慢移动手指时圆的数量增加,而当用户快速移动手指时圆的数量增加很少,假设用户快速移动手指,则该路径上只有6 7圈,但是如果用户缓慢移动手指,其路径上将是30/40或更多圈,这似乎非常有问题,是否可能快速移动的手指可以存储更少的积分?但是,如果我谈论line,则画布上的线条会完美地绘制,而用户快速或缓慢地移动它时,我会在下面共享我的代码

    private void DrawCircleBrush(List<PointF> points) {
    PointF p1 = points.get(0);
    PointF p2 = points.get(1);

    Path path = new Path();
    path.moveto(p1.x,p1.y);

    for (int i = 1; i < points.size(); i++) {
        int rc = (int) (20 +(this.paintstrokeWidth/5));
        path.addCircle(p1.x,p1.y,(float) rc,Path.Direction.ccw);
    }

    this.invalidate();

}

我像这样在action_move上调用DrawCircleBrush Fucnion

path.reset();
points.add(new PointF(x,y));
DrawCircleBrush(points);

您可以在所附图片中看到快动手指和慢动手指的区别。

here is the difference of fast moving finger and slow moving finger

在这张照片中可以看到我想要实现的目标,因为当我快速或缓慢移动手指时,画笔会在此应用程序中画出相同的颜色

This is what i want to achive

解决方法

好吧,我终于找到了解决方案。 这就是我要如何获取所有要点的方法,请注意,这是一个称为Bresenham线算法的定理,它仅适用于整数, 这就是我得到所有要点的方法,快动或慢动手指总是相同的:D

//x0,y0,is the starting point and x1,y1 are current points

public List<PointF> findLine( int x0,int y0,int x1,int y1)
{
    List<PointF> line = new ArrayList<PointF>();

    int dx = Math.abs(x1 - x0);
    int dy = Math.abs(y1 - y0);

    int sx = x0 < x1 ? 1 : -1;
    int sy = y0 < y1 ? 1 : -1;

    int err = dx-dy;
    int e2;

    while (true)
    {

        line.add(new PointF(x0,y0));

        if (x0 == x1 && y0 == y1)
            break;

        e2 = 2 * err;
        if (e2 > -dy)
        {
            err = err - dy;
            x0 = x0 + sx;
        }

        if (e2 < dx)
        {
            err = err + dx;
            y0 = y0 + sy;
        }
    }
    return line;
}

我如何在笔刷上使用此功能

             //radius of circle
            int rc = (int) (20 +(this.paintStrokeWidth/5));
            //getting the points of line
            List<PointF> pointFC =findLine((int)this.startX,(int) this.startY,(int) x,(int) y);
            //setting the index of first point
            int p1 = 0;
            //will check if change occur
            boolean change = false;

            for(int l=1; l<pointFC.size(); l++){

                //getting distance between two pints 
                float d = distanceBetween(pointFC.get(p1),pointFC.get(l));
                if(d>rc){
                    // we will add this point for draw
                    //point is a list of PointF //declared universally
                    points.add(new PointF(pointFC.get(l).x,pointFC.get(l).y));
                    we will change the index of last point
                    p1 = l-1;
                    change = true;

                }
            }
            if(points.size() >0){
                path.reset();
                DrawCircleBrush(points);
            }

            if(change){
                we will cahnge the starts points,//set them as last drawn points
                this.startX = points.get(points.size()-1).x;
                this.startY = points.get(points.size()-1).y;
            }

   //Distance betwenn points
   private float distanceBetween(PointF point1,PointF point2) {
        return (float) Math.sqrt(Math.pow(point2.x - point1.x,2) +  
        Math.pow(point2.y - point1.y,2));
    }

//this is how im drawing my circle brush
private void DrawCircleBrush(List<PointF> points) {

        Path path = this.getCurrentPath();
        path.moveTo(points.get(0).x,points.get(0).y);

        for (int i = 1; i < points.size(); i++) {
            PointF pf = points.get(i);
            int rc = (int) (20 +(this.paintStrokeWidth/5));
            path.addCircle(pf.x,pf.y,(float) rc,Path.Direction.CCW);

        }
    }

结果:即使快速或缓慢移动手指,画笔也一样Moving finger fast or slow no difference

,

检查here中的“ colored_pixels”