title: Custom Layout that rounds the corners of its content
body: <p>I would like to create a generic ViewGroup which can then be reused in XML layouts to round the corners of anything that is put into it.</p>
  
  <p>For some reason <code>canvas.clipPath()</code> doesn't seem to have an effect. What am I doing wrong?</p>
  
  <p>Here is the Java code:</p>
  
  <pre><code>package rounded;
  
  import static android.graphics.Path.Direction.CCW;
  public class RoundedView extends FrameLayout {
      private float radius;
      private Path path = new Path();
      private RectF rect = new RectF();
  
      public RoundedView(Context context, AttributeSet attrs) {
          super(context, attrs);
          this.radius = attrs.getAttributeFloatValue(null, "corner_radius", 0f);
      }
  
      @Override
      protected void onDraw(Canvas canvas) {
          int savedState = canvas.save();
          float w = getWidth();
          float h = getHeight();
          path.reset();
          rect.set(0, 0, w, h);
          path.addRoundRect(rect, radius, radius, CCW);
          path.close();
          boolean debug = canvas.clipPath(path);
          super.onDraw(canvas);
          canvas.restoreToCount(savedState);
      }
  }
  </code></pre>
  
  <p>Usage in XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;rounded.RoundedView   xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent" 
      corner_radius="40.0" &gt;
      &lt;RelativeLayout 
          android:id="@+id/RelativeLayout1"
          android:layout_width="match_parent"
          android:layout_height="match_parent" &gt;
          ...
      &lt;/RelativeLayout&gt;
  &lt;/rounded.RoundedView&gt;
  </code></pre>