要实现阴影,解决方法有很多,有直接当前控件携带阴影 移动自己的背景图层,或者是让父容器进行背景图层绘制阴影,
public class CustomShadowLayerView extends View {
public CustomShadowLayerView(Context context) {
super(context);
}
public CustomShadowLayerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public CustomShadowLayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public CustomShadowLayerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.scale(1,1);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Paint.Style.FILL);
setLayerType( LAYER_TYPE_SOFTWARE , null);
paint.setStrokeWidth(50);
paint.setShadowLayer(50, 30, 30, Color.RED);
canvas.drawRect(new Rect(0,0,getWidth()-50,getHeight()-50),paint);
super.onDraw(canvas);
/* paint.setShadowLayer(5, 30, 30, Color.RED);
canvas.drawRect(new Rect(0,0,50,50),paint);*/
}
}