Batdude
Batdude

Reputation: 556

Canvas Drawing Accuracy Problems

Someone help me understand the crazy Canvas class in Android. It doesn't seem accurate and I am not quite sure why the parameters are floating point values. For example, here is the polygon I want to draw, with absolute point values:

       mPaint.setStyle(Paint.Style.STROKE);
       mPaint.setColor(Color.RED);
       mPaint.setAntiAlias(false);
       mPaint.setDither(false);
       mPaint.setFilterBitmap(false);

       Path path1 = new Path();
       path1.moveTo(151, 100);
       path1.lineTo(200, 200);
       path1.lineTo(100, 151);
       path1.lineTo(200, 151);
       path1.lineTo(100, 201);
       path1.lineTo(151, 100);
       //   So that these points constitute a closed polygon  
       path1.setFillType(Path.FillType.EVEN_ODD);
       mPaint.setStyle(Paint.Style.FILL);
       mPaint.setColor(Color.GRAY);
       //   Draw the polygon  
       mCanvas.drawPath(path1, mPaint);

When I run similar code in Windows with the Windows GDI, it nails the points exactly. In android, however, there are two problems:

1) The line seems anti-aliased (there are blackish-pixels around the gray lines) even though I turned off anti-aliasing. 2) It doesn't always hit the specified points. Sometimes it does, sometimes it doesn't

In order to get closer to the points I wanted, I have to do this:

       Path path1 = new Path();
       path1.moveTo(151, 100-1);
       path1.lineTo(200+1, 200+1);
       path1.lineTo(100-1, 151);
       path1.lineTo(200+2, 151);
       path1.lineTo(100, 201);
       path1.lineTo(151+1, 100);

I can't figure out the rules here or what its doing that is so odd.

Upvotes: 0

Views: 232

Answers (1)

Raykud
Raykud

Reputation: 2484

That is nore like the Path class what you mean and not the Canvas. Now I recommend you to set the dither to true mPaint.setDither(true), allow me to quote the dither method:

Helper for setFlags(), setting or clearing the DITHER_FLAG bit Dithering affects how colors that are higher precision than the device are down-sampled. No dithering is generally faster, but higher precision colors are just truncated down (e.g. 8888 -> 565). Dithering tries to distribute the error inherent in this process, to reduce the visual artifacts.

in other words it tells you that there could be an issue parsing the colors. So that could be the one that is causing you the issue.

Upvotes: 1

Related Questions