TextureView TranslationX & Y comportamiento no esperado en API 23
Actualmente estoy haciendo un reproductor de cámara con un textureview
para hacer mi cámara. Debido a que la vista previa puede tener cualquier dimensión he creado algún código personalizado para alterar la textura cuando OnSurfaceTextureUpdated
se llama:
void updateTextureMatrix(int width, int height) { Display display = WindowManager.DefaultDisplay; var isPortrait = (display.Rotation == SurfaceOrientation.Rotation0 || display.Rotation == SurfaceOrientation.Rotation180); int previewWidth = orgPreviewWidth; int previewHeight = orgPreviewHeight; if(isPortrait) { previewWidth = orgPreviewHeight; previewHeight = orgPreviewWidth; } // determine which part to crop float widthRatio = (float)width / previewWidth; float heightRatio = (float)height / previewHeight; float scaleX; float scaleY; if(widthRatio > heightRatio) { // height must be cropped scaleX = 1; scaleY = widthRatio * ((float)previewHeight / height); } else { // width must be cropped scaleX = heightRatio * ((float)previewWidth / width); scaleY = 1; } Android.Graphics.Matrix matrix = new Android.Graphics.Matrix(); matrix.SetScale(scaleX, scaleY); _textureView.SetTransform(matrix); float scaledWidth = width * scaleX; float scaledHeight = height * scaleY; float dx = (width - scaledWidth) * 0.5f; float dy = (height - scaledHeight) * 0.5f; _textureView.TranslationX = dx; _textureView.TranslationY = dy; }
La escala y cálculo de dx
& dy
funcionan perfectamente bien en los dispositivos Android más antiguos, pero los dispositivos que tengo a mi disposición con el nivel 23 de la API lanzan un comportamiento inesperado.
- DESACTIVAR la traducción automática de la lista de tiendas en Google Play
- Error: "app_name" no se traduce en af
- Diferencia entre Camera.translate y Matrix.preTranslate o Matrix.postTranslate?
- ¿Alguna buena herramienta de traducción de la comunidad para Android?
- No se pudo encontrar la herramienta de compilación revisión 23.0.1
La galaxia S3 lo muestra correctamente:
Pero en la S7:
El teléfono corta una gran parte de la imagen, a pesar de posicionarlo correctamente. Esto me hace creer que la parte inferior no se está haciendo donde en los dispositivos más antiguos que es. ¿Puede alguien confirmar esto y señalarme en la posición correcta para solucionar este problema?
- Android: ¿Funcionamiento de matriz en ImageView, animado?
- la localización de android no funciona para algunos idiomas
- Custom textview con rotar, acercar, alejar y mover en el tacto en android?
- Tecla Eliminar en Editor de traducciones
- Android Plural Strings con múltiples parámetros
- Traducir fácilmente xml en Android
- ¿Cómo compartir textos y traducciones entre aplicaciones móviles?
- Android - cómo analizar json con el objeto en el índice
Después de largas pruebas descubrí que el problema se debía al método SetTransform
. Estaba ajustando mi escala usando la matriz pero esto de alguna manera rendía mi textura e ignoraba TranslationX
& TranslationY
. Quitar la matriz y reemplazarla por float scaledWidth = width * scaleX; Float scaledHeight = altura * scaleY;
float dx = (width - scaledWidth) * 0.5f; float dy = (height - scaledHeight) * 0.5f; _textureView.ScaleX = scaleX; _textureView.ScaleY = scaleY; _textureView.TranslationX = dx; _textureView.TranslationY = dy;
Arreglado mi problema de renderizar incorrectamente en ciertos dispositivos android.