private void drawPath() {
mFingerPaint.setStyle(Paint.Style.STROKE);
// 设置两张图片相交时的模式(取下层绘制非交集部分)
mFingerPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mCanvas.drawPath(mPath, mFingerPaint);
}
测量和绘制过程如下:
@Override
protected void onDraw(Canvas canvas) {
canvas.drawText(mText, getWidth() / 2 - mTextBound.width() / 2, getHeight() / 2 + mTextBound.height() / 2, mBackPint);
if (!isComplete) {
drawPath();
canvas.drawBitmap(mBitmap, 0, 0, null);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
// 初始化bitmap
mBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
// 绘制遮盖层
mFingerPaint.setStyle(Paint.Style.FILL);
mCanvas.drawRoundRect(new RectF(0, 0, width, height), 30, 30, mFingerPaint);
mCanvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.mask), null, new RectF(0, 0, width, height), null);
}
此外还有一篇也是使用了此技术的博客,点击这里进行查看。