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.
Related
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 built an app and used an imageview (SmartImageView to be exactly, but i doubt that the problem) as reference for the place where I want my images to be, the thing is: the imageview has 50x50 px (set to wrap_content) but when I pull the images from web (favicons, to be exactly) if they are bigger than 60x60 instead of keeping the imageview size, they keep their own size.
I want my imageview to work as a mask and crop and/or resize the favicons to it's 50x50 size, how can I do it?
Heres the xml of the imageview:
<com.loopj.android.image.SmartImageView
android:id="#+id/favicon_placeholder"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/favicon_example2" />
And here's the java bit of code that pulls the images to it:
linkImageView.setImageUrl(link.favicon);
You are setting your height and width to wrap_content which means that it is going to resize itself to fit the content that you give it. If you want it to always be 50px no matter what size image you give it then you need to declare the size statically instead of wrap_content. And set your scaleType so the system knows how to manipulate the image should it be a different size. See here for possible values
<com.loopj.android.image.SmartImageView
android:id="#+id/favicon_placeholder"
android:layout_width="50px"
android:layout_height="50px"
android:scaleType="fitXY"
android:src="#drawable/favicon_example2" />
note that the use of the px unit is generally discouraged. dp is used more often because it can scale up or down for smaller and larger resolution devices. But if you want it to be the exact same pixels on every device then you must declare it with px
Look at ImageView.ScaleType. You may want to have a calculated size or fixed size in dp when using ImageView.ScaleType properties.
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.
I want to scale an image in an ImageView in the following way. The ImageView has some dimensions Width (W) and Height (H). The image I'm putting into the image view could be smaller or bigger than WxH. I want it to scale while preserving aspect ratio to fill WxH space.
It seems like the closest thing to what I want is android:scaleType="centerInside", but what I'm seeing is that if the image is smaller than WxH, it will put a small-unscaled version of that image in the center of the ImageView (like the documentation says), but I want it to scale it to "fit", while showing the entire image, and stretching it to the maximum possible size of the container without cropping anything. In other words, stretch preserving aspect ratio until either the width or the height bumps into the edge of the container (ImageView).
This seems like an obvious thing to want to do, but I can't get it to work this way!!!
From the Android docs...
public static final Matrix.ScaleToFit 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.
The XML attribute for this is...
android:scaleType="fitCenter"
I am trying to apply image on an image view instance...but it doesnt cover it properly...
please advise
here it is my image view code:
android:id="#+id/imageViewVessel"
android:layout_width="fill_parent"
android:scaleType="fitStart"
android:layout_height="170dip"
android:src="#drawable/vessel"
EDIT by kcoppock: Adding code from devaditya's comment below
TableRow rowImage = new TableRow(this);
rowImage.setBackgroundColor(Color.GRAY);
rowImage.setMinimumHeight(150);
rowImage.setGravity(Gravity.CENTER);
rowImage.setMinimumWidth(LayoutParams.FILL_PARENT);
ImageView imgViewVessel=new ImageView(this);
imgViewVessel.setImageResource(R.drawable.vessel);
imgViewVessel.setMinimumHeight(150);
imgViewVessel.setMinimumWidth(LayoutParams.FILL_PARENT);
imgViewVessel.setScaleType(ScaleType.FIT_XY);
rowImage.addView(imgViewVessel);
To expand on Gao's answer, you do need to set a scaleType for your ImageView, but it is unlikely that fitXY is the scaleType that you are looking for. You can find the complete list at the above link, but a few of the most common are:
centerCrop: This will maintain the aspect ratio of the image, filling the frame entirely, but cropping off either the left and right, or top and bottom of the if the aspect ratio of the frame and source image are different.
centerInside: This also maintains the aspect ratio, but the image is scaled to fit entirely within the view, so that the longest edge is the same size as the frame. This can give you a letterbox type of effect if the aspect ratios of the frame and source image are different. fitStart and fitEnd are the same scaling method, but they have different placement of the image (top-left and bottom-right, respectively).
fitXY: This one should only be used if disproportionate scaling does not affect the graphic. In the case of bitmap graphics, this is almost always bad to use. This sets the width of the source image to the width of the view, and the height of the source image to the height of the view, without maintaining the aspect ratio of the source image.
You can set scale type in the layout file : android:scaleType="fitXY" or call setScaleType with fitXY.