2

I would like to draw a circle overlay in my android app, which I can ofcourse easily do and add a color to it as well.

But what I would really like to do is to keep the circle transparent and shade the outside of the circle (the rest of the world) with a light gray color.

I tried to add the circle as a path, shade the world and clip the circle out, but i get a bounding box around the circle on my device (although it looks fine on the emulator).

Any help appreciated. Below is a code snippet:

public void draw(Canvas canvas, MapView mapView, boolean shadow) { super.draw(canvas, mapView, shadow);

    Projection projection = mapView.getProjection();

    Point pt = new Point();

    GeoPoint geo = new GeoPoint((int) (mLat * 1e6), (int) (mLon * 1e6));

    projection.toPixels(geo, pt);
    float circleRadius = projection.metersToEquatorPixels(mRadius)
            * (1 / FloatMath.cos((float) Math.toRadians(mLat)));

    Paint innerCirclePaint;

    innerCirclePaint = new Paint();
    innerCirclePaint.setColor(Color.TRANSPARENT);
    innerCirclePaint.setAlpha(35);
    innerCirclePaint.setAntiAlias(true);
    innerCirclePaint.setStyle(Paint.Style.FILL);

    Path path = new Path();
    path.setFillType(Path.FillType.WINDING);
    path.addCircle((float) pt.x, (float) pt.y, circleRadius, Path.Direction.CCW);canvas.drawPath(path, innerCirclePaint);
    canvas.drawARGB(150, 0, 0, 0 );
    canvas.clipPath(path);
}

2 Answers2

1
paint.setColor(Color.parseColor ("#88ff0000"));
QuokMoon
  • 4,387
  • 4
  • 26
  • 50
0

Try

path.setFillType(Path.FillType.INVERSE_WINDING);

instead of

path.setFillType(Path.FillType.WINDING); 

I tried it for a normal image and this patch worked for me

    Canvas canvas = new Canvas(bitmap);
    Paint innerCirclePaint;

    innerCirclePaint = new Paint();
    innerCirclePaint.setColor(Color.parseColor ("#88ff0000"));
    innerCirclePaint.setAlpha(35);
    innerCirclePaint.setAntiAlias(true);
    innerCirclePaint.setStyle(Paint.Style.FILL);

    Path path = new Path();
    path.setFillType(Path.FillType.INVERSE_WINDING);
    path.addCircle(150, 150, 150, Path.Direction.CCW);

    canvas.drawPath(path, innerCirclePaint);
    canvas.drawARGB(0, 0, 0, 0 );
    canvas.clipPath(path);

And even clipPath could have been a reason

Work around Canvas.clipPath() that is not supported in android any more

Community
  • 1
  • 1
DeRagan
  • 22,827
  • 6
  • 41
  • 50