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:
- Copiar una matriz de bits de la imagen de mapa de bits en la programación ANDROID
- Rotación Opengl de Android con datos de sensores
- ¿Cuál es la diferencia entre `Matrix.setRotate` y` Matrix.postRotate`
- Selección de Android OpenGL 3D
- Utilizar getRotationMatrix y getOrientation en Android 2.1
// 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)
- ¿Por qué a veces Bitmaps son los mismos objetos?
- Cambiar mapa de bits Color con función
- ¿Podría alguien por favor explicar la matriz (Mr.Anderson)?
- Recortar una transformación de perspectiva de la imagen en Android
- Android: ¿Hay algún evento desencadenado cuando activity.setContentView ha terminado de procesar?
- Funcionalidad backport android.graphics.Camera.setLocation ()
- Sensores Android para OpenGL
- Tiempo de procesamiento de OpenCV Mat
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í
- Qué hacer con el error curl clone.bundle en AOSP repo sync
- Diferencia entre close () y disconnect () en Android Bluetooth API?