Pregunta

I've tried as many permutations of scaleType and fillViewport and adjustViewBounds and layout_* as I can think of and none of them do what I want. They either fill the screen but lose the aspect ration (fillXY), cut off a portion of the image (centerCrop), or scale the image down so it doesn't fill the width of the screen (centerInside, fitStart, fitCenter, center, matrix).

Here is one specific example:

Source image is 222x470 pixels.
Emulator screen is 480x800hdpi in portrait orientation.

I want the ScrollView to fill the entire screen.
I want the ImageView to scale the image so it fills the width of the screen (stretching the source image from 222 wide to 480 wide).
I want the ImageView to preserve the aspect ratio of the source image, meaning its height will have to scale from 470 pixels to approximately 1016 pixels.
Since that won't fit on the screen (it is only 800 pixels high), the ScrollView should kick in. By default the top of the image should show and the ScrollView should allow scrolling down to see the rest of the image.

This is just one example. Ideally I'd like this to work regardless of image size and screen size.

Extra credit if you know a way to do it all within the XML layout.

And before y'all come down on me like a ton of bricks (or bricked cellphones?), I'm aware there are several similar questions on this site (see here, here, here, here, and here) but they are either unanswered or the answers are unacceptable ("use fitXY", which does not preserve aspect ratio, or require specifying exact pixel width/height in the ImageView which is not a general purpose solution to the problem). It'd be nice to get a solid answer to this documented and out there.

¿Fue útil?

Solución

Answering my own question in the hopes it helps someone else.

I found a simple functional answer here. The AspectRatioImageView class works where all my XML scaling attempts failed.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top