setimagedrawable android imageview horizontalscrollview nexus-4

android - setimagedrawable



¿Ancho de ImageView incorrectamente escalado en Nexus 4? (1)

Estoy usando la técnica que se describe aquí . La siguiente línea funciona en todos mis dispositivos, excepto mi Nexus 4 :

int imageWidth = horizontalScrollView.getChildAt(0).getMeasuredWidth();

El diseño es simple:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <HorizontalScrollView android:id="@+id/main_scroller" android:layout_width="match_parent" android:layout_height="match_parent" > <ImageView android:id="@+id/main_image" android:layout_width="wrap_content" android:layout_height="match_parent" android:adjustViewBounds="true" android:scaleType="centerCrop" /> </HorizontalScrollView> </RelativeLayout>

En este HorizontalScrollView , programáticamente creo un mapa de bits que está alrededor de 500x1000 píxeles. Por lo tanto, la imagen será estirada para llenar aproximadamente 1200x2400 (vea centerCrop ). Por lo tanto, el ancho de ImageView debe ser 2400. ¡ Y lo es! En mi Nexus 7, Samsung S3 Mini y algunos otros dispositivos de baja densidad. ¡Pero no en mi Nexus 4! En Nexus 4, sí tiene su altura estirada pero no tiene el ancho (sí, es increíble): 1000x1000 píxeles. He intentado ambos getWidth() y getMeasuredWidth() . Y cuando intento desplazarme, puedo ver solo el 50% de la imagen horizontalmente (pero 100% vertical).

ACTUALIZACIÓN 1:

Al mirar mDrawMatrix de mi ImageView , puedo ver que difiere:

Nexus 7: Matrix {[2.2979167, 0.0, 0.0] [0.0, 2.2979167, 0.0] [0.0, 0.0, 1.0]}

Nexus 4: Matrix {[2.1458333, 0.0, -623.0 ] [0.0, 2.1458333, 0.0] [0.0, 0.0, 1.0]}

ACTUALIZACIÓN 2:

Este es un error en ImageView.onMeasure() en Android 4.2.2, corregido en 4.3. La pregunta ahora es, ¿cómo agrego esta corrección en 4.2.2? onMeasure() no es 100% trivial ya que se están llamando a muchas funciones privadas.


He tenido el mismo problema en Samsung Galaxy S4 4.2.2 y S3 4.1.1, haga lo que haga, un mapa de bits programáticamente establecido no se escalaría para llenar el ancho de su ViewGroup principal. Intenté todas las combinaciones de scaleType inútilmente.

Para mí, la solución fue establecer la densidad en el mapa de bits a un valor específico antes de mostrarlo.

Bitmap myBitmap = ... // S3 and S4 get confused about displaying bitmaps without a specific density, // and fail to scale them properly. Setting the density to this value helps (is possible ANY density helps, but haven''t tested this) myBitmap.setDensity(DisplayMetrics.DENSITY_HIGH); myImageView.setImageBitmap(myBitmap);