简书链接:阴影的实现多种方案研究
文章字数:75,阅读全文大约需要1分钟
要实现阴影,解决方法有很多,有直接当前控件携带阴影 移动自己的背景图层,或者是让父容器进行背景图层绘制阴影,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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);*/

}
}