Android canvas draw line - make the line thicker
Asked Answered
Y

4

29

This seems like it should be somewhat trivial, however in my android app, I am using canvas to draw a series of lines that are connected together. For some reason my lines are very very faint and thin. I was wondering how can I make my lines thicker? Here is my code..

for(int i=1; i<myArrayListOfValues.size(); i++){

        Paint myPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        myPaint.setStrokeWidth(8/*1 /getResources().getDisplayMetrics().density*/);
        myPaint.setColor(0xffff0000);   //color.RED

        canvas.drawLine(myArrayListOfValues.get(i), myArrayListOfValues.get(i), myArrayListOfValues.get(i-1), myArrayListOfValues.get(i-1), myPaint);       

    }

Another thing is..my lines and circles that I draw are ALWAYS black.. setColor() never seems to have any effect. I've tried using the color names (e.g color.red) and even their hex values (e.g 0xffff0000)

Yarborough answered 25/7, 2011 at 22:0 Comment(3)
Can you post some more code from this class, like the entire onDraw? or the class if its small enough. It looks okay as it is. Why did you create the Paint object inside the loop? all you iterations use same 'myPaint' so its better to create it once outside the loop.Tacky
I have moved it outside of the loop now. That is all my code inside the onDraw method. I just want to know how you can make the lines thicker. It is displaying the lines but they are too thinYarborough
Try removing the anti_alias flag, and try using 8.0 instead.Tacky
T
22

Try Including this line just after you decleare 'mypaint'

 mypaint.setStyle(Paint.Style.STROKE); 
Tacky answered 25/7, 2011 at 22:32 Comment(3)
how to make it thin?Defloration
that's stretching it :PTacky
it didn't work for me. In JavaDoc, its given like that : NOTE: since a line is always "framed", the Style is ignored in the paint.Kneecap
S
49

Change the value of

myPaint.setStrokeWidth(8);

to a bigger integer, for instance:

myPaint.setStrokeWidth(50);

it will make the line thicker

see also Paint.setStrokeWidth(float)

Skull answered 7/12, 2015 at 15:27 Comment(3)
acutally this worked instead of the "marked right" answerPeaceable
this should be the answer instead!Union
OP is already using setStrokeWidth , the trouble is that the Paint.Style.STROKE wasn't set and so it wasn't taking effect.Tacky
T
22

Try Including this line just after you decleare 'mypaint'

 mypaint.setStyle(Paint.Style.STROKE); 
Tacky answered 25/7, 2011 at 22:32 Comment(3)
how to make it thin?Defloration
that's stretching it :PTacky
it didn't work for me. In JavaDoc, its given like that : NOTE: since a line is always "framed", the Style is ignored in the paint.Kneecap
S
2

What happens if you remove the ANTI_ALIAS_FLAG? Also, you should move the Paint constructor outside the for loop, so it doesn't get recreated every iteration.

Schiedam answered 25/7, 2011 at 22:8 Comment(2)
fair point about creating a paint instance. The ANTI_ALIAS_FLAG is in there because I read on here that it can help render the line #5377552Yarborough
ANTI_ALIAS_FLAG will just smoothen the line if you are trying to draw curves or slanted lines.Tacky
H
1

Try this(kotlin)

myPaint.apply{
            isAntiAlias = true
            color = Color.BLACK
            style = Paint.Style.STROKE
            strokeWidth = 5.dp
        }
Helical answered 19/1, 2022 at 23:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.