As mentioned in Defining Shadows and Clipping Views
You should implement ViewOutlineProvider
abstract class by which a View
builds its Outline
, used for shadow casting and clipping
Rectangular CustomView
public class CustomView extends View {
// ..
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
/// ..
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
setOutlineProvider(new CustomOutline(w, h));
}
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private class CustomOutline extends ViewOutlineProvider {
int width;
int height;
CustomOutline(int width, int height) {
this.width = width;
this.height = height;
}
@Override
public void getOutline(View view, Outline outline) {
outline.setRect(0, 0, width, height);
}
}
//...
}
note: This feature is only supported by API21, pre API21 should use 9-patch.