Hy!
I download a picture from the internet and want to display it. The height should be 1/6 of the max screen height (for multi phone support)
Code:
final ImageView iv = (ImageView) findViewById(R.id.ImageView01);
iv.setMaxHeight(getWindowManager().getDefaultDisplay().getHeight()/6);
Please help!
Try using the layout_weight attribute of LinearLayout to divide the screen into the parts you need. The ratio of the values you set to the different parts makes the parts become bigger or smaller.
Related
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.
In My Application i have One button like this :
The Resolution of that button is 192x32. And when i put this button in to drawable-mdpi, it seems good to layout. Now for other screen resolution for multiple screen support which size of button i have to make to see the Good Layout Design according to other Devices screen ?
I mean for drawable-ldpi and drawable-hdpi, which resolution i have to make for this button ? How to do Such calculation for to make this button size to fit for all the screen size ?
Please help me for this.
Thanks.
No need to create multiple buttons for multiple screen support.
Instead create a single button and set the width and height at run time.This is achieved by getting the display width and height. Use the bellow code to get the display H & W values.
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
based in the above values set the button width and height at runtime.
Example:
Button bt=(Button)findViewById(R.id.button);
bt.setWidth(width);//screen width(fill_parent)
bt.setHeight(height/6);//1/6 of the screen height
The above code set the button width to screen(display) width size and height to 1/6 of the screen.
There is much information on this subject on the android developers website. In particular, there is a list of the various dpi levels and what range of DPIs that they correspond to.
Also, Rather than providing different images for different resolutions, you could make the image a nine-patch, and have it auto-magically expand to fit the button. Although if you want to keep the highlighting in the background proportional, it might be somewhat difficult to make it expand vertically.
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.
I have four ImageViews in a layout. I set the maximum width for each one of them by setMaxWidth function. The problem is that they keep a small distance between them and I don't want that. If I don't stipulate the maximum width, these gaps disappear, but the ImageViews sizes are inappropriate (bigger than it should be).
The main code is:
musicSubmenuButton = new ImageView(context);
musicSubmenuButton.setId(id++);
musicSubmenuButton.setImageResource(R.drawable.bt_musicas_on);
musicSubmenuButton.setMaxWidth(buttonSize);
musicSubmenuButton.setAdjustViewBounds(true);
photosSubmenuButton = new ImageView(context);
photosSubmenuButton.setId(id++);
photosSubmenuButton.setImageResource(R.drawable.bt_fotos_off);
photosSubmenuButton.setMaxWidth(buttonSize);
photosSubmenuButton.setAdjustViewBounds(true);
agendaSubmenuButton = new ImageView(context);
agendaSubmenuButton.setId(id++);
agendaSubmenuButton.setImageResource(R.drawable.bt_agenda_off);
agendaSubmenuButton.setMaxWidth(buttonSize);
agendaSubmenuButton.setAdjustViewBounds(true);
infoSubmenuButton = new ImageView(context);
infoSubmenuButton.setId(id++);
infoSubmenuButton.setImageResource(R.drawable.bt_info_off);
infoSubmenuButton.setMaxWidth(buttonSize);
infoSubmenuButton.setAdjustViewBounds(true);
The size I wanted, but without the gaps (obtained using the above code):
The ImageViews without the gaps, but in the original resolution (obtained using the above code, but commenting the setAdjustViewBounds calls):
Does anybody know why these gaps appear and/or a workaround to get rid of these gaps and keep my desired size?
Instead of setting max width, set layout_width on them to fill_parent. Then they will space out evenly.
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