我的Imageview在Max级别上缩放后停止移动

时间:2017-04-19 05:27:20

标签: android image-zoom

在我的Android App中我使用了 ZoomImageView 类。除了我的ImageView.it之外,所有工作都完美,在达到 MAX 缩放级别后停止缩放,拖动和旋转。

以下是我的 onTouchEvent()

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

        mScaleDetector.onTouchEvent(event);
        ImageView view = (ImageView) v;
        float scale;
        dumpEvent(event);
        // Handle touch events here...

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_CANCEL:
                touch = false;
                invalidate();
                break;
            case MotionEvent.ACTION_DOWN:
                if (isInBitmap(event)) {
                    touch = true;
                    savedMatrix.set(matrix);
                    if (MainActivity.mCurrentView != null) {
                        MainActivity.mCurrentView.setInEdit(false);
                        MainActivity.mCurrentView.setInEdit(false);
                    }
                    if (bottom.getVisibility() == INVISIBLE) {
                        rel.bringToFront();
                        setAlpha(0.6f);
                    }
                    bottom.setVisibility(INVISIBLE);
                    start.set(event.getX(), event.getY());
                    mode = DRAG;
                    left.setVisibility(VISIBLE);
                    right.setVisibility(VISIBLE);
                    bottom.setVisibility(INVISIBLE);
                    invalidate();
                    postInvalidate();
                    requestLayout();
                }
                break;
            case MotionEvent.ACTION_UP:
                touch = false;
                rel_frame.bringToFront();
                left.bringToFront();
                right.bringToFront();
                bottom.bringToFront();
                // MainActivity.drawabletext.bringToFront();
                setAlpha(1.0f);// first finger lifted
                invalidate();

                break;
            case MotionEvent.ACTION_POINTER_UP: // second finger lifted
                mode = NONE;
                //lastEvent=null;
                invalidate();
                break;

            case MotionEvent.ACTION_POINTER_DOWN: // first and second finger down
                if (isInBitmap(event)) {
                    touch = true;
                    if (bottom.getVisibility() == INVISIBLE) {
                        rel.bringToFront();
                        setAlpha(0.6f);
                    }
                    oldDist = spacing(event);
                    if (oldDist > 5f) {
                        savedMatrix.set(matrix);
                        midPoint(mid, event);
                        mode = ZOOM;
                    }
                    if (oldDist > 10f) {
                        savedMatrix.set(matrix);
                        midPoint(mid, event);
                    }
                    lastEvent = new float[4];
                    lastEvent[0] = event.getX(0);
                    lastEvent[1] = event.getX(1);
                    lastEvent[2] = event.getY(0);
                    lastEvent[3] = event.getY(1);
                    d = rotation(event);
                    invalidate();
                    postInvalidate();
                    requestLayout();
                }

                break;
            case MotionEvent.ACTION_MOVE:

                if (isInBitmap(event)) {

                    if (mode == DRAG) {
                        matrix.set(savedMatrix);
                        matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); // create the transformation in the matrix  of points
                    } else if (mode == ZOOM) {
                        // pinch zooming
                        float newDist = spacing(event);
                        Log.d(TAG, "newDist=" + newDist);
                        if (newDist > 5f) {
                            matrix.set(savedMatrix);
                             scale = newDist / oldDist;
                            matrix.postScale(scale, scale, mid.x, mid.y);

                        }
                        if (lastEvent != null) {
                            newRot = rotation(event);
                            float r = newRot - d;
                            matrix.postRotate(r, getMeasuredWidth() / 2,
                                    getMeasuredHeight() / 2);
                        }
                    }
                    invalidate();
                    postInvalidate();
                }
                break;
        }
        invalidate();
        postInvalidate();
        requestLayout();
        view.setImageMatrix(matrix);
        invalidate();
        postInvalidate();
        requestLayout();// display the transformation on screen
        return true;
    }

请任何人帮我解决这个问题。谢谢你提前

1 个答案:

答案 0 :(得分:1)

onTouch 替换为您,让我知道..

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

    view1 = (ImageView) v;
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            savedMatrix.set(matrix);
            start.set(event.getX(), event.getY());
            mode = DRAG;
            lastEvent = null;
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            oldDist = spacing(event);
            if (oldDist > 10f) {
                savedMatrix.set(matrix);
                midPoint(mid, event);
                mode = ZOOM;
            }
            lastEvent = new float[4];
            lastEvent[0] = event.getX(0);
            lastEvent[1] = event.getX(1);
            lastEvent[2] = event.getY(0);
            lastEvent[3] = event.getY(1);
            d = rotation(event);
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_POINTER_UP:
            mode = NONE;
            lastEvent = null;
            break;
        case MotionEvent.ACTION_MOVE:
            if (mode == DRAG) {
                matrix.set(savedMatrix);
                float dx = event.getX() - start.x;
                float dy = event.getY() - start.y;
                matrix.postTranslate(dx, dy);
            } else if (mode == ZOOM) {
                float newDist = spacing(event);
                if (newDist > 10f) {
                    matrix.set(savedMatrix);
                    float scale = (newDist / oldDist);
                    matrix.postScale(scale, scale, mid.x, mid.y);
                }

                if (lastEvent != null) {
                    newRot = rotation(event);
                    float r = newRot - d;
                    matrix.postRotate(r, view1.getWidth() / 2,
                            view1.getHeight() / 2);
                }
            }
            break;
    }
    view1.setImageMatrix(matrix);

    return true;
}

并确保使用Android默认 ImageView 而非图书馆 ZoomImageView