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.
Related
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.
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'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?
I need something like<img width="100%" /> for Android <ImageView>. I mean resize width to all available space (shrink or enlarge width of image) and automatically change height to keep aspect ratio.
Something like <ImageView android:layout_width="match_parent" android:layout_height="auto">
Useandroid:scaleType="fitCenter"do with image what I want, but only with image, doesn't change height of View itself. When I set booth dimensions to match_parent I will get what I want, but only if I have only one image on the screen. But I need some texts under image.
It's only way create child of ImageView?
For exampleandroid:adjustViewBounds="true"does't do anything for me.
I found where is problem. Default implementation of ImageView can't enlarge image. If image is bigger than screen width android:adjustViewBounds="true" works correctly and adjust height to keep aspect ratio of image. But if image is smaller that screen width it don't upscale it.
This is cause by this code
if (newHeight <= heightSize) {
heightSize = newHeight;
}
in ImageView class (on line 688, API 10)
I made special child of ImageView which allow enlarge image to fit width.
Here is: https://gist.github.com/tprochazka/5486822
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.