Vista de la imagen de Android escala de la matriz + traducir

Estoy tratando de obtener manualmente una imagen dentro de una imagen centrada y la instalación de la pantalla. Necesito hacerlo con una matriz (después cambiaré dinámicamente la transformación de la matriz).

El problema es que no puedo obtener la imagen centrada en la vista (la escala es apropiada). Aquí está el código:

// Compute the scale to choose (this works) float scaleX = (float) displayWidth / (float) imageWidth; float scaleY = (float) displayHeight / (float) imageHeight; float minScale = Math.min(scaleX, scaleY); // tx, ty should be the translation to take the image back to the screen center float tx = Math.max(0, 0.5f * ((float) displayWidth - (minScale * imageWidth))); float ty = Math.max(0, 0.5f * ((float) displayHeight - (minScale * imageHeight))); // Compute the matrix Matrix m = new Matrix(); m.reset(); // Middle of the image should be the scale pivot m.postScale(minScale, imageWidth/2, imageHeight/2); // Translate m.postTranslate(tx, ty); imageView.setImageMatrix(m); 

El código anterior funciona si no centré la escala en el centro de la imagen (pero necesitaré hacerlo más tarde, así que necesito averiguar la fórmula ahora).

Pensé que hacer lo siguiente corregiría el problema, pero la imagen todavía está desplazada (hacia abajo y derecha).

 tx += 0.5*imageWidth*minScale; ty += 0.5*imageHeight*minScale; 

Algunos valores que tengo: – imagen: 200×133 – pantalla: 800×480 – minScale: 2.4 – final esquina superior izquierda de la imagen: 100, 67 (debe ser 17, 0)

Hay un método conveniente llamado Matrix.setRectToRect (RectF, RectF, ScaleToFit) para ayudarlo aquí.

 Matrix m = imageView.getImageMatrix(); RectF drawableRect = new RectF(0, 0, imageWidth, imageHeight); RectF viewRect = new RectF(0, 0, imageView.getWidth(), imageView.getHeight()); m.setRectToRect(drawableRect, viewRect, Matrix.ScaleToFit.CENTER); imageView.setImageMatrix(m); 

Que debe establecer la matriz m para tener combo de escala y traducir los valores que se necesita para mostrar el dibujable centrado y encajar en el widget ImageView.

He aquí cómo resolví mi problema usando matrices (solicitado por joakime en la otra respuesta):

 private void setImageTransformation(float tx, float ty, float scale) { savedMatrix.reset(); savedMatrix.postTranslate(-imageWidth / 2f, -imageHeight / 2f); savedMatrix.postScale(scale, scale); savedMatrix.postTranslate(tx, ty); imageView.setImageMatrix(savedMatrix); } public void resetImageMatrix() { if (!isImageLoaded()) return; imageWidth = imageView.getDrawable().getIntrinsicWidth(); imageHeight = imageView.getDrawable().getIntrinsicHeight(); float scaleX = (float) displayWidth / (float) imageWidth; float scaleY = (float) displayHeight / (float) imageHeight; minScale = Math.min(scaleX, scaleY); maxScale = 2.5f * minScale; initialTranslation.set( Math.max(0, minScale * imageWidth / 2f + 0.5f * (displayWidth - (minScale * imageWidth))), Math.max(0, minScale * imageHeight / 2f + 0.5f * (displayHeight - (minScale * imageHeight)))); currentScale = minScale; currentTranslation.set(initialTranslation); initialImageRect.set(0, 0, imageWidth, imageHeight); setImageTransformation(initialTranslation.x, initialTranslation.y, minScale); } 

Estoy engañando aquí un poco porque el pellizco no está realmente centrado entre los dedos del usuario, que es aceptable en mi caso.

Hey yo estaba teniendo el mismo problema, y ​​que estaban tan cerca! Era una cuestión de orden de operaciones, al menos para mí. Lo siguiente funcionó para mí:

 tx = (width - imgWidth) * 0.5f * scale; 

O usando

  m.postScale(minScale, minScale); //keep aspect ratio float tx = (getWidth() - bitmap1.getWidth()* scale) * 0.5f ; float ty = (getHeight() - bitmap1.getHeight()* scale) * 0.5f ; matrix.postTranslate(tx, ty ); 

Donde getWidth es el ancho de su imageView. Funciona perfectamente para mí

  • Android ImageView - Obtener coordenadas de tap (clic) independientemente de la ubicación de desplazamiento o la escala de zoom
  • ¿Cómo se utilizan los valores de android.graphics.Matrix?
  • Android visualiza la transformación de rotación 3D en pantallas de gran resolución
  • Estiramiento Mapa de bits en una esquina específica
  • cómo rotar el mapa de bits en android en el punto central utilizando matriz
  • Android: gira la imagen en ImageView por 90degrees pero sin demora
  • Voltear una imagen de mapa de bits horizontal o verticalmente
  • La rareza de Android OpenGL con el método setLookAtM
  • FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.