I have two points in the canvas, now I'm able to draw a line between those points like this below image by using
This code canvas.drawLine(p1.x, p1.y, p2.x, p2.y, paint);
I want to draw the arc between two points like below image.
How can I draw like this.
The arc() method creates an arc/curve (used to create circles, or parts of circles). Tip: To create a circle with arc(): Set start angle to 0 and end angle to 2*Math. PI. Tip: Use the stroke() or the fill() method to actually draw the arc on the canvas.
You can draw arcs using any of the following methods: Three points on an arc ( ). Start point-center-endpoint ( ), or Start point-endpoint-center ( ), or Center-start point-endpoint ( ). Start point-center-included angle ( ), or Start point-included angle-center ( ), or Center-start point-included angle ( ).
Use the Canvas method public void drawBitmap (Bitmap bitmap, Rect src, RectF dst, Paint paint) . Set dst to the size of the rectangle you want the entire image to be scaled into. EDIT: Here's a possible implementation for drawing the bitmaps in squares across on the canvas.
Finally I got the solution from this code:
float radius = 20; final RectF oval = new RectF(); oval.set(point1.x - radius, point1.y - radius, point1.x + radius, point1.y+ radius); Path myPath = new Path(); myPath.arcTo(oval, startAngle, -(float) sweepAngle, true);
To calculate startAngle
, use this code:
int startAngle = (int) (180 / Math.PI * Math.atan2(point.y - point1.y, point.x - point1.x));
Here, point1
means where you want to start drawing the Arc. sweepAngle
means the angle between two lines. We have to calculate that by using two points like the blue points in my Question image.
Do something like this:
//Initialized paint on a class level object. Paint p = new Paint(); p.setColor(Color.BLACK); //Calculate the rect / bounds of oval RectF rectF = new RectF(50, 20, 100, 80); @Override protected void onDraw(Canvas canvas) { //Do the drawing in onDraw() method of View. canvas.drawArc (rectF, 90, 45, false, p); }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With