Skip to content

Instantly share code, notes, and snippets.

@DipaliShah
Created August 14, 2019 06:15
Show Gist options
  • Save DipaliShah/ed06bfc27256442c7a93d9da2ff6bb1b to your computer and use it in GitHub Desktop.
Save DipaliShah/ed06bfc27256442c7a93d9da2ff6bb1b to your computer and use it in GitHub Desktop.
Android Clipping to round corners of ViewGroup :- Rounded Corner Layout
public class RoundedRelativeLayout extends RelativeLayout {
private RectF rectF;
private Path path = new Path();
private float cornerRadius = 20;
public RoundedRelativeLayout(Context context) {
super(context);
}
public RoundedRelativeLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RoundedRelativeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
rectF = new RectF(0, 0, w, h);
resetPath();
}
@Override
public void draw(Canvas canvas) {
int save = canvas.save();
canvas.clipPath(path);
super.draw(canvas);
canvas.restoreToCount(save);
}
@Override
protected void dispatchDraw(Canvas canvas) {
int save = canvas.save();
canvas.clipPath(path);
super.dispatchDraw(canvas);
canvas.restoreToCount(save);
}
private void resetPath() {
path.reset();
path.addRoundRect(rectF, cornerRadius, cornerRadius, Path.Direction.CW);
path.close();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment