How to set image fit on every screen with aspect ratio - android

I have some Images, coming from the the server, I am confuced regarding image resolution. how it is possible to show every image complete on every screen with the aspect ratio.
I need to cover the entire screen with no crop. Image should be complete on every resolution screen.
<ImageView
android:id="#+id/photo_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
Image coming from server
Picasso.with(context).load(shopDataForAdvs.get(position).getSrc()).fit().placeholder(R.drawable.product_image).error(R.drawable.product_image).into(imageView);
Here I have Image which is streaching

Decided to upgrade my comment to an answer...
Every image has an aspect ratio. Every phone has an aspect ratio. There is no guarantee that these two aspect ratios will be the same, so you have three options:
1 - Stretch the image to fit. This is what android:scaleType="fitXY" and the Picasso fit() call are doing. I don't really recommend this choice, as the image will look "weird", but it is a valid choice.
2 - Crop the image to fit. This would be android:scaleType="centerCrop" and Picasso centerCrop(). This will scale the image up (without stretching it) until the smallest dimension of the image matches the dimension of the phone, and will crop the image in the other dimension.
3 - Letterboxing. This would be android:scaleType="centerInside" or Picasso centerInside(). This will scale the image up (without stretching it) until the largest dimension of the image matches the dimension of the phone, and will leave the rest of the ImageView blank.
I recommend option 2, or perhaps a combination of options 3 and 2 (load the image without cropping, but let the user zoom in if they desire).

Related

Add white spaces to an image to fit to a specific resolution

In apps like NoCrop an image is made to fit a resolution of 1:1 (made square) by adding some extra spaces at the top and bottom of the image (shown below).
Similarly, I wish to add some spaces to an image through my Android app, to fit a resolution of ratio, say 16:9. My app creates a video by stitching these images together, and for the video, all the images first need to be converted into a fixed resolution.
I have not used Bitmaps much, nor I am an expert in Android. Any kind of help would be appreciated! :)
Try adding these to your ImageView in xml:
android:scaleType="centerCrop"
android:adjustViewBounds="true"
This will crop your image so that it fits the ImageView

Resize an image to square resolution

I am developing an android application which resizes android pictures. I was wondering if its possible to resize a file with resolution (1080X1920) to (500X500) square size.
Without looking too bad (quality and dimensions).
Let me show you an example.
First image:
Resized picture:
Answers using code will be appreciated
If you don't want to distort the image you have two options:
Crop the image
Implement the seam carving algorithm
Edit: this assumes you want to fill up the whole square. If not, just keep the width/height ratio and downscale the image properly so that the largest dimension fits in the side of the square.

Android displaying a crisp image after zooming

Currently I have a set of images which has resolution of 480x640 each.
When I zoom the image using an image displayer library I got from googling (TouchImageView), the image got a bit blurry.
Is it possible to use higher resolution image so that the image will not be blurry when zoomed?
As far as I know, images will be scaled in terms of width in Android, but the height will not be scaled, right?
Here's my code for the image displayer :
<com.km.parkit.TouchImageView
android:id="#+id/overview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:contentDescription="#string/imageDesc"
android:scaleType="matrix"
android:src="#drawable/parkinglot" />

how to scale images appropriately to fit the screen

In my app I have a Gallery-similar functionality and faced the issue that I need to scale the images. My test device in Nexus 5 which has a screen width of 1080p (360dp). Below are screenshots of an image having a width of 604 pixels opened with my app and the system-default Gallery:
My app:
=============================
Gallery:
The code I use to show the image is quite simple:
pathToImage = getIntent().getStringExtra("path");
img = (ImageView) findViewById(R.id.imgPictureView);
try {
bmpImage = BitmapFactory.decodeFile(pathToImage);
img.setImageBitmap(bmpImage);
} catch (Exception e) {
e.printStackTrace();
}
Opening a hi-res camera photo causes my ImageView to adjust its width to fill the screen, however I'm getting a heap size of about 80 (!!) MB.
It's obvious that the system gallery decides whether to scale the Bitmap up/down to make it fit the screen. For example, it scales a 60px image up, but only by a small factor so it doesn't become really blurry. I guess it also scales large camera pictures down to prevent OutOfMemoryError's. So in my example above, the gallery assumed the width of 604px is enough to scale it up to 1080 without noticeable quality loss, my app of course not due to lack of any additional code.
So the question is: how can I achieve the same result and determine the factor to scale images up/down without quality loss depending on the current screen?
Note: for those who will now post tons of (beyond controversy amazing) code examples showing how to actually scale a Bitmap or determine the screen width - this is NOT my question.
Bitmap can use a lot of memory and you got to scale them down. Instead of giving you a copied solution, here's a detailed documentation:
http://developer.android.com/training/displaying-bitmaps/index.html
You really should go through the whole guide. There's a lot to learn.
Second, set your ImageView's width to MATCH_PARENT and try changing its scaleType attribute to centerCrop. Play with the other available values for scaleType to get your desired result.

showing image of sixe 64 X 64 on any size imageview without loosing its quality

I am retrieving image from server and setting it to image view.The image size is 64 X 64 .
But I want it to suppport all size image views. If I use imageview of size 150dp X 150dp
it gets blurred out,it stretches.Is there any method so that my image maintains its quality even on bigger size imageview?I had heard to use ninepatch images in drawable but here i'm downloading it from server and image size is fixed 64 X 64 .
Plz Help!!!!
NinePatch is for stretching images, usually only for background images with a single color or a gradient in one direction. So that is not your answer.
As to showing your 64x64 image as 150x150.. no. It's just as if you'd do image enlargement in an image editor - the information is not there. Although it shouldn't be that bad.
Check this to see if any alternative fits your needs:
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
Typically used:
<ImageView
android:id="#+id/image1"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_height="wrap_content"
android:layout_width="0dp"
/>
I am afraid it can't be done...Not with all image types at least. There are images called VECTOR images which retains quality at significant level of image resizes. So you should try using some vector kinda images which could be of .PNG images, etc.
Because shrinking a larger image works most of the time, but enlarging smaller images will always under distortion.

Categories

Resources