Android 6.0 does not handle drawCircle method correctly

In my application, I need to draw circles using a bitmap and the drawCircle () method.

Everything worked perfectly and exactly the same as before Android 6.0.

It still draws circles in all previous versions, but draws rectangles when I use the application in 6.0 format. But , if I change it to fill, it draws a circle in both api 22 and api 23. Does anyone have the same problem or some idea why this happens?

Here is the source code and a screenshot (the application runs on API 23 on the left and API 22 on the right). same application for different api

public final class Circle1View extends View { private float xCenter, yCenter; private Bitmap grid = null; public Circle1View (Context context) { super(context); init(); } private void init() { setLayerType(View.LAYER_TYPE_SOFTWARE, null); } @Override protected void onDraw(Canvas canvas) { int w = getWidth(); int h = getHeight(); xCenter = w / 2; yCenter = h / 2; drawBitmaps(w, h); canvas.translate(xCenter, yCenter); canvas.scale(xCenter, yCenter); canvas.drawBitmap(grid, null, new RectF(-1, -1, 1, 1), null); } private void drawBitmaps(int w, int h) { grid = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(); canvas.translate(xCenter, yCenter); canvas.scale(xCenter, yCenter); Paint gridPaint = new Paint(); gridPaint.setStrokeWidth(0.01f); // Works with FILL // gridPaint.setStyle(Paint.Style.FILL); gridPaint.setStyle(Paint.Style.STROKE); canvas.setBitmap(grid); canvas.drawCircle(0, 0, 0.5f, gridPaint); } } 
+8
android android-6.0-marshmallow
source share
1 answer

I think this has something to do with scaling and translation. Imagine a circle that is drawn so small takes only 4 pixels. When you increase this size to full size, you are left with 4 straight lines between these pixels.

When I change the stroke width to 0.04f, the problem will disappear. I would suggest you simplify the code directly using the supplied Canvas :

 @Override protected void onDraw(Canvas canvas) { int w = getWidth(); int h = getHeight(); xCenter = w / 2; yCenter = h / 2; Paint gridPaint = new Paint(); gridPaint.setStrokeWidth(1f); gridPaint.setStyle(Paint.Style.STROKE); canvas.drawCircle(xCenter, yCenter, w/4, gridPaint); } 

Regarding your question about the difference between API levels: Marshmallow introduced changes for drawBitmap() . You can see the corresponding source code for Lollipop and Marshmallow .

+1
source share

All Articles