1

How can I set the width and height of an ImageView before loading the image with, for example, setBitmapImage()?

The point is that I have a GridView with variable number of columns and I do need to stretch the width of the images by preserving the aspect ratio, and, since I load the images in an asynchronous manner, I do want to prepare the layouts before loading the images, that is, width and height of the ImageView.

Didac Perez Parera
  • 3,734
  • 3
  • 52
  • 87

4 Answers4

1

Set the ImageView's ScaleType.

You can chose one of these :

enter image description here

Emil Davtyan
  • 13,808
  • 5
  • 44
  • 66
0

Try with:

yourLayout.setLayoutParams(new LayoutParams(width, height));
sschrass
  • 7,014
  • 6
  • 43
  • 62
Onur A.
  • 3,007
  • 3
  • 22
  • 37
  • Sorry, that's not an answer, it's just a line of code that does not solve my problem. – Didac Perez Parera Aug 06 '13 at 13:25
  • A line of code can solve your question dont underestimate it ;) Anyway if you want to keep ratio you can use scaleType property of imageview, it has a lot options to pick one http://developer.android.com/reference/android/widget/ImageView.ScaleType.html – Onur A. Aug 06 '13 at 13:34
  • Hi Onur! thanks for your comment! anyway, the point is that I am trying to retrieve the width of a layout but the result is always -1 (wrap_content, I guess) but, at the end, this is not the actual width. How can I measure the real width at a concrete moment? – Didac Perez Parera Aug 06 '13 at 14:29
  • BitmapFactory can be used to get width and height, how you get those images? – Onur A. Aug 06 '13 at 14:47
  • I do know the bitmap size, I just need to know the width of the parent layout of the ImageView in order to stretch it – Didac Perez Parera Aug 06 '13 at 16:08
  • if your parent view is fill_parent then getting the screen size will give you the parent width, you can get screen size Display display = getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); int width = size.x; int height = size.y; – Onur A. Aug 06 '13 at 21:29
0

You should take a look here, there was a similar question asked earlier today:

Scale Image to fill ImageView width and keep aspect ratio

Community
  • 1
  • 1
arnefm
  • 1,008
  • 8
  • 7
0

know the height and the width of the image, but You the following code to retrieve the layout dimension in the ANDROID

yourLayout.setLayoutParams(new LayoutParams(width, height));

Mohsin Shaikh
  • 494
  • 1
  • 4
  • 11