Created
August 14, 2019 06:15
-
-
Save DipaliShah/ed06bfc27256442c7a93d9da2ff6bb1b to your computer and use it in GitHub Desktop.
Android Clipping to round corners of ViewGroup :- Rounded Corner Layout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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