Clipping is an expensive operation, and is only truly supported on Lollipop and higher.
If you're okay with Lollipop+ only, and really want to use a RelativeLayout
, you can call myView.setClipToOutline(true)
. If your myView
has a rounded background, this will tell the system to clip all children to that rounded shape.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
myView.setClipToOutline(true);
}
If you need rounded corner clipping on pre-Lollipop versions, then the best thing to do is use an overlay to mask parts of the view you don't want to see. That is, create a drawable with opaque corners and a transparent center, and apply that on top of the views you want to clip. This won't actually clip your views, but it will provide the illusion of doing so.
More on this strategy here: https://www.techrepublic.com/article/pro-tip-round-corners-on-an-android-imageview-with-this-hack/
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…