0

How to draw a circler path by swiping, it's draw with edges. I wanted to draw path with smooth edges.

this is my code.

        this.mPaint = new Paint();
            this.mPaint.setStyle(Paint.Style.STROKE);
            this.mPaint.setStrokeWidth(getResources().getDimensionPixelSize(R.dimen._5sdp));
            this.mPaint.setStrokeCap(Paint.Cap.ROUND);
            this.mPaint.setStrokeJoin(Paint.Join.ROUND);
            this.mPaint.setColor(Color.BLUE);
            this.mPaint.setAntiAlias(true);
            this.mPaint.setDither(true);

this code is in OnTouchListener

public boolean onTouch(View view, MotionEvent motionEvent) {
                        int action = motionEvent.getAction();
                        float x = motionEvent.getX();
                        float y = motionEvent.getY();
                        switch (action) {
                            case MotionEvent.ACTION_DOWN:
                                mPath.reset();
                                mPath.moveTo(x, y);
                                break;
                            case MotionEvent.ACTION_MOVE:
                                Log.d(TAG, "onT:-> x " + x + " y-> " + y);
                                mPath.lineTo(x, y);
                                view.invalidate();
                                break;
                            case MotionEvent.ACTION_UP:
                            case MotionEvent.ACTION_CANCEL:
                                mPaths.add(new CurrentDraw(mPath, mPaint));
                                mPath.reset();
                                view.invalidate();
//                                updateLocalSS();
                                return false;
                        }
                        return true;
                    }

 

this code is in ondraw() method

if (mPaths != null) {
                    for (int i = 0; i < mPaths.size(); i++) {
                        canvas.drawPath(mPaths.get(i).path, mPaths.get(i).paint);
                    }
                }
                if (mPath != null) {
                    canvas.drawPath(mPath, mPaint);
                }

Figure 1

Figure 2

EAS
  • 366
  • 2
  • 18

1 Answers1

0

Please just try this way may help you

RectF area;
private Paint bgPaint;

private Paint initpaint() {
    int bColor = Color.WHITE;
    if (!isInEditMode()) {
        bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    } else {
       bgPaint = new Paint();
    }
   
  bgPaint.setColor(bColor);
  bgPaint.setStyle(Paint.Style.STROKE);
  bgPaint.setStrokeWidth(sizeBg);   
}


Public void initArea(){
   float drawPadding = (size / 2);
   float width = getWidth();
   float height = getHeight();
   float left = drawPadding;
   float top = drawPadding;
   float right = width - drawPadding;
   float bottom = height - drawPadding;
   area = new RectF(left, top, right, bottom);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Path path = new Path();
    path.addCircle(area.centerX(), area.centerY(), getRadius(), Path.Direction.CCW);
    canvas.drawPath(path, bgPaint);  
}

private float getRadius() {
    if (area != null) {
        return (area.width() / 2);
    } else {
        return 0;
    }
}
gpuser
  • 1,143
  • 1
  • 9
  • 6
  • will you explain, where should I call initarea() and initpaint() method? – EAS Dec 03 '21 at 07:06
  • you can call anywhere before draw it like constructor of class or onsize changed method or where you are already creating paint object in your class. – gpuser Dec 03 '21 at 07:20
  • What should I write in on Touch listener? – EAS Dec 03 '21 at 07:26
  • you can update the path lis in ontouchlistener....can you please update question with share more line of code which you written in your project to understand more....in above code I just shared the way may help you – gpuser Dec 03 '21 at 09:30