我有一个带有背景和前景图像的 imageView。当我尝试使用具有清除设置的路径和绘画通过前景擦除时,而不是删除前景路径,其绘制黑线。有人能帮助我吗?
这是我的代码片段
public class ShowBackgrounOnDrawImage extends ImageView {
private static final float MINP = 0.25f;
private static final float MAXP = 0.75f;
private Bitmap mBitmap, blur = null;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
public boolean imageLoaded = false;
private Canvas imageCanvas = null;
Context here;
Long width = null, height = null;
private Paint mPaint;
public BlurOnDrawImage(Context c) {
super(c);
initialize();
}
public BlurOnDrawImage(Context context, AttributeSet attrs)
{
super(context, attrs);
initialize();
}
public BlurOnDrawImage(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
initialize();
}
private void initialize() {
mPath = new Path();
mPaint = new Paint();
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
mPaint.setColor(Color.TRANSPARENT);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeWidth(10f);
mPaint.setAntiAlias(true);
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
mBitmapPaint.setStrokeWidth(80f);
}
public void setBitmap(Bitmap bitmap) {
mBitmap = bitmap;
mCanvas = new Canvas(bitmap);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
//mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
// mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (imageLoaded) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
//canvas.drawPath(circlePath, circlePaint);
} else {
//imageCanvas = canvas;
}
}
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
}
private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;
}
//circlePath.reset();
//circlePath.addCircle(mX, mY, 30, Path.Direction.CW);
}
private void touch_up() {
mPath.lineTo(mX, mY);
// commit the path to our offscreen
mCanvas.drawPath(mPath, mPaint);
// kill this so we don't double draw
//circlePath.reset();
mPath.reset();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!imageLoaded) {
return super.onTouchEvent(event);
}
float x = event.getX();
float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}
}
谢谢