我正在尝试使用圆形倒置剪裁绘制蒙版位图。预期的效果应该看起来像太阳光线从圆的边缘向一个方向发光。掩蔽工作得很好,但剪掉内圈是行不通的。有什么我想念的吗?
这是 ImageView 的 onDraw 方法,带有一个额外的掩码位图。在 eclipse ADT 工具中预览视图时,使用此代码几乎可以正常工作(Xfermode 除外,它在预览中不受支持),但在设备上会忽略剪辑。
@Override
protected void onDraw(Canvas canvas) {
if (originalBounds == null) {
originalBounds = new Rect();
getDrawingRect(originalBounds);
maskDrawable.setBounds(originalBounds);
maskBitmap = drawableToBitmap(maskDrawable, getWidth(),
getHeight(), Config.ARGB_8888);
bitmap = drawableToBitmap(getDrawable(), getWidth(), getHeight(),
Config.ARGB_8888);
}
if (clipPath == null && clipPathRadius != 0) {
clipPath = new Path();
clipPath.addCircle(originalBounds.exactCenterX(),
originalBounds.exactCenterY(), clipPathRadius,
Path.Direction.CCW);
}
canvas.saveLayer(originalBounds.left, originalBounds.top,
originalBounds.right, originalBounds.bottom, null,
Canvas.MATRIX_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
| Canvas.FULL_COLOR_LAYER_SAVE_FLAG
| Canvas.CLIP_SAVE_FLAG
| Canvas.CLIP_TO_LAYER_SAVE_FLAG);
if (clipPath != null) {
canvas.clipPath(clipPath, Op.DIFFERENCE);
}
paint.setXfermode(null);
canvas.drawBitmap(bitmap, null, originalBounds, paint);
paint.setXfermode(xfermode);
canvas.saveLayer(originalBounds.left, originalBounds.top,
originalBounds.right, originalBounds.bottom, paint,
Canvas.MATRIX_SAVE_FLAG | Canvas.HAS_ALPHA_LAYER_SAVE_FLAG
| Canvas.FULL_COLOR_LAYER_SAVE_FLAG
| Canvas.CLIP_SAVE_FLAG
| Canvas.CLIP_TO_LAYER_SAVE_FLAG);
paint.setXfermode(null);
canvas.rotate(maskRotation, originalBounds.exactCenterX(),
originalBounds.exactCenterY());
if (clipPath != null) {
canvas.clipPath(clipPath, Op.DIFFERENCE);
}
canvas.drawBitmap(maskBitmap, null, originalBounds, paint);
canvas.rotate(0, originalBounds.exactCenterX(),
originalBounds.exactCenterY());
canvas.restore();
canvas.restore();
}
这是预览显示的内容,可以清楚地看到中间的圆圈被剪掉了,但这在设备上并没有发生: