-1

I have an ImageView that has:

android:layout_height="100dp"   
android:layout_width="wrap_content"
android:scaleType="fitCenter" 
android:adjustViewBounds="true"   

What I see is that if I set an image that has as dimensions (based on the info of my pc) 1280px854
If I open it in my pc the image looks huge.
The same image is small in my ImageView although I have set wrap_content.
I am not sure I get how this works. E.g. 1280/4 = 320dpi (xxxhdpi) but for with wrap_content the image view does not have as width 320dpi
To clarify: I am trying to understand why it works like that and does not make it bigger with wrap_content.

Jim
  • 3,845
  • 3
  • 22
  • 47
  • Are you actually using height or layout_height (same for width)? – Daniel Beleza Apr 13 '19 at 09:47
  • you've set the height, hence, wrap content for the width will preserve the ratio of the image (x, y) – Martin Apr 13 '19 at 09:47
  • @DanielBeleza: Sorry for the confusion. I updated the post – Jim Apr 13 '19 at 09:50
  • @Mitch: Is there some formula so we know what is the max width it can have respecting this ratio? – Jim Apr 13 '19 at 09:52
  • 1
    When you have the height specified and width set to `wrap_content`, the width already implies max-width with regards to the ratio. If you're interested in how much that exactly is then: width = height * (1280 * 854). In this case 150-ish – Martin Apr 13 '19 at 09:57
  • @Mitch: That 150 is it dp? Yes that is what I am trying to understand. How these values work together – Jim Apr 13 '19 at 10:48
  • @Jim Yes, that's dp. If you were to put pixels in the formula (for the height), then that would give you pixels back for the width. If I put this down into an answer, would you accept it? – Martin Apr 14 '19 at 08:56
  • @Mitch: I would if you elaborated it on it so I can understand how this multiplication holds up to give the values – Jim Apr 14 '19 at 18:25

3 Answers3

1

First, let's talk about the ratio of your image. It's 1280px wide and 854px tall, hence, its proportion is 1280/854 ≈ 3/2.

Second, you've set the height of your ImageView to 100dp. If the width is not specified, then android will try its best to perceive the ratio (3/2) and will calculate the width:

ratio = width/height
3/2 = width/100
width = 150dp

Notice it gave us dp, if we were to plug px instead(for the height) it will give us px back.

Third, now that we have the size of the image (150dp x 100dp) and width='wrap_content' the width of the imageView will correspond to the 150dp

Martin
  • 1,159
  • 10
  • 34
  • In your earlier comment you wrote `width = height * (1280 * 854).` Was that supposed to be `width = height * (1280 / 854).`? – Jim Apr 15 '19 at 20:03
0

The solution is to set android:layout_height="wrap_content" since the 100dp in layout_height was limiting the "growth" of image width.

Daniel Beleza
  • 389
  • 1
  • 15
  • This is not what I am asking. I don't have a problem with the current display. I am asking how it works – Jim Apr 13 '19 at 10:47
0

This atribute:

android:scaleType="fitCenter"

means that the original aspect ratio of the image will be preserved.
You want to fit that image an an ImageView with:

android:layout_height="100dp"

so since you constraint the height of the image, if it is bigger that 100dp it will shrink to that height and its width will shrink proportionally.

forpas
  • 160,666
  • 10
  • 38
  • 76
  • This is not what I am asking. I don't have a problem with the current display. I am asking how it works – Jim Apr 13 '19 at 10:47
  • 1
    As I mention in my answer: *the original aspect ratio of the image will be preserved* this is what `fitCenter` does. – forpas Apr 13 '19 at 10:49
  • That is helpful indeed. But can we know how much the width could expand while preserving that ratio at the same time? – Jim Apr 13 '19 at 11:03
  • Proportionally means that if the height will be the 1/3 of the original height, then the same will apply to width, since you don't place any constraint to the width. – forpas Apr 13 '19 at 11:09
  • See these links: https://proandroiddev.com/a-guide-for-android-imageview-scaletype-and-adjustviewbounds-64a1e4a35503 and https://abhiandroid.com/ui/scaletype-imageview-example.html – forpas Apr 13 '19 at 11:18