当我旋转位图时,它会在android中自动缩放

时间:2017-05-24 10:08:41

标签: android bitmap rotation scale

我正在尝试设置最小和最大比例因子,但是当我设置它时,旋转在某种程度上会自动缩放我的位图。我该如何解决这个问题?

这是我在位图上执行onTouchEvent的代码:

@Override
public boolean onTouchEvent(MotionEvent event) {
    int action = MotionEventCompat.getActionMasked(event);
    boolean handled = true;

    switch (action) {

        case MotionEvent.ACTION_POINTER_DOWN:
            oldDis = spacing(event);
            if (oldDis > 10f) {
                savedMatrix.set(matrix);
                midPoint(event);
                lastRotateDegree = rotation(event);
                isPointerDown = true;
            } else {
                isPointerDown = false;
            }
            isInSide = false;
            isInResize = false;
            break;

        case MotionEvent.ACTION_POINTER_UP:
            isPointerDown = false;
            break;

        case MotionEvent.ACTION_MOVE:
            if (isPointerDown) {
                matrix.set(savedMatrix);

                //Two-finger rotate
                if(event.getPointerCount() == 2) {
                    float newRot = rotation(event);
                    float r = newRot - lastRotateDegree;
                    matrix.postRotate(r, mid.x, mid.y);
                }

                //Two-finger scaling
                float scale;
                float disNew = spacing(event);
                if (disNew == 0 || disNew < 10f) {
                    scale = 1;
                } else {
                    scale = disNew / oldDis;
                }
                matrix.postScale(scale, scale, mid.x, mid.y);

                matrix.getValues(arrayOfMatrix);
                float scaleX = arrayOfMatrix[Matrix.MSCALE_X];
                float scaleY = arrayOfMatrix[Matrix.MSCALE_Y];

                if(scaleX <= 0.7f || scaleY <= 0.7f) {
                    matrix.postScale((0.7f)/scaleX, (0.7f)/scaleY, mid.x, mid.y);
                } else if(scaleX >= 2.5f || scaleY >= 2.5f) {
                    matrix.postScale((2.5f)/scaleX, (2.5f)/scaleY, mid.x, mid.y);
                }

                invalidate();

            }
            break;
    }

public void midPoint(MotionEvent event) {
    float x = event.getX(0) + event.getX(1);
    float y = event.getY(0) + event.getY(1);
    mid.set(x / 2, y / 2);
}

private float rotation(MotionEvent event) {
    double delta_x = (event.getX(0) - event.getX(1));
    double delta_y = (event.getY(0) - event.getY(1));
    double radians = Math.atan2(delta_y, delta_x);
    return (float) Math.toDegrees(radians);
}

private float spacing(MotionEvent event) {
    if (event.getPointerCount() == 2) {
        float x = event.getX(0) - event.getX(1);
        float y = event.getY(0) - event.getY(1);
        return (float) Math.sqrt(x * x + y * y);
    } else {
        return 0;
    }
}

0 个答案:

没有答案