1.初始化paint
Paint shadowPaint;
shadowPaint = new Paint();
shadowPaint.setColor(backgoundColor);
shadowRadius = DensityUtil.dp2px(ShadowApp.context(), 3);
shadowPaint.setShadowLayer(shadowRadius, 0, 0, shadowColor);
2.初始化path
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
path = new Path();
path.reset();
path.addRoundRect(new RectF(shadowRadius, shadowRadius, getMeasuredWidth()- shadowRadius, getMeasuredHeight()- shadowRadius), roundRadius, roundRadius, Path.Direction.CW);
}
3.绘制:
@Override
protected void dispatchDraw(Canvas canvas) {
canvas.drawRoundRect(new RectF(shadowRadius, shadowRadius, getMeasuredWidth() - shadowRadius, getMeasuredHeight() - shadowRadius), roundRadius, roundRadius, shadowPaint);
canvas.clipPath(path, Region.Op.REPLACE);
super.dispatchDraw(ca