Proportionately re-sizing ImageView when width is set to fill parent - android

Is there a way to proportionally set the height of an ImageView if the width is set to android:layout_width="fill_parent"?
It appears there is no direct way of adjusting an ImageView without skewing one of it's dimensions. This seems bizarre given the application is expected to support a slew of screen sizes and resolutions.

You can use wrap_content for the height and set the correct scale type (either in code or via the android:scaleType attribute in XML). You are probably looking for CENTER_INSIDE/android:scaleType="centerInside".
See ImageView.ScaleType

Related

How to handle ImageView for different screen sizes without hardcoding?

I've an app that display list of images along with some text in a listview.
It actually fetches those images through web service.
If i use wrap_content for ImageView, it may stretch and the list will be irregular if the image size varies.
If i hardcode by giving some width and height (in dp), does it affects our multi screen support concept?
I wouldn't advise you to use wrap_content for the ImageView.
You could try setting a fixed height to it and use it full-width.
For the fixed size you have to use dp(Density Independent Pixels) which will result in having the size of this view 'almost' the same on any device. An by almost I mean you won't have the exact same percentage on the screen(for obvious reasons) but Android will scale it appropriately.
Second and most important is to set the scale_type property to the ImageView component in the xml file. There are various options but probably center_crop would fit your needs the best(I advise you to try out all the rest of them too, so you can understand the difference between, center, centerCrop, centerInside, fitCenter, fitEnd, fitStart, fitXY, matrix - these are all the possible values scale_type can have).
EDIT:
Here is the documentation description for these types:
center Displays the image centered in the view with no scaling.
centerCrop Scales the image such that both the x and y dimensions are greater than or equal to the view, while maintaining the image aspect ratio; crops any part of the image that exceeds the size of the view; centers the image in the view.
centerInside Scales the image to fit inside the view, while maintaining the image aspect ratio. If the image is already smaller than the view, then this is the same as center.
fitCenter Scales the image to fit inside the view, while maintaining the image aspect ratio. At least one axis will exactly match the view, and the result is centered inside the view.
fitStart Same as fitCenter but aligned to the top left of the view.
fitEnd Same as fitCenter but aligned to the bottom right of the view.
fitXY Scales the x and y dimensions to exactly match the view size; does not maintain the image aspect ratio.
If You want to display it in a ListView then it is better to hard-code the dimension. and add ScaleType you want to the image.
If you want to use GridView then use this property
android:stretchMode="columnWidth"
android:numColumns="auto_fit"
the columns are adjusted dynamically according to the density of the screen when using auto_fit

ImageView static height, dynamic width

Thanks to this site, I successfully implemented a Horizontal ListView of ImageViews.
Now what I need is for the ImageViews in the list to have a fixed height (they're part of a more complex layout, so I'm using the LinearLayout weight trick to give it a height equal to 1/3 of the screen height), and a width that adjusts in relation to the height without destroying aspect ratio. The images have a longer height than width - an aspect ratio similar to a phone on portrait.
Now I've tried setting the scaleType to the different available settings, also set adjustViewBounds to true, and set the layout_width to wrap_content and layout_height to fill_parent. The nearest I got to doing it was this:
http://img849.imageshack.us/img849/1289/device20120831133004.png
(i placed the white borders as image background)
What else can be done?
Problem here is that your ImageView cannot be adjusted to desired height because it's aspect ratio is different from screen. Your view matching screen height (or 1/3 of it) and width cannot be scaled proportionally without crop.
Try to set scaleType to centerCrop and load images resized to desired height.

Android ImageView ScaleType and item height

Things looked quite simple first but in the end the result is not good.
I have an image which has a width larger than screen's width. So I need to scale it down in my imageview. I looked over the ScaleType options and tried them all but none is ok. First "center" only displays the image centered on the layout, no scaling done. "fitCenter" scales the image to fit in my layout but has a major drawback: the height of the item remains as it would have the large image in it. Take a look at the second screen in the attached image. How can I force the list item, to reduce its height to wrap both the text and the image ?
Use the scaletype which seems best to you ( I guess you like what you see with fitCenter). The additional thing that you must do is
android:adjustViewBounds="true"
in your ImageView.
or you could go with FitXY but sometimes the result is not exactly what you want.
use android:scaleType="fitXY"
Could you use FitXY?
This would work if you knew the size of the area you were putting the image into.
CentreInside may also work, I've used this to scale down images, but I think it depends if you've control of the size of the bounding layout element.
You either need to set android:height = "wrap_content" on your outer container, or set static height of TextBox and give ImageView android:weight = "1" so that the ImageView takes remaining space in container.

Scaling images in an image view with a fixed width

I have several images I lazyload into a ListActivity. Now the thing is that the images have different aspect ratios.
I would like all images to display with the same width set in xml (to fit into my layout), while taking as much space hightwise as they need.
Is there a way to do that in xml?
Thanks!
I think you want android:scaleType="fitCenter"
From the sdk docs at http://developer.android.com/reference/android/graphics/Matrix.ScaleToFit.html#CENTER:
Compute a scale that will maintain the original src aspect ratio, but will also ensure that src fits entirely inside dst. At least one axis (X or Y) will fit exactly. The result is centered inside dst.
So, I think if you set a width on the imageview but set the height to wrap_content you should get what you want with the scaleType fitCenter.

How can specific the layout_width and layout_height of a Framelayout which fits an image

I have an image which is 80px X 50 px, and I need to place that in one of the child of a FrameLayout, how can I specific the layout_width and layout_height of a Framelayout which fits an image without scaling it?
I know there is a layout_height="wrap_content" layout_wight="wrap_content" for FrameLayout, but I can't use it, since that FrameLayout has other children. So I would like to hard code the FrameLayout width/height to match the dimension of the image?
Should I use layout_width="80px" or layout_width="80dip"?
Thank you.
You can use layout_width="80px" + layout_height="80px" + scaleType="fitXY" to specify the ImageView in the size you want.
dip depends on the screen densities, which says, 1dp != 1px.

Categories

Resources