android image view in list will not scale to width - android

New to Android, bad with layouts so this is probably a simple solution but I can't find it. I am adding an image to a ListView, the ListView is defined like this:
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
Image is added like this:
ImageView image = new ImageView(context);
image.setScaleType(ImageView.ScaleType.FIT_CENTER);
image.setAdjustViewBounds(true);
image.setImageDrawable(drawable);
return image;
in the ListAdapter. I want the Image to scale so the width fills the screen and the height adjust proportionally. What is happening is the image is scaling down so the height fills the list row but there is plenty of room for the width to be bigger. I had this working earlier but can't figure out what changed. Why is the image shrinking to some seemingly arbitrary height and how do I get it to fill the screen by width?
Edit:
So this happened when I started building for 1.6 instead of 1.5, probably a resolution thing. I guess the image is a bit sharper when it's small but I want it larger, it's almost unusable smaller and I am not able to get a larger image, it's from the web.

I think there is no really "simple" solution, but it's already answered here (without list's, but i don't think it matters):
Android: How to stretch an image to the screen width while maintaining aspect ratio?

Have you tried ImageView.ScaleType.FIT_XY ?
Scale Types

Related

ImageView Scaling: fix height variable width with maxWidth

I am going to show some images, but my constraint is as title. I want to make all images have same height but according to their width they must have different width. I just do not want to lose any part of the image when scaling. here is my code:
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/ImageView_IssueFirstLookActivity_magas"
android:layout_width="wrap_content"
android:layout_height="170dp"
android:layout_marginLeft="32dip"
android:layout_weight="2"
android:maxWidth="40dp"
android:adjustViewBounds="true"
android:scaleType="fitStart" />
I tried all I could with different values of android:scaleType but get nowhere.
here is a figure that what I want but when I play with those properties it dose not scale it as I want for example some time it dose not show image width completely and it crops it badly. Another try for example is android:scaleType="fitStart" which cause the width of my images to be fine but the heights are not fit anymore. Any idea?
andriod ImageView attributes does not support the requirement you said just by statically choosing combination of width,height and saleType in the layout xml.
Instead you can do it in the java code by actually finding the width for the image for a given fixed height and setting them programattically to each view in dp(make sure you do the pixel to dp calculation for achieving right output, salable across devices)
If you don't mind an extra dependency, you can do it with Picasso
Picasso.get()
.load(R.drawable.my_image_resource)
.placeholder(R.drawable.ic_image_placeholder)
.error(R.drawable.ic_broken_image)
.resize(0, (int) imagefixedHeight)
// optional:
// .centerInside()
.into(myImageView);

Handle ImageView pictures in Android

i have a question about pictures in an ImageView in Android.
My problem is, that the pic i wanna show in my ImageView seems too big for my smartphone screen. Therefore it is not on the place i set in the xml...
Here is a pic that you know what i mean: http://www10.pic-upload.de/31.10.12/iuebxzkvr3e.png
The grey area is my Banner which should be located on top of my activity...
If i scale the pic, it should work (the smaller, the higher till it fits), but i think other smartphones with a different resolution should have the same problem again with my app..
Here is my XML:
<ImageView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/ivBanner"
android:src="#drawable/banner"/>
Nothing special.
thanks in advance
In your layout you should set its height and width to absolute dp values:
Here is a px to dp converter:
http://labs.skinkers.com/content/android_dp_px_calculator/
That solved the problem for me, for every screen size you need a different values(hdpi/xhdpi).
you can set scaleType for ImageView to fitXY, or centerCrop
android:scaleType = "fitXY"

Photo: wrap_content, but max_width = layout width

I have a normal "read an article" type page with a single large photo at the top.
I'd like it to:
not scale larger than the source imgae
not be larger than the screen
not be CRAZY long vertically
side things I already have working
border (via padding), center crop (would rather top crop, but seems not avail)
The main issue I'm having is - I can either have it fill_parent (ie full width) OR wrap_content (ie as large as the original image). And nothing I have done so far allows the image to be normal size (ie keep from enlarging).
Is there a "norm" for this kind of thing? I'm sure there's some kind of sweet spot or combination of attributes for this that works - I just can't find them.
My current attempt:
<ImageView
android:id="#+id/article_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/super_light_gray"
android:scaleType="centerCrop"
android:contentDescription="#string/articlePhoto"
android:cropToPadding="true"
android:layout_marginBottom="20dp"
android:padding="1dp"/>
You can write some code in the Activity to get the image size and display size.
If the image size exceeds display size set width to display, if not set to image size. If you want to be fancy you can find the ratio between the x and y and scale vertically also.

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.

Categories

Resources