Resize image to full width and variable height with Picasso
Finally I solved it doing a transformation of Picasso, here is the snippet:
Transformation transformation = new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
int targetWidth = holder.message_picture.getWidth();
double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
int targetHeight = (int) (targetWidth * aspectRatio);
Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
if (result != source) {
// Same bitmap is returned if sizes are the same
source.recycle();
}
return result;
}
@Override
public String key() {
return "transformation" + " desiredWidth";
}
};
mMessage_pic_url = message_pic_url;
Picasso.with(this.context)
.load(message_pic_url)
.error(android.R.drawable.stat_notify_error)
.transform(transformation)
.into(holder.message_picture, new Callback() {
@Override
public void onSuccess() {
holder.progressBar_picture.setVisibility(View.GONE);
}
@Override
public void onError() {
Log.e(LOGTAG, "error");
holder.progressBar_picture.setVisibility(View.GONE);
}
});
This line is for customize with your desired width:
int targetWidth = holder.message_picture.getWidth();
Additionally this snipped include Callback for loading hide and error drawable built-in Picasso.
If you need more information to debug any error, you MUST implement a custom listener (Picasso builder) beacuse the onError
Callback
information is "null". You only know that there is an error for UI behavior.
I hope this helps someone to save many hours.
Resize image to full width and fixed height with Picasso
You are looking for:
.fit().centerCrop()
What these mean:
fit
- wait until theImageView
has been measured and resize the image to exactly match its size.centerCrop
- scale the image honoring the aspect ratio until it fills the size. Crop either the top and bottom or left and right so it matches the size exactly.
Resizing image with picasso
try this:
for your imageview:
<ImageView
android:id="@id/img"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true" //add this
android:scaleType="fitCenter" />
And in picasso
use .fit().centerCrop()
Picasso.with(getContext()).load(mSelectedTrack.getArtworkURL()).resize(800,300).fit().centerCrop().into(mAlbumCoverArt);
How to set ImageView height and width before image is loaded with Picasso inside Recycerview StaggeredGridLayout
Calc the aspect ratio of your image by width/height
and wrap your ImageView inside something like this:
public class RelativeSizeLayout extends FrameLayout {
private float aspectRatio = 1.77f;
public RelativeSizeLayout(Context context) {
this(context,null);
}
public RelativeSizeLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public RelativeSizeLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean shouldDelayChildPressedState() {
return false;
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int width = MeasureSpec.getSize(widthMeasureSpec);
heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (width / aspectRatio), MeasureSpec.EXACTLY);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
public void setAspectRatio(float ratio) {
aspectRatio = ratio;
}
}
Android Picasso resize() not consistent
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.
scaling image size in Picasso
Breaking the first part down in to two sections:
I want the image to display in it's original aspect ratio
To do this you can call .fit().centerInside() on the Picasso request
scaled to be a reasonable size - ie 75% of the screen width
The easiest way to do this would be to specify this in your layout, so set an ImageView to be this width. Then when Picasso loads the image into this ImageView it will automatically scale it to fit (but keeping the aspect ratio as specified above).
Related Topics
Format Credit Card in Edit Text in Android
Android Design Considerations: Asynctask VS Service (Intentservice)
Estimating Beacon Proximity/Distance Based on Rssi - Bluetooth Le
How to Update/Refresh Specific Item in Recyclerview
Refreshing Activity on Receiving Gcm Push Notification
Adjust Icon Size of Floating Action Button (Fab)
Hardcoded String "Row Three", Should Use @String Resource
What Does the Filter Parameter to Createscaledbitmap Do
How to Put a Constraintlayout Inside a Scrollview
Android Studio: How to Generate Signed APK Using Gradle
Resize Image to Full Width and Variable Height with Picasso
How to Scroll to Top of Long Scrollview Layout
Transfer Data from One Activity to Another Activity Using Intents