I am making an extends class LinearLayout I want to show Rect like this:
--------------------- => it is round corner
| border frame | .
| ----------------- | .
| | hearder | | .
| | - - - - - - - | | .
| | center | | .
| | | | .
| | - - - - - - - | | .
| | buttom | | .
| ---------------- | .
| | .
---------------------- .
. . . . . . . . . . . => it is shadow
and I used paintShadow.setShadowLayer (this.radius, 8, 8, Color.GRAY); the shadow is not round.
so who knows how to make a round rectangle and shadow. the code:
@SuppressLint("DrawAllocation")
public class CornerLinearLayout extends LinearLayout{
public CornerLinearLayout(Context context,AttributeSet attr){
super(context,attr);
init();
}
public CornerLinearLayout(Context context) {
super(context);
init();
}
private static final int RADIUS=10;
private int frameColor;
private int radius;
private Path mClip;
private int frameWidth;
private int headerHeight;
private int buttomHeight;
private int headerColor;
private int buttomColor;
private int centerColor;
private void init() {
this.radius = RADIUS;
this.frameColor = 0xFFFFFFFF;
this.frameWidth = 4;
this.headerHeight = 50;
this.buttomHeight = 50;
headerColor = 0xFF31234A;
buttomColor = 0xFF9ACFFF;
centerColor = 0xFF55AACC;
this.setBackgroundColor(0);
this.setPadding(frameWidth, frameWidth, frameWidth,frameWidth);
}
private Paint paintShadow = new Paint();
private Paint paint = new Paint();
public void onDraw(Canvas canvas){
super.onDraw(canvas);
RectF rf = new RectF(0,0,this.getWidth()-10,this.getHeight()-5);
paintShadow.setShadowLayer(this.radius, 8, 8, Color.GRAY);
rf.right -= 3;
rf.bottom -= 3;
paintShadow.setColor(this.frameColor);
paintShadow.setStrokeWidth(5);
canvas.drawRoundRect(rf,this.radius,this.radius, paintShadow);
paint.setStyle(Paint.Style.FILL);
if(this.centerColor != 0){
paint.setColor(this.centerColor);
rf.right -= 5;
rf.left += 5;
rf.top += 5;
rf.bottom -=5;
canvas.drawRoundRect(rf, this.radius, this.radius, paint);
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mClip = new Path();
RectF rect = new RectF(0, 0, w, h);
mClip.addRoundRect(rect, this.radius, this.radius, Direction.CW);
}
@Override
protected void dispatchDraw(Canvas canvas) {
canvas.save();
canvas.clipPath(mClip);
super.dispatchDraw(canvas);
canvas.restore();
}
}