我想擦除图像的一部分并将 XferMode 设置为清除图像。但是当我在 android > 3.0 上进行测试时,它工作正常并且在 android < 3.0 (2.2) 上画黑线。我找不到这个问题的解决方案。谁能解释我为什么?这是 TouchView 方法:
public TouchView(Context context) {
super(context);
mPath = new Path();
Display display = getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
int dWidth = display.getWidth();
@SuppressWarnings("deprecation")
int dHeight = display.getHeight();
int id = getIntent().getIntExtra("id", -1);
bgr1 = BitmapFactory.decodeResource(getResources(),BackgroundAdapter.mThumbIds[id]);
bgr = Bitmap.createScaledBitmap(bgr1, dWidth, dHeight, true);
overlay1 = BitmapFactory.decodeResource(getResources(),OverlayAdapter.mThumbIds[id]).copy(Config.ARGB_8888, true);
overlay = Bitmap.createScaledBitmap(overlay1, dWidth, dHeight, true);
c2 = new Canvas(
pTouch = new Paint(/*Paint.ANTI_ALIAS_FLAG*/);
pTouch.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
pTouch.setColor(Color.TRANSPARENT);
pTouch.setDither(true);
pTouch.setStrokeWidth(20);
pTouch.setAntiAlias(true);
pTouch.setFilterBitmap(true);
pTouch.setStyle(Paint.Style.STROKE);
pTouch.setMaskFilter(new BlurMaskFilter(10, Blur.
}
这将绘画设置为 TouchView:
private void touch_start(float x, float y){
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;
mDrawPoint = true;
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);
mPath.lineTo(mX, mY);
c2.drawPath(mPath, pTouch);
mPath.reset();
mPath.moveTo(mX, mY);
mX = x;
mY = y;
}
}
private void touch_up() {
mPath.reset();
}
public boolean onTouchEvent(MotionEvent 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;
}
和 onDraw:
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(bgr, 0, 0, null);
Paint new_paint = new Paint();
new_paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));
new_paint.setStyle(Paint.Style.STROKE );
new_paint.setFilterBitmap(true);
canvas.drawBitmap(overlay, 0, 0, new_paint);
canvas.drawColor(Color.TRANSPARENT);
canvas.isHardwareAccelerated();
c2.drawPath(mPath, pTouch);
}
一世