Android crop imageview - android

There is my ImageView
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="150dp" />
I load image to this ImageView using Picasso library.
Picasso.with(getActivity()).load(url)
.config(Bitmap.Config.RGB_565)
.into(imageView);
There is problem that there is padding inside ImageView and loaded image.
There is image to show padding:
I use .fit() method to avoid this gap, but image stretches and quality losts.
(You can see that this dark-blue circles is ovals now)
The question is:
What should I do to avoid loosing quality of image and resize it directly to ImageView. I can't add .centerCrop() because left side of image is logical safe-zone. Also I can't change layout_height="150dp" because it will break all my layout.
Anyway thank you!

Look at your ImageView:
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="150dp" />
You are forcing the image to stretch to the width of its parent view (the screen maybe?). Try using wrap_content for the width and keeping the height the same. This may cause the image to shrink, but its up to your desired UX.
#stefs found that adding this:
...android:adjustViewBounds="true" />
would help. Check out the post here.
If you want static image sizes, you may have to deal with the padding.

Related

how to fix stretching image in image view in android when i use fitxy Scale type in android

<ImageView
android:id="#+id/bigimages"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_below="#+id/productTitle"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="40dp"
android:scaleType="fitXY"
android:adjustViewBounds="true"
android:src="#drawable/bath_and_body_1" />
Glide.with(this)
.load("https://images-eu.ssl-images-amazon.com/images/I/41ZNY84Q-7L.jpg")
.placeholder(R.drawable.cancel_icon)
.into(bigimages);
https://images-eu.ssl-images-amazon.com/images/I/41ZNY84Q-7L.jpg
this is my code and image URl i have to display full image without streching but i am not able to display its stretched please suggest me how to fix it . below is current image which is stretched
enter image description here
how to fix there is stretching in text yellow....
YOu can try :
android:scaleType="fitXY"
android:adjustViewBounds="true"
Change to
android:scaleType="centerCrop"
or
Glide.with(this)
.load("https://images-eu.ssl-images-amazon.com/images/I/41ZNY84Q-7L.jpg")
.placeholder(R.drawable.cancel_icon)
.centerCrop()
.into(bigimages);
Of the various scaleType options, fitXY is specifically made to stretch the image. It will make sure that the entire image is stretched/shrunk to fit in exactly the space provided.
You probably want centerInside; this will make sure that the entire image fits within the view, and will not distort the aspect ratio. If your view is square and the image is rectangular, centerInside will scale the image down so that it fits inside the view, meaning either the top+bottom or left+right will be empty.
Alter your Glide implementation with:
Glide.with(this)
.load("https://images-eu.ssl-images-amazon.com/images/I/41ZNY84Q-7L.jpg")
.override(width, height) //set width & height as you want
.palceholder(R.drawable.cancel_icon)
.into(bigimages);
References:
Glide Image Resizing & Scaling
Glide Documentation

Android Picasso resize() not consistent

I am using Picasso to resize my background images on my Android interface.
I am stretching the width of the image to fill the width, but I cannot adjust the height, as the background image needs to line up with the circular image in the middle.
Problem is that on different devices, the height appears to adjust as the background is not positioned consistently. I would post an example but I can't as I dont have 10 reputation yet.
Here is my java
ImageView imgBackground = (ImageView) v.findViewById(R.id.imgBackground);
aParent = (MainActivity) getActivity();
//Picasso.with(getActivity()).load(R.drawable.morn_blur_bg).fetch();
Picasso.with(aParent.getApplicationContext())
.load(R.drawable.morn_blur_bg)
.resize(aParent.getBackgroundWidth(), aParent.getBackgroundHeight())
.into(imgBackground);
And this is my XML
<ImageView
android:id="#+id/imgBackground"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="center"
android:layout_alignParentTop="true" />
Any help is greatly appreciated.
It turns out that the imageview was stretching the image after picasso did its thing.
I tried to set the imageview height, but the OS overrode me.
In the end I set imageView.scaletype="matrix", and now it does not stretch my image.
hope that helps someone else, i've spent a lot of time on this.
In my case, I put android:scaleType="centerCrop" on my ImageView and then no need to apply centerCrop() through Picasso.

