The wrap_content
in Imageviews only works if the image is already of known size, since the layout is inflated as soon as the Activity starts. And in your case your image will still be uploaded from the web.
My suggestion, since you are using Picasso, is to load the image as a Bitmap to a Target
and use the callback method onBitmapLoaded()
of the same to determine that the image is ready, then you still in this method can access the properties of width and height of the image and resize the Imageview according to the SetLayoutParams()
.
Something like that:
Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
if (bitmap != null) {
int width = bitmap.getWidth();
int height = bitmap.getHeight();
// OBS: Crie um LayoutParams de acordo com o ViewGroup pai da ImageView.
// No meu exemplo, estou supondo que a ImageView está dentro de um
// FrameLayout.
imageView.setLayoutParams(new FrameLayout.LayoutParams(width, height));
imageview.setImageBitmap(bitmap);
}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {}
};
Picasso.with(this)
.load(url)
.into(target);
Retire
android:scaleType="fitEnd"
or useandroid:scaleType="center"
– ramaral
@Ramaral didn’t work, I took the
android:scaleType="fitEnd"
and also tried to put theandroid:scaleType="center"
– Matheus Rohwedder
Remove the scaleType and add
android:adjustViewBounds="true"
– ramaral
The adjustViewBounds (in API’s smaller than 17) does not "stretch" the image to occupy the entire Windows image if the image is smaller. For images larger than Imageview it "shrinks" correctly. You have to treat this if you are going to use this attribute.
– Márcio Oliveira
@Márciooliveira I think that in this situation has no problem, since it is being used
wrap_content
– ramaral
@ramaral, take a look: https://inthecheesefactory.com/blog/correct-imageview-adjustviewbounds-with-adjustable-imageview/en
– Márcio Oliveira
@Márciooliveira Yeah, I know. The problem is that I don’t know if the AP wants the image to increase or get the size it has. But how does it say that
fitCenter
did not solve should be because he wants it to increase and in this caseandroid:adjustViewBounds="true"
only works for API17+.– ramaral
@Márciooliveira am using API 26
– Matheus Rohwedder