1

I want to set a clip region corresponding to a RoundRectShape onto my canvas, i.e. anything I draw will be clipped against the round rectangle.

I was looking at Canvas.clipRegion() and at Region itself and it seems fairly simple to set a clipping region. The problem is that I can't see any easy way of converting a round rectangle into one. I thought I might be able to construct a RoundRectShape, wrap it in a ShapeDrawable and call ShapeDrawable.getTransparentRegion() to get the region for the non visible part. Then it would be a simple matter to invert the region and set the clip region. Except getTransparentRegion() is return null so code such as the following would not work.

Any ideas?

    final float radii[] = { 8, 8, 8, 8, 8, 8, 8, 8 };
    final Shape shape = new RoundRectShape(radii, null, null);
    shape.resize(getWidth(), getHeight());

    ShapeDrawable shapeDrawable = new ShapeDrawable(shape);
    final Region clipRegion = shapeDrawable.getTransparentRegion();
    clipRegion.op(shapeDrawable.getBounds(), Region.Op.DIFFERENCE);
    canvas.clipRegion(clipRegion);
locka
  • 5,809
  • 3
  • 33
  • 38
  • I was trying to do something similar; and this answer on another question helped me: http://stackoverflow.com/a/8132932/68034 – mlsteeves Feb 05 '12 at 08:01

0 Answers0