Android ImageView dynamic ScaleType

I have an Android Activity with an ImageView.
I put the ScaleType of the imageview on CENTER_CROP, so the image itself would not stretch.
<ImageView
android:id="#+id/imgView"
android:layout_width="fill_parent"
android:layout_height="135dp"
android:scaleType="centerCrop"
android:src="#drawable/capture" />
The image that's loaded at first does what I expect, it's cropped to the center, filling the whole ImageView.
Now, if I set the image of the ImageView programatically,
imgView.setImageBitmap(b)
imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);
The picture gets displayed, but it's completely stretched out to fill the ImageView...
AdjustViewBounds has no effect what so ever, nor does anything else.
The difference between the working XML and the not-working java code is that in XML I'm putting the image as a resource, and the code is putting a Drawable in the ImageView from a web-service...
Does anyone have an idea to the solution?
I am not sure but android:scaleType="fitXY" or android:scaleType="fitCenter" may be the solution of your problem.
I solved this by using the original image from my camera-intent. For some reason, all thumbnail bitmaps were stretched out and in landscape mode.

How to create an ImageView that fills the parent height and displays an Image as big as possible?

I have an ImageView that is defined in the following way:
<ImageView
android:id="#+id/cover_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_below="#id/title"
android:layout_above="#id/divider"
android:adjustViewBounds="true"
android:src="#drawable/image_placeholder"
android:scaleType="fitStart"/>
Now after downloading a new bitmap I change the drawable. The image now appears in the top left corner of the ImageView. Is there a way to have the image fill up the whole height that is possible and then adjust the width of the view to enable scaling the image without changing the ascpect ratio?
The image fills up all the space on a standard screen but on a WVGA Resolution the image takes only about half of the actual height of the ImageView.
If I'm understanding you correctly, what you need to use is the centerCrop scaleType. fitStart scales the image proportionally, but neither the width nor height will exceed the size of the view, and the image will, as you said, have a top|left gravity.
Using centerCrop scales the image proportionally, but causes the shortest edge of the image to match the size of the view, and if there is additional data on the long side that does not fit, it is simply cropped off. The gravity is, of course, center. The below worked for me:
<ImageView
android:src="#drawable/image_placeholder"
android:id="#+id/cover_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="centerCrop"
/>
You can change scale type to fitXY via call to
imageView.setScaleType(ScaleType.FIT_XY);
simply do it in xml like
android:scaleType="fitXY"
Basically the answer here is that there is no predefined value for what you are trying to achieve. The solution is to create a Matrix that fits to your needs and call setImageMatrix(matrix) on your ImageView.

Android image scaling and alignment

I'm trying to show an image scaled while preserving its aspect ratio, but at the same time align it to the bottom.
Using android:scaleType="FitXY" causes the image to be centered vertically and horizontally, so it doesn't get aligned to the bottom.
Using "FitEnd" causes the image to be aligned to the bottom right corner, so it isn't centered horizontally.
Is there any way to get around this? Maybe using some matrix to scale it (scaleType="matrix")?
EDIT: To clarify a bit on what it is I want exactly...
I have an ImageView, whose layout (location, size, gravity, etc.) I can't change. I want to load a bitmap as its source image, but have that bitmap get scaled to the ImageView's size (preserving the aspect ratio of the bitmap) and then aligned to the ImageView's bottom.
EDIT: After trying everything I could think of (and everything that was suggested here), we ended up sub-classing ImageView and doing the scaling/translation ourselves in onDraw.
Try using android:scaleType="centerInside" instead of fitXY.
Layout being used in a Blrfl Labs application to scale an image to fit the area the layout gives it without hosing up the aspect ratio:
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0"
android:gravity="center"
android:src="#drawable/blah_blah_blah"
android:scaleType="centerInside"
android:adjustViewBounds="false"
/>
You can use
android:layout_centerHorizontal="true"
android:scaleType="fitEnd"
if your ImageView is inside a RelativeLayout.
Untested, but try adding:
android:layout_gravity="bottom|center_horizontal"
to your ImageView along with FitXY scaling.

Categories

Resources