2011-03-12 30 views
11

Estoy tratando de hacer un reproductor multimedia para la plataforma Android y una de las características que intento agregar es la capacidad de arrastrar y pellizcar y hacer zoom en las imágenes.Obtener coordenadas y ancho/alto desde una matriz

El problema que tengo es que copié este código de "Hola, Android" ed. 3:

@Override 
public boolean onTouch(View v, MotionEvent event) { 
ImageView view = (ImageView) v; 
switch (event.getAction() & MotionEvent.ACTION_MASK) { 
case MotionEvent.ACTION_DOWN: 
    savedMatrix.set(matrix); 
    start.set(event.getX(), event.getY()); 
    mode = DRAG; 
    break; 
case MotionEvent.ACTION_POINTER_DOWN: 
    oldDist = spacing(event); 
    if (oldDist > 10f) { 
     savedMatrix.set(matrix); 
     midPoint(mid, event); 
     mode = ZOOM; 
    } 
    break; 
case MotionEvent.ACTION_UP: 
case MotionEvent.ACTION_POINTER_UP: 
    mode = NONE; 
    break; 
case MotionEvent.ACTION_MOVE: 
    if (mode == DRAG) { 
     matrix.set(savedMatrix); 
     matrix.postTranslate(event.getX() - start.x, event.getY() - start.y); 
    } 
    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); 
     } 
    } 
    break; 
} 
view.setImageMatrix(matrix); 

Este fragmento de código hace exactamente lo que quiero, pero necesito el X pura, las coordenadas Y y la anchura/altura de la imagen en lugar de la matriz.

¿Alguien sabe cómo cambiar la matriz a coordenadas X, Y y el ancho y alto?

Respuesta

36

Resulta que la solución es simple:

float[] values = new float[9]; 
matrix.getValues(values); 
globalX = values[Matrix.MTRANS_X]; 
globalY = values[Matrix.MTRANS_Y]; 
width = values[Matrix.MSCALE_X]*imageWidth; 
height = values[Matrix.MSCALE_Y]*imageHeight; 
+0

gracias a un hombre registro !!! :) – cV2

+0

solución encantadora ... genial –

+1

Solo quiero agregar que'imageWidth' y'imageHeight' son el ancho y alto del dibujante después de que se ha dibujado en el ImageView (valores intrínsecos). –

4

gracias, he estado buscando toda la noche para encontrar esto, sólo una cosa: en mi ejemplo, tuve que usar:

float height = matrixValues[4]*((ImageView)currentView).getDrawable().getIntrinsicHeight(); 

para obtener la altura correcta.

+1

Yo también. La otra respuesta del usuario489447 no proporciona valores precisos. –

3

escribí algunos métodos que pueden ayudar a algunos usuarios (muchas gracias por el código anterior !!!):

private void logImageViewMatrixInfos(Matrix matrix, ImageView imageView) { 
     float[] values = new float[9]; 
      matrix.getValues(values); 
      float globalX = values[2]; 
      float globalY = values[5]; 
      float width = values[0]* imageView.getWidth(); 
      float height = values[4] * imageView.getHeight(); 

      Log.i("Log value", "Image Details: xPos: " + globalX + " yPos: " + globalY + "\nwidth: " + width + " height: " + height); 
    } 

    private float getXValueFromMatrix(Matrix matrix) { 

     float[] values = new float[9]; 
      matrix.getValues(values); 
      float globalX = values[2]; 

      return globalX; 
    } 

    private float getYValueFromMatrix(Matrix matrix) { 

     float[] values = new float[9]; 
      matrix.getValues(values); 
      float globalY = values[5]; 

      return globalY; 
    } 

    private float getWidthFromMatrix(Matrix matrix, ImageView imageview) { 

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

      float width = values[0]* imageview.getWidth(); 

      return width; 
    } 

    private float getHeightFromMatrix(Matrix matrix, ImageView imageview) { 

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

      float height = values[4] * imageview.getHeight(); 

      return height; 
    } 

: =)

Cuestiones relacionadas