Skip to content Skip to sidebar Skip to footer

How To Draw Brushes Or Shape Using Path When Moving Finger Fast In Andorid Canvas (generate Missing Point When User Move Finger Fast)

Im really confuse that how can i draw professional brushes in android, im drawing circle using path when user moves its finger on screen but when user move its finger slow the numb

Solution 1:

Ok At last i find solution. this is how im getting all the points , note that this a theorem called Bresenham's line algorithm and its only works with integer, this is how im getting all the point , move finger fast or slow point will always be same :D

//x0,y0 , is the starting point and x1,y1 are current pointspublic List<PointF> findLine( int x0, int y0, int x1, int y1)
{
    List<PointF> line = newArrayList<PointF>();

    intdx= Math.abs(x1 - x0);
    intdy= Math.abs(y1 - y0);

    intsx= x0 < x1 ? 1 : -1;
    intsy= y0 < y1 ? 1 : -1;

    interr= dx-dy;
    int e2;

    while (true)
    {

        line.add(newPointF(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;
}

How im using this function for my brush,

//radius of circleint 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 pointint 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 pointsthis.startX = points.get(points.size()-1).x;
                this.startY = points.get(points.size()-1).y;
            }

   //Distance betwenn pointsprivatefloatdistanceBetween(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 brushprivatevoidDrawCircleBrush(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);

        }
    }

Result: brush is same even move finger fast or slow Moving finger fast or slow no difference

Solution 2:

Check the "colored_pixels" from here

Post a Comment for "How To Draw Brushes Or Shape Using Path When Moving Finger Fast In Andorid Canvas (generate Missing Point When User Move Finger Fast)"