Change size of FrameLayout when screen size has been found - android

At the moment I have a FrameLayout which will hold a preview of a picture taken. The size of this is determined in the layout xml file I created for it, at the moment its just set to fill_parent. However the image taken is at 4:3 aspect ratio, but this isn't always going to be the right aspect ratio for the users screen.
For example with my Xperia X10 which is 480 X 854, the 4:3 image taken by the camera always stretches to fit the FrameLayout.
What I want to do is change the size of the FrameLayout outside of the XML file so that it is 4:3 and a size appropriate for the users screen size. Any ideas?

I guess you are using an ImageView inside your FrameLayout to display the actual picture? In this case, use scaleType to define how the picture should be stretched.
You can set that in your XML, just use android:scaleType=".." inside your ImageView tag.

Framelayout.LayoutParams lp = new LinearLayout.LayoutParams(width, height);
lv.setLayoutParams(lp);
Try using the above code...

Related

How do I set an ImageView's max height to available space on screen?

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.

Save Aspect Ratio in ImageView - Android

I have an application which the user can capture images and then send them
to the server. I set the image to be as Base64 and then send it to the server.
In the application I have this screens:
Feed
Post Screen
Fullscreen Image (Landscape)
When the user enters to each screen I get from the server a Base64 and
decode it to bitmap and set it to be the source of the imageview.
each screen has it's layout.
for the feed and post screens I set the imageview width and height to be hardcoded with some dp, and for the fullscreen image I set them to be fill_parent.
Now for each imageview I set the scale type to : fitXY, but I see that the
pictures are streched and in the fullscreen mode I can see the pixels.
I want to set the image to fit the xy but to be smooth as possible
I tried to set the adjustToBound to true and scaleType to cropCenter, fitCenter
and etc but it doesn't help.. how can I save the aspect ratio?
In order for adjustViewBounds to work, one dimension of the ImageView needs to be set to a hard value like dp value or match_parent, and the other dimension needs to be set to wrap_content. adjustViewBounds will make the wrap_content dimension adjust to be the right size to keep the image's aspect ratio.

ImageView fills different screen sizes, but maintaing the aspect ratio

I have a rectangular image in an image view. I want to fit the image in the screen for different screen sizes while maintaining the aspect ratio.
In smaller screen its working fine, but its not getting stretched in biggerscreens. Some gap remains in the bottom of the image.
This is my code:
LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
LayoutParams params1 = new LayoutParam(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
mMainLayout = new LinearLayout(context);
mMainLayout.setLayoutParams(params);
backgroundImage = new ImageView(context);
backgroundImage.setId(0);
backgroundImage.setAdjustViewBounds(true);
backgroundImage.setScaleType(ScaleType.FIT_CENTER);
backgroundImage.setLayoutParams(params1);
backgroundImage.setImageResource(R.drawable.background_circles_en);
mMainLayout.addView(backgroundImage);
I have used many combinations of fill_parent, wrap_content with multiple scaleTypes: fitCenter, centerInsideand they all draw the images in the right aspect ratio, but none of them actually scale the images up and the ImageView itself, resulting in either the TextViews get pushed all the way down off the screen, blank spaces inside the ImageView, or image not scaled.
Please give a right combination so that it will work properly for different screen sizes.
Privide ScaleType FIT_XY, it will scale image to x and y dimensions, irrespective of aspect ratio.

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.

android image view problem

I have one question. I want to set one image to image button. I have one button image of size 90 x 48. I want to set this image on Image button.
Problems:
I need to set image on background parameter or src parameter?
If i set image on background parameter its not showing its actual size(smaller than actual size) if i have set the Layout width = wrap content and Layout Height= wrap content. If i give Layout width = 98dip and Layout Height= 48dip then its showing the same size. Is it a coorect way?
thanks
set image as background Parameter sometimes src makes problem
if you are set width and height as wrap_content then android will check the screen size and try to adjust to your screen if screen size is big then android displays your image as its actual height and width and if screen size is less then it will compress your image
so best way is to set height and width is as wrap_content if you have to use portrait and landscape mode both
Yes.
If you use Layout width = wrap content and Layout Height= wrap content, image will be as actual size. Maybe just the density of your monitor is less than the density of your phone?
Remember dip != pixels.

Categories

Resources