触摸位置和擦除位置不匹配

时间:2017-11-16 07:00:45

标签: android canvas matrix bitmap

我正在制作照片编辑应用程序,在我的活动中我有一个位图,当它没有完全缩放或旋转其绘图时会触摸擦除但是在我缩放或旋转位图之后,橡皮擦在触摸时不会被擦除points.its根据位图缩放在其他位置上擦除。

我该如何解决这个问题,请帮助我。

Drawingview.java

public class DrawingView extends ImageView implements View.OnTouchListener,MatrixGestureDetector.OnMatrixChangeListener {

private int ERASE = 1;
private int ZOOMTOUCH = 5;



private Matrix matrix = new Matrix();
Matrix inverse = new Matrix();
MatrixGestureDetector mgd = new MatrixGestureDetector(matrix, this);
@Override
public void onChange(Matrix matrix) {
invalidate();
}  
public interface ActionListener {
    void onActionCompleted(int i);
}
public void setImageBitmap(Bitmap bm) {
    if (bm != null) {
        bmp = bm;
        orgBit = bm.copy(Bitmap.Config.ARGB_8888, true);
        Bitmap2 = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
        c2 = new Canvas();
        c2.setBitmap(Bitmap2);

        c2.drawBitmap(bmp, 0, 0, null);

    }

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    canvas.concat(matrix);
    canvas.drawBitmap(Bitmap2,0,0, null);

    if (c2 != null) {
        Paint p;

        if (!updateOnly) {

            if (isTouched) {

                paint = getPaintByMode(MODE, brushSize);
                c2.drawPath(tPath, paint);

                isTouched = false;
            } else if (curIndx >= 0 && drawOnLasso) {
                redrawCanvas();
            }
        }

        if (MODE == ERASE) {
            p = new Paint();
            p.setColor(SupportMenu.CATEGORY_MASK);
            erPaint.setStrokeWidth(updatebrushsize(erps, scale));
            canvas.drawCircle(f887X, f888Y, (float) (brushSize / 2), erPaint);
            canvas.drawCircle(f887X, f888Y + ((float) offset), updatebrushsize(ImageUtils.dpToPx(getContext(), 7), scale), p);
        }
        if (MODE == ZOOMTOUCH) {

        }
        updateOnly = false;
    }
}

@Override
public boolean onTouch(View v, MotionEvent event) {


    if (MODE == ERASE) {
        f887X = event.getX();
        f888Y = event.getY() - ((float) offset);
        isTouched = true;
        switch (event.getAction()) {
            case 0:
                paint.setStrokeWidth((float) brushSize);
                tPath = new Path();
                tPath.moveTo(f887X, f888Y);
                drawPath.moveTo(f887X, f888Y);
                invalidate();
                break;
            case 1:
                drawPath.lineTo(f887X, f888Y);
                tPath.lineTo(f887X, f888Y);
                invalidate();
                changesIndx.add(curIndx + 1, new Path(tPath));
                brushIndx.add(curIndx + 1, Integer.valueOf(brushSize));
                modeIndx.add(curIndx + 1, Integer.valueOf(MODE));
                vectorPoints.add(curIndx + 1, null);
                lassoIndx.add(curIndx + 1, Boolean.valueOf(insidCutEnable));
                tPath.reset();
                curIndx++;
                clearNextChanges();
                break;
            case 2:
                drawPath.lineTo(f887X, f888Y);
                tPath.lineTo(f887X, f888Y);
                invalidate();
                break;
            default:
                return false;
        }
    }
    if (MODE == ZOOMTOUCH) {
       matrix.invert(inverse);
        float[] pts = {event.getX(), event.getY()};
        inverse.mapPoints(pts);
        mgd.onTouchEvent(event);
    }

    return true;
}

}
class MatrixGestureDetector {
        private static final String TAG = "MatrixGestureDetector";

        private int ptpIdx = 0;
        private Matrix mTempMatrix = new Matrix();
        private Matrix mMatrix;
        private OnMatrixChangeListener mListener;
        private float[] mSrc = new float[4];
        private float[] mDst = new float[4];
        private int mCount;

        interface OnMatrixChangeListener {
            void onChange(Matrix matrix);
        }

        public MatrixGestureDetector(Matrix matrix, MatrixGestureDetector.OnMatrixChangeListener listener) {
            this.mMatrix = matrix;
            this.mListener = listener;
        }

        public void onTouchEvent(MotionEvent event) {
            if (event.getPointerCount() > 2) {
                return;
            }

            int action = event.getActionMasked();
            int index = event.getActionIndex();

            switch (action) {
                case MotionEvent.ACTION_DOWN:
                case MotionEvent.ACTION_POINTER_DOWN:
                    int idx = index * 2;
                    mSrc[idx] = event.getX(index);
                    mSrc[idx + 1] = event.getY(index);
                    mCount++;
                    ptpIdx = 0;
                    break;

                case MotionEvent.ACTION_MOVE:
                    for (int i = 0; i < mCount; i++) {
                        idx = ptpIdx + i * 2;
                        mDst[idx] = event.getX(i);
                        mDst[idx + 1] = event.getY(i);
                    }
                    mTempMatrix.setPolyToPoly(mSrc, ptpIdx, mDst, ptpIdx, mCount);
                    mMatrix.postConcat(mTempMatrix);
                    if (mListener != null) {
                        mListener.onChange(mMatrix);
                    }
                    System.arraycopy(mDst, 0, mSrc, 0, mDst.length);
                    break;

                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_POINTER_UP:
                    if (event.getPointerId(index) == 0) ptpIdx = 2;
                    mCount--;
                    break;
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

在此链接Android ImageView Scaling and translating issue

上找到解决方案thanx to pskink

通过在图层中使用反转矩阵包含方法。