如定义阴影和剪切视图中所述
您应该实现构建它ViewOutlineProvider
的抽象类,用于阴影投射和剪辑View
Outline
矩形自定义视图
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);
}
}
//...
}
注意:此功能仅 API21 支持,API21 前应使用 9-patch。