Getting LayoutParams.WRAP_CONTENT to ignore a large background image - android

I'm trying to make a LinearLayout that has height = LayoutParams.WRAP_CONTENT.
I am receiving an arbitrarily sized bitmap from a server to set as the background image for the LinearLayout.
How do I set the background image of the LinearLayout without resizing the view if the received bitmap is larger than the contents of the linear layout?
It would be nice if the background image could maintain its aspect ratio, and scale to match the width of the screen.
I tried overriding onMeasure as a temporary solution, but that just wound up biting me.

how about using an imageView inside your layout , which also has its adjustViewBounds set to true in order to keep aspect ratio?

Related

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/formatting multiple imagebutton array in Android

So I have this task to create a horizontal scrolling array of image buttons that are basically photo avatars of users. These avatars aren't constrained by aspect ratio or size, and so I've been playing with ways to scale them and format them. I've gotten them scaling via the scaletype="fitCenter" and using static width and height. But what I really want them to do is to butt up against one another. Currently if an image is taller than it is high, you get the kind of letterboxing but on the sides vs. the top (blank areas). I've tried all the different scaling values, wrapping each imagemap within a linearlayout, etc., but nothing I try seems to get rid of those (while displaying the entire image to scale). Is there any way to do this?
Just to reiterate what I think you're doing, you have three image scenarios:
Square image
Landscape image (wider than tall)
Portrait image (taller than wide)
Laying out a row of fixed-size ImageViews (or ImageButtons) using FIT_CENTER works great for what you need if all the images were either square or landscape, because the scaling will always make the image stretch to the horizontal bounds of the view (the largest dimension). However, with portrait images, the scaling causes the view to be inside the bounds of your fixed-size view so that the entire image height can be visible.
If you need to maintain the aspect ratio of the image, there really is no ScaleType to help with this because the logic would be circular (fit the view to the image, while simultaneously fitting the image to the view). The solution is to adjust the size (specifically, the width) of each ImageView to match what the image will be scaled to. Here's a sample of a factory method you might use to generate the ImageView to fit the image you want to put inside it. You could also modify this slightly to reset parameters on an existing ImageView if you like:
private ImageView getImageViewForThumbnail(Bitmap thumbnail) {
float viewHeight = //Your chosen fixed view height
float scale = ((float)thumbnail.getHeight()) / viewHeight;
float viewWidth = thumbnail.getHeight() / scale;
ImageView view = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int)viewWidth, (int)viewHeight);
view.setLayoutParams(params);
view.setScaleType(ScaleType.FIT_XY);
view.setImageBitmap(thumbnail);
return view;
}
You're basically just calculating what the aspect width of the ImageView should be to match the fixed height you've chosen for all of them.
HTH
Use the scaleType fitXY, it stretches the image to the layout params you assigned, if the image has less dimensions and also shrinks the image to the layout params you assigned, if the image is large. The key point is to mention the image layout params to the imageView , that is the width and height of the image.

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

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

How to Change size of ImageView Dynamically?

I am creating a dynamic table whose rows contains a imageview and a textview. My problem is this imageview is taking full size of original image but I want to change the size of imageview.I have used setLayoutParams which has no effect.
As an alternative I also used textview instead of imageview and set image as textview's background and used setWidth and setHeight but it has the same problem.
Plz Help Me.
Have you tried the scaleType-Attribute?
Check out the setMaxWidth and setMaxHeight-methods of the ImageView-class:
To set an image to be a maximum of 100
x 100 while preserving the original
aspect ratio, do the following: 1) set
adjustViewBounds to true 2) set
maxWidth and maxHeight to 100 3) set
the height and width layout params to
WRAP_CONTENT.
Link
Assuming your image is a drawable resource, you can use ScaleDrawable instead, and use the xml attributes android:scaleHeight and android:scaleWidth to shrink your image.
OK I found the Solution
Resizing an ImageView within a TableLayout programmatically
Try changing your ImageView's layout_width and layout_height inside the layout file to some constant values measured in sp, for example 100sp. This will make all ImageViews look the same.

Categories

Resources