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.
Related
I need an ImageView in which the user of my app loads an image from his gallery or camera. If the image is landscape, the ImageView width should be equal to screen width. If the image is portrait, the ImageView should take up as much screen space as there is available (I have a view above and a view below the ImageView). How can I achieve this?
Below is the desired result:
First you have to obtain the photo orientation, this can be achieved by reading its EXIF data. The answers to this question can help you to read the EXIF data. Then you have to change the width and height of your ImageView. If the image is portrait then you can use "match_parent" to both height and width. If the image is in landscape then put the height with "wrap_content" and the width in "match_parent". You need to do this by code.
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
I have a few buttons I want to display in a row in a decision tree. I'm trying to get the buttons in each row of the tree to be the same size. But the buttons have different text. Some have a couple words and some have a sentence. Is there a way I can get the buttons to all be the same size and all maintain the same width to height (4:3) ratio?
I can get the heights the same using fill parent on the button heights while having the parent row layout_height set to wrap_content. How can I get the width of the buttons to be the same size while maintaining a 4:3 width to height ratio. (The width to height ratio constraint is so I don't end up with really tall and thin buttons, which would look silly)
Anyone have any suggestions?
Edit:
I just saw this link which is kinda what I am looking for: Scaling layout with invariant aspect ratio in Android
My problem with this is that it will inflate all the buttons and introduce a lot of empty space around the text inside.
If I could find a way to incrementally increase the width or height (whichever is smaller) of the button and then resize it so it re-"wraps_content", then this could work. Anyone know how to re-wrap the content?
If you are able to get the same height for all you can use android:layout_weight="1" for all buttons and put android:layout_width="0dip" so all buttons will have same width
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
I need something like<img width="100%" /> for Android <ImageView>. I mean resize width to all available space (shrink or enlarge width of image) and automatically change height to keep aspect ratio.
Something like <ImageView android:layout_width="match_parent" android:layout_height="auto">
Useandroid:scaleType="fitCenter"do with image what I want, but only with image, doesn't change height of View itself. When I set booth dimensions to match_parent I will get what I want, but only if I have only one image on the screen. But I need some texts under image.
It's only way create child of ImageView?
For exampleandroid:adjustViewBounds="true"does't do anything for me.
I found where is problem. Default implementation of ImageView can't enlarge image. If image is bigger than screen width android:adjustViewBounds="true" works correctly and adjust height to keep aspect ratio of image. But if image is smaller that screen width it don't upscale it.
This is cause by this code
if (newHeight <= heightSize) {
heightSize = newHeight;
}
in ImageView class (on line 688, API 10)
I made special child of ImageView which allow enlarge image to fit width.
Here is: https://gist.github.com/tprochazka/5486822