packagecom.etwod.yulin.t4.unit;importandroid.content.Context;importandroid.content.res.TypedArray;importandroid.graphics.Bitmap;importandroid.graphics.Canvas;importandroid.graphics.Paint;importandroid.graphics.PorterDuff;importandroid.graphics.PorterDuffXfermode;importandroid.graphics.RectF;importandroid.support.annotation.ColorInt;importandroid.util.AttributeSet;importandroid.view.View;importcom.etwod.yulin.android.R;/*** A circular cover view.Above other views to hide the conner.You can set radians by self.
* Created by
* 磊磊tua
* 覆盖形成圆角*/
public class CircularCoverView extendsView {private int leftTopRadians = 30; //leftTopRadians
private int leftBottomRadians = 30; //leftBottomRadians
private int rightTopRadians = 30; //rightTopRadians
private int rightBottomRadians = 30; //rightBottomRadians
private int coverColor = 0xffeaeaea; //color of cover.
publicCircularCoverView(Context context) {this(context, null, 0);
}publicCircularCoverView(Context context, AttributeSet attrs) {this(context, attrs, 0);
}public CircularCoverView(Context context, AttributeSet attrs, intdefStyleAttr) {super(context, attrs, defStyleAttr);
TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.CircularCoverView);
leftTopRadians=typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_top_radius, leftTopRadians);
leftBottomRadians=typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_left_bottom_radius, leftBottomRadians);
rightTopRadians=typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_top_radius, rightTopRadians);
rightBottomRadians=typedArray.getDimensionPixelSize(R.styleable.CircularCoverView_right_bottom_radius, rightBottomRadians);
coverColor=typedArray.getColor(R.styleable.CircularCoverView_cover_color, coverColor);
}/*** set radians of cover.*/
public void setRadians(int leftTopRadians, int rightTopRadians, int leftBottomRadians, intrightBottomRadians) {this.leftTopRadians =leftTopRadians;this.rightTopRadians =rightTopRadians;this.leftBottomRadians =leftBottomRadians;this.rightBottomRadians =rightBottomRadians;
}/*** set color of cover.
*
*@paramcoverColor cover's color*/
public void setCoverColor(@ColorInt intcoverColor) {this.coverColor =coverColor;
}/*** create a sector-bitmap as the dst.
*
*@paramw width of bitmap
*@paramh height of bitmap
*@returnbitmap*/
private Bitmap drawSector(int w, inth) {
Bitmap bm=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c= newCanvas(bm);
Paint p= newPaint(Paint.ANTI_ALIAS_FLAG);
p.setColor(0xFFFFCC44);//notice:cannot set transparent color here.otherwise cannot clip at final.
c.drawArc(new RectF(0, 0, leftTopRadians * 2, leftTopRadians * 2), 180, 90, true, p);
c.drawArc(new RectF(0, getHeight() - leftBottomRadians * 2, leftBottomRadians * 2, getHeight()), 90, 90, true, p);
c.drawArc(new RectF(getWidth() - rightTopRadians * 2, 0, getWidth(), rightTopRadians * 2), 270, 90, true, p);
c.drawArc(new RectF(getWidth() - rightBottomRadians * 2, getHeight() - rightBottomRadians * 2, getWidth(), getHeight()), 0, 90, true, p);returnbm;
}/*** create a rect-bitmap as the src.
*
*@paramw width of bitmap
*@paramh height of bitmap
*@returnbitmap*/
private Bitmap drawRect(int w, inth) {
Bitmap bm=Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
Canvas c= newCanvas(bm);
Paint p= newPaint(Paint.ANTI_ALIAS_FLAG);
p.setColor(coverColor);
c.drawRect(new RectF(0, 0, leftTopRadians, leftTopRadians), p);
c.drawRect(new RectF(0, getHeight() -leftBottomRadians, leftBottomRadians, getHeight()), p);
c.drawRect(new RectF(getWidth() - rightTopRadians, 0, getWidth(), rightTopRadians), p);
c.drawRect(new RectF(getWidth() - rightBottomRadians, getHeight() -rightBottomRadians, getWidth(), getHeight()), p);returnbm;
}
@Overrideprotected voidonDraw(Canvas canvas) {super.onDraw(canvas);
Paint paint= newPaint();
paint.setFilterBitmap(false);
paint.setStyle(Paint.Style.FILL);//create a canvas layer to show the mix-result
int sc = canvas.saveLayer(0, 0, getWidth(), getHeight(), null, Canvas.MATRIX_SAVE_FLAG |Canvas.CLIP_SAVE_FLAG|Canvas.HAS_ALPHA_LAYER_SAVE_FLAG|Canvas.FULL_COLOR_LAYER_SAVE_FLAG|Canvas.CLIP_TO_LAYER_SAVE_FLAG);//draw sector-dst-bitmap at first.
canvas.drawBitmap(drawSector(getWidth(), getHeight()), 0, 0, paint);//set Xfermode of paint.
paint.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_OUT));//then draw rect-src-bitmap
canvas.drawBitmap(drawRect(getWidth(), getHeight()), 0, 0, paint);
paint.setXfermode(null);//restore the canvas
canvas.restoreToCount(sc);
}
}