Android: How to center crop images without scaling - android

I have some images I want to show in thumbnails, and I want them to center crop and not scale. The images themselves are almost always rectangular, and I want to crop them around their center and have them fit into a square ImageView. The bitmaps themselves either have a height that matches the size of the ImageView, or a width that matches. So essentially I just want to crop off the left/right or the top/bottom of each photo, and not lose any quality.
When I try to use ScaleType.CENTER_CROP my images are becoming blurry. Any suggestions?

You should just use ScaleType.CENTER. This will perform no scaling, it will just center the image behind the image frame. Anything falling outside the ImageView will be cropped.

Related

How to resize image in Android without shrinking it?

I know this is a bit unusual, but I want to resize the bounds of an image without the image itself being scaled down.
For example, when I have an 200x200 image, and resize it to 100x200 the result should just be half of the image, where the other half is just cut.
Is that possible?
I don't know how you resize the image in your app, but if you resize any 200x200 image to 100x200, this result will be a part of your initial image, depending on the starting point.(could be the 1st half starting at pixel 0, the 2nd half starting at pixel 100 or any other half in between depending on the starting pixel)

Round shaped image is elongated horizontally or vertically

I have to display a splash image which has a round shaped object (a Ball). The Layout for splash is a simple linear layout with just a single Image view to occupy the full screen.
Image : single image with the size of 1280 x 720.
When my splash screen is shown in the App, The round object is shown in different shape in different screen sizes. I hope the aspect ratio and the resolution is the cause for these elongated images.
Could you please suggest an idea / approach to solve this ?
Do I need to consider the aspect ratio or the resolution or both ?
Finally the ball should look like a ball in all the devices :)
Thanks in Advance.
1) Yes, by default Android will scale your image down to fit the ImageView, maintaining the aspect ratio. However, make sure you're setting the image to the ImageView using android:src="..." rather than android:background="...". src= makes it scale the image maintaining aspect ratio, but background= makes it scale and distort the image to make it fit exactly to the size of the ImageView. (You can use a background and a source at the same time though, which can be useful for things like displaying a frame around the main image, using just one ImageView.)
2)You should also see android:adjustViewBounds to make the ImageView resize itself to fit the rescaled image. For example, if you have a rectangular image in what would normally be a square ImageView, adjustViewBounds=true will make it resize the ImageView to be rectangular as well. This then affects how other Views are laid out around the ImageView.
You can change the way it default scales images using the android:scaleType parameter. By the way, the easiest way to discover how this works would simply have been to experiment a bit yourself! Just remember to look at the layouts in the emulator itself (or an actual phone) as the preview in Eclipse is usually wrong.
Reference : How to scale an Image in ImageView to keep the aspect ratio
set imageView property
scaleType="centerInside"
Add scaled versions of the image with the same file name under folders 'res->drawable','res->drawable-ldpi','res->drawable-hdpi' and under xhdpi "http://developer.android.com/guide/practices/screens_support.html#DesigningResources"

Custom crop on Android

I'm writing a custom image crop.
I have an image view and a rectangle to choose area for cropping on it.
After cropping I need to know a top-left point of rectangle, that present cropping area.
But when i try to get it, Rect gives me a X, Y and width coordinates of the screen, not image. How can I get a real coordinates of the image?
I guess you could apply a simple proportionnality rule using the actual size in pixels of the device screen (assuming the image is displayed in full screen). You can easily get the screen size using this very complete answer: https://stackoverflow.com/a/1016941/1417179
From there, if your image dimensions is wh and the screen dimensions wshs, you can get the coordinates in image space using image_coordinates = screen_coordinates*w/ws.
You may have to cheat a little if the image displayed is not of the same aspect ratio than the screen, but the idea remains the same.
Hope this helps!

Padding out a bitmap to make it a power of two

I have a bunch of images of dimension 480x800. The intention is that these will fill the entire screen of the device.
I would like to take the image and use createScaledBitmap() to resize the image to fit the screen exactly. I then want to put this bitmap inside another bitmap (not stretch it), which will have dimensions that are a power of two.
So for example, if the screen size is 320x480 I resize my 480x800 image to be 320x480. I then want to place this 320x480 image inside of a bitmap that is 512x512 so that the original image is nested in the top left corner (with the extra space being blank).
How can I achieve this on android? I have gotten so far as to resize the image to fit the screen exactly, but not making the power of two bitmap and filling it.
Solved this by using a texture atlas

Android: Stretch & Crop Image (but remains Aspact-ratio)

I would like to stretch and crop an image so that it shows full screen on a Android device.
For example, inside the red-rectangle above would be the only area displayed on the phone.
I want it to center and crop. If the height of the image is small than the height of the device, I want to stretch the image as well.
By the way, I don't NEED images to be cropped. I just want my ImageView to display only the reg-rectangle part given the landscape image.
Have you tried setting android:scaleType="centerCrop" on your ImageView in XML? If I understand what you're asking for correctly, it should do just that.

Categories

Resources