蒙层绘制
基本思路:将一个矩形填满画布并涂上半透明的颜色,然后通过设置PorterDuffXfermode来挖出一块空洞
定义值
private int mViewLY,mViewLX;//展示View位置
private int mViewHeight,mViewWidth,mViewMax;// 展示 View的宽高以及其中最大值
private Paint mPaintTranslucent;//画笔 背景
private Paint mPaintLucent;//画笔 高亮区
Print 常用属性
mPaint.setColor(int);//设置颜色值
mPaint.setAlpha(int);//设置透明度
mPaint.setStyle(Paint.Style);//设置样式 FILL,FILL_OR_STROKE,或STROKE
mPaint.setStrokeWidth(int);//设置画笔宽度
mPaint.setXfermode(Xfermode);//设置图像重叠时的处理方式
mPaint.setAntiAlias(boolean)://设置是否抗锯齿
mPaint. setTextSize(float);//设置文字大小
Canvans 常用属性
mCanvas.drawRect(RectF, Paint);//矩形
mCanvas.drawPath(Path, Paint);//路径
mCanvas.drawArc(RectF,float 起始角度 ,float 跨越角度,boolean 是否闭合, Paint);//画弧
mCanvas .drawCircle(float X, float Y , float radius,Paint)
onMeasure中 获取整个视图大小
mViewWidth = MeasureSpec.getSize(widthMeasureSpec);
mViewHeight = MeasureSpec.getSize(heightMeasureSpec);
获取需高亮展示View大小以及位置
//获得 需高亮展示View大小
mView.measure(0,0);
mViewHeight = mView.getMeasuredHeight();
mViewWidth = mView.getMeasuredWidth();
//宽高中最大值
if(mViewHeight>mViewWidth){
mViewMax = mViewHeight;
} else{
mViewMax = mViewWidth;
}
if(mViewLX == 0){
//获得位置
int[] locationTwo = new int[2];
mView.getLocationOnScreen(locationTwo);
mViewLX = locationTwo[0];
mViewLY = locationTwo[1];
}
获得状态栏高度 沉浸式状态栏时不需要
private int getStatusBarHeight() {
int statusBarHeight = -1;
int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
if (resourceId > 0) { statusBarHeight = getResources().getDimensionPixelSize(resourceId); }
return statusBarHeight;
}
获取整个视图大小 并根据其新建一块画布
Canvas bitmapCanvas = new Canvas(Bitmap.createBitmap(mViewWidth, mViewHeight, Bitmap.Config.ARGB_8888));
通过坐标 计算圆心坐标以及半径
int mCircleX = mViewLX + (mViewWidth/2);//X坐标
int mCircleY = isBarH?mViewLY+(mViewHeight/2)-(mTop+getStatusBarHeight()):mViewLY+(mViewHeight/2)-mTop;//Y坐标
float mRadius = 1.05f;//半径扩充 使圆比View稍大一些
int mR = (int)(mViewMax/2 * mRadius);//半径
画展示区域 设置PorterDuffXfermode为PorterDuff.Mode.SRC_OUT模式
//给画笔设置 PorterDuffXfermode
PorterDuffXfermode mode = new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT);
mPaintLucent.setXfermode(mode);
//画背景以及展示区
bitmapCanvas.drawRect(0, 0, mThisViewWidth, mThisViewHeight, mPaintTranslucent);
bitmapCanvas.drawPath(path, mPaintLucent);
本人Android小白一枚,如有错误,请指正