Android Picasso resize() not consistent - android

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.

Related

Android crop imageview

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.

ImageView Scaling: fix height variable width with maxWidth

I am going to show some images, but my constraint is as title. I want to make all images have same height but according to their width they must have different width. I just do not want to lose any part of the image when scaling. here is my code:
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/ImageView_IssueFirstLookActivity_magas"
android:layout_width="wrap_content"
android:layout_height="170dp"
android:layout_marginLeft="32dip"
android:layout_weight="2"
android:maxWidth="40dp"
android:adjustViewBounds="true"
android:scaleType="fitStart" />
I tried all I could with different values of android:scaleType but get nowhere.
here is a figure that what I want but when I play with those properties it dose not scale it as I want for example some time it dose not show image width completely and it crops it badly. Another try for example is android:scaleType="fitStart" which cause the width of my images to be fine but the heights are not fit anymore. Any idea?
andriod ImageView attributes does not support the requirement you said just by statically choosing combination of width,height and saleType in the layout xml.
Instead you can do it in the java code by actually finding the width for the image for a given fixed height and setting them programattically to each view in dp(make sure you do the pixel to dp calculation for achieving right output, salable across devices)
If you don't mind an extra dependency, you can do it with Picasso
Picasso.get()
.load(R.drawable.my_image_resource)
.placeholder(R.drawable.ic_image_placeholder)
.error(R.drawable.ic_broken_image)
.resize(0, (int) imagefixedHeight)
// optional:
// .centerInside()
.into(myImageView);

How to auto center crop ImageView Android?

I made a listView images. My goal that each item of my listView have the same height of 100dp. I encounter a problem that my imageView is resized automatically, but does not take the entire space of the parent.
Here is my current result and objectif result :
Does anyone know the option to add to make crop center automatically?
I am not sure if I understood correctly. It must be either of the two, that you want, I guess.
In your image view set the attribute
android:scaleType="fitXY"
to fit the ImageView completely.
You can choose
android:scaleType="centerCrop"
to crop Center.
i know it's late but maybe it would be helpful for someone coming on this question page by using android:scaleType="centerCrop" image is centered but it is cropped from all sides so it would be better to use this property instead that is fitCenter and with this use another property too to maintain the aspect ratio
android:adjustViewBounds="true"
android:scaleType="fitCenter"
Are you aware of the cropping options on ImageView?
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
You will just apply these as an attribute in your XML:
like:
android:scaleType="centerCrop"
in java: imageView.setScaleType(ScaleType.FIT_XY);

Prevent Image Scaling in Android App

I have an image that I am downloading from the internet in my android application. I do not want this image to be resized/scaled. Is there anyway I can stop the image from being scaled?
Here is my XML entry for the ImageView:
<ImageView
android:id="#+id/conditionImage"
android:scaleType="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textMain"
android:contentDescription="#string/noaaLogoDescription" />
Here is my code for showing it in the image view:
BitmapFactory.Options bmpOptions = new Options();
bmpOptions.inScaled = false;
URL url = new URL("URLPATHTOIMAGE");
ImageView conditionImageView = (ImageView)findViewById(R.id.conditionImage);
InputStream content = (InputStream)url.getContent();
conditionImage = BitmapFactory.decodeStream(content, null, bmpOptions);
conditionImageView.setImageBitmap(conditionImage);
I have tried changing the scaleType in the ImageView XML element to use matrix, center, and centerInside. My last try was using the above code to set inScale to false. Does anyone have any other ideas?
The odd part is this is not happening in the emulator.
Have you put all images in simple "drawable" folder. I mean without extending hdpi,mdpi etc.
Try set
android:layout_width="wrap_content"
android:adjustViewBounds="true"
EDIT:
Not sure why, but changing
conditionImageView.setBackgroundDrawable(getResources().getDrawable(R.id.conditionImage));
to
conditionImageView.setImageDrawable(getResources().getDrawable(R.id.conditionImage));
solves my problem.
Adnan, using your example of giving the image view a fixed width and height and also removing the scaleType=centerImage fixed the issue.
This is a simple problem. The scaling happens when the ImageView is a lot larger than the rendered image itself. To avoid this ugly pixelated scailing, just "wrap content" the imageview and you should get your original image quality.
I resolved this problem by hardcoding height of imageview ( as I know it in advance) and then set the scaleType as cropCenter
see the below code for more clearity.
<ImageView
android:id="#+id/iv"
android:layout_width="match_parent"
android:layout_height="75dp"
android:scaleType="centerCrop" />

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.

Categories

Resources