0

I'm using Matrix in my app to zoom and drag, in a ImageView.

To control the limits of scrolling and zooming, I use the methods exposed in how-to-control-the-drag-of-the-imageview-using-matrix

And everything works fine, until I apply the PostRotate method to the ImageView Matrix object.

To understand what is happening, I have performed the following procedure: I place an image in the ImageView, and I turn it 90º, doing a dump of data after each operation.

I do not understand the values I get from the Matrix object, so I do not know how to adapt the code so that it can be applied in case of rotating the bitmap.

Look:

private void DumpDatosEnMatrix(){
    int viewWidth = imagenView.getWidth();
    int viewHeight = imagenView.getHeight();

    float []m = new float[9];
    matrix.getValues(m);

    // translation is simple
    float tx = m[Matrix.MTRANS_X];
    float ty = m[Matrix.MTRANS_Y];

    Log.d(TAG,"============================================");
    Log.d(TAG,"Translación: transX,transY: "+tx+","+ty);
    Log.d(TAG,"Sesgado: skewX,skewY: "+m[Matrix.MSKEW_X]+","+m[Matrix.MSKEW_X]);

    // calculate real scale
    float scalex = m[Matrix.MSCALE_X];
    float skewy = m[Matrix.MSKEW_Y];
    float rScale = (float) Math.sqrt(scalex * scalex + skewy * skewy);

    Log.d(TAG,"escala: "+rScale);

    // calculate the degree of rotation
    float rAngle = Math.round(Math.atan2(m[Matrix.MSKEW_X], m[Matrix.MSCALE_X]) * (180 / Math.PI));

    Log.d(TAG,"angulo: "+rAngle);

    int imageWidth=(int)(bitmap.getWidth()*rScale);
    int imageHeight=(int)(bitmap.getHeight()* rScale);

    Log.d(TAG,"Tamaño del bitmap(w,h) :"+imageWidth+","+imageHeight);
    Log.d(TAG,"Tamaño del ImageView(w,h) :"+viewWidth+","+viewHeight);
}

Resultados:

============================================ Translación: transX,transY: 0.0,221.22856 Sesgado: skewX,skewY: 0.0,0.0 escala: 1.0285715 angulo: 0.0 Tamaño del bitmap(w,h) :720,415 Tamaño del ImageView(w,h) :720,858

============================================ Translación: transX,transY: 562.49414,70.97687 Sesgado: skewX,skewY: -1.0285715,-1.0285715 escala: 1.0285715 angulo: -90.0 Tamaño del bitmap(w,h) :720,415 Tamaño del ImageView(w,h) :720,858

============================================ Translación: transX,transY: 722.0161,655.5801 Sesgado: skewX,skewY: -0.0,-0.0 escala: 1.0285715 angulo: -180.0 Tamaño del bitmap(w,h) :720,415 Tamaño del ImageView(w,h) :720,858

============================================ Translación: transX,transY: 167.56201,791.09814 Sesgado: skewX,skewY: 1.0285715,1.0285715 escala: 1.0285715 angulo: 90.0 Tamaño del bitmap(w,h) :720,415 Tamaño del ImageView(w,h) :720,858

1 Answers1

0

Do not worry, I found the solution. I do not understand the inner workings of the Matrix object, but it works.

When turning, the coordinate axis is modified in this way:

    float []m = new float[9];
    matrix.getValues(m);

    // calculate the degree of rotation
    float rAngle = Math.round(Math.atan2(m[Matrix.MSKEW_X], m[Matrix.MSCALE_X]) * (180 / Math.PI));

    boolean aLoAlto=(Math.abs(rAngle)==90f)?true:false;

    // calculate real scale
    float scalex = m[Matrix.MSCALE_X];
    float skewy = m[Matrix.MSKEW_Y];
    float rScale = (float) Math.sqrt(scalex * scalex + skewy * skewy);

    int imageWidth = (int) (bitmap.getWidth() * rScale);
    int imageHeight = (int) (bitmap.getHeight() * rScale);

    if (aLoAlto) {
        imageHeight = (int) (bitmap.getWidth() * rScale);
        imageWidth = (int) (bitmap.getHeight() * rScale);
    }


    float transX = m[Matrix.MTRANS_X];
    float transY = m[Matrix.MTRANS_Y];

    nt xAxis=0,yAxis=0;
    switch ((int)rAngle){
        case 90:
            yAxis=imageHeight;
            break;
        case 180:
        case -180:
            yAxis=imageHeight;
            xAxis=imageWidth;
            break;
        case -90:
            xAxis=imageWidth;
            break;
    }

For calculations, bypassing the axis to the point (0,0), from that code simply need to be done

 transX-=xAxis;
 transY-=yAxis;

Of course, this code is intended for right angles, which is what I need.