6 jun
2017

when i rotate bitmap it is scaled automatically in android

I am trying to set the minimum and maximum scale factor but when i set it, the rotation on some degree is automatically scaling my bitmap. How can I solve this issue ?

Here is my code that I have done onTouchEvent on bitmap:

@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;
    }
}

COMENTARIOS

DEJA TU COMENTARIO

© 2017 website by Rubit Corporation