Draw a line with curved edges in Android
Asked Answered
F

4

19

I am using canvas.drawLine to draw some line in android but the lines are too sharp but i need a curved edges enter image description here

Here the 1 is what i have and 2 is what i want to achieve, means a line with curved edges rather than straight edges

How can I achieve that ??

EDIT 2:

I am trying to use the Canvas object to to draw a line. but the lines have a sharp edge, I need a rounded off edge I am using the Paint object

mPaint = new Paint();
mPaint.setColor(Color.BLACK)

Any help would be appreciated great.

Florafloral answered 29/12, 2013 at 7:14 Comment(6)
Paint.setStrokeCap(Paint.Cap.ROUND)Cyclonite
@Cyclonite : Can you post it as answer so that i can accept itFlorafloral
Your question is put on hold. I cannot post an answer. You should improve it, or remove it. You should read this: meta.stackexchange.com/questions/156810/…Cyclonite
#8288449Trattoria
@Cyclonite : Now you can post your answer. It seems flagging the moderators hold and comment has removed (also my comments too), they felt touchy perhaps ;)Florafloral
Finally the justice has been served :)Cyclonite
C
46

Use the Paint.setStrokeCap() method. You need Paint.Cap.ROUND. The default one is Paint.Cap.BUTT. There is a similar Path property that is called path join. It determines how to draw the parts of the path where it's constituent segments join. To set it use Path.setPathJoin(). You might need it in the future. Good luck.

Cyclonite answered 29/12, 2013 at 9:32 Comment(0)
H
0

You can use below

pitchPaint.setStrokeCap(Paint.Cap.ROUND)
Hoeve answered 1/6, 2022 at 7:16 Comment(1)
How is that different from the accepted answer?Karlsbad
D
0

this is for kotlin Jetpack Compose users : use the cap attribute

Canvas(modifier = Modifier.fillMaxSize()) {

            // Fetching width and height for
            // setting start x and end y

            val canvasHeight = size.height
            
            // drawing a line between start(x,y) and end(x,y)
            drawLine(
                start = Offset(x = 0f, y = 0.12f * canvasHeight),
                end = Offset(x = 0f, y =0.5f *canvasHeight),
                color = Color.Red,
                strokeWidth = 10F,
                cap = StrokeCap.Round
            )
        }

enter image description here

Doloritas answered 11/3, 2023 at 9:28 Comment(0)
A
0
path = new Path();
path.moveTo(50, 50);
path.lineTo(50, 500);
path.lineTo(200, 500);
path.lineTo(200, 300);
path.lineTo(350, 300);

float radius = 50.0f;
CornerPathEffect cornerPathEffect = new CornerPathEffect(radius);
paint.setPathEffect(cornerPathEffect);
// this setPathEffect will set the curve edges for every new joining

canvas.drawPath(path, paint);

Go through this link , This Link will teach you proper usages

Arleanarlee answered 31/5, 2023 at 12:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.