0

Edit: partial answer

If both dimensions are set in the xml, it behaves as expected, for example

android:layout_width="250dp"
android:layout_height="250dp"

if one is left to wrap_content, it doesn't.

Original Post

Here is my layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
android:id="@+id/board"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="@drawable/board" />

</LinearLayout>

Why does the image I see on screen have the correct dimensions (screen height x screen height because it is a square bitmap and I set match_parent for height), BUT if I call ImageView.getWidth() and ImageView.getHeight() it gives me the screen dimensions?

Best regards

pouzzler
  • 1,800
  • 2
  • 20
  • 32

3 Answers3

6

This is happening because of the reason I have provided in your earlier question here https://stackoverflow.com/a/10183799/1244489

When you call getWidth() and getHeight(), it returns the size of your ImageView which in your case if you observe is the fullscreen size. Your source Image has been shrunk to fit the screen maintaining its aspect ratio but the ImageView isn't shrunk. To shrink it to the Image size, you need to provide the parameter

android:adjustViewBounds="true"

in your ImageView.

Community
  • 1
  • 1
Shubhayu
  • 13,402
  • 5
  • 33
  • 30
  • Height and width are still equal to the screen's. Thanks for the help all the same. – pouzzler Apr 17 '12 at 10:23
  • Would like to get this solved as it has bothered me in a number of occasions. Have you got a fix for it? – Shubhayu Apr 17 '12 at 10:45
  • you could try adding the following 2 parameters in your ImageView, android:scaleType="fitCenter" and android:adjustViewBounds="true". Also check if you are programatically setting any values of the ImageView in your code. – Shubhayu Apr 17 '12 at 10:49
  • oh damn!! I totally missed out what is creating your problem! change this android:layout_height="match_parent" to android:layout_height="wrap_content". – Shubhayu Apr 17 '12 at 11:39
  • Nope I already tried that, and just about any combination of layout and imageview heights, weights, widths I could think of. – pouzzler Apr 17 '12 at 11:46
  • hmmm.. well I think you should keep it to wrap_content and also the above 2 I mentioned. Let me see if I can think of something else that could be the issue. I need to figure out whether the behaviour changes when you provide a src rather than adding the imageview programatically – Shubhayu Apr 17 '12 at 11:52
1

Found the answer here: Fit image into ImageView, keep aspect ratio and then resize ImageView to image dimensions?

Here's the root of the problem: the ImageView doesn't size itself to the image dimensions, so we have to size it ourselves:

LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) imageView.getLayoutParams(); 
params.width = width;
params.height = height;
imageView.setLayoutParams(params);
Community
  • 1
  • 1
pouzzler
  • 1,800
  • 2
  • 20
  • 32
0

Because getWidth() and getHeight() are called from the View hierarchy which are screen dimensions. In fact, if the image was scaled using ImageView's scaletypes like fitCenter, the dimensions you get won't match the image at all. They will remain the pixel width and pixel height of the entire view and not just what was taken up by the image.

DeeV
  • 35,865
  • 9
  • 108
  • 95
  • Why is the view larger than the image, since wrap_content is set? Can you please explain? Thanks a lot already. – pouzzler Apr 16 '12 at 19:35
  • It's the way `ImageView` is scaling in order to fit the height with `match_parent`. The image you're using is a larger than the dimensions you're trying to fill. By default, it's set to center the image and fit it so everything is shown. It will shrink your image likewise. That's why I said the pixel dimensions of the entire `View` will be different than the pixel dimensions of the shown image. – DeeV Apr 16 '12 at 19:48