Android canvas draw line - make the line thicker
Solution 1
Try Including this line just after you decleare 'mypaint'
mypaint.setStyle(Paint.Style.STROKE);
Solution 2
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)
Solution 3
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.
Related videos on Youtube
user859348
Updated on January 20, 2022Comments
-
user859348 over 2 years
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)
-
Shaunak almost 13 yearsCan 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.
-
user859348 almost 13 yearsI 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 thin
-
Shaunak almost 13 yearsTry removing the anti_alias flag, and try using 8.0 instead.
-
-
user859348 almost 13 yearsfair 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 stackoverflow.com/questions/5377052/…
-
Shaunak almost 13 yearsANTI_ALIAS_FLAG will just smoothen the line if you are trying to draw curves or slanted lines.
-
AndroidCrazy over 10 yearshow to make it thin?
-
Shaunak over 10 yearsthat's stretching it :P
-
Burhan ARAS almost 10 yearsit 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.
-
cherry-wave over 7 yearsacutally this worked instead of the "marked right" answer
-
Haikal Nashuha about 7 yearsthis should be the answer instead!