Android canvas draw line - make the line thicker

39,254

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.

Share:
39,254

Related videos on Youtube

user859348
Author by

user859348

Updated on January 20, 2022

Comments

  • user859348
    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
      Shaunak almost 13 years
      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.
    • user859348
      user859348 almost 13 years
      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 thin
    • Shaunak
      Shaunak almost 13 years
      Try removing the anti_alias flag, and try using 8.0 instead.
  • user859348
    user859348 almost 13 years
    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 stackoverflow.com/questions/5377052/…
  • Shaunak
    Shaunak almost 13 years
    ANTI_ALIAS_FLAG will just smoothen the line if you are trying to draw curves or slanted lines.
  • AndroidCrazy
    AndroidCrazy over 10 years
    how to make it thin?
  • Shaunak
    Shaunak over 10 years
    that's stretching it :P
  • Burhan ARAS
    Burhan ARAS almost 10 years
    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.
  • cherry-wave
    cherry-wave over 7 years
    acutally this worked instead of the "marked right" answer
  • Haikal Nashuha
    Haikal Nashuha about 7 years
    this should be the answer instead!