假设我有500 x 500的ImageView,一半的ImageView是红色的第二部分是透明的。 我只想在ImageVIew的非透明部分用手指画画,所以如果我在透明部分绘制它将不可见,就像这个图像一样。 可能吗 ?
我试过但这段代码不正确。
public class DrawingView extends ImageView {
private boolean isTransparent;
private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;
private Bitmap mBitmap;
private Canvas mCanvas;
private Path mPath;
private Paint mBitmapPaint;
Context mContext;
private Path circlePath;
public DrawingView(Context c) {
super(c);
mContext = c;
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
circlePath = new Path();
}
@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_4444);
mCanvas = new Canvas(mBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (mBitmap != null && canvas != null && !isTransparent) {
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
}
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) {
if(mBitmap.getPixel((int)x, (int)y) == Color.TRANSPARENT || mBitmap.getPixel((int)mY, (int)mY) == Color.TRANSPARENT) {
isTransparent = true;
} else {
isTransparent = false;
}
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);
circlePath.reset();
mCanvas.drawPath(mPath, mPaint);
mPath.reset();
}
@Override
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;
}
}
答案 0 :(得分:1)
我不知道mPaint
初始化的位置,但由于mPaint
用于绘制手指痕迹,因此将此行放入应该执行的操作:
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_ATOP));
请参阅this image了解相关信息。