I am using Picasso to show image from url. My question is how can I resize the image to a variable heigth and width without crop the image? Currently when I resize the image without centerCrop() it stretches the image. This is what I have:
// Get current display dimensions
DisplayMetrics metrics = getResources().getDisplayMetrics();
width = metrics.widthPixels;
height = metrics.heightPixels / 2;
// Set image from URL in ImageView
Picasso.with(getApplicationContext())
.load(path)
.placeholder(R.drawable.loading).resize(width, height)
.centerCrop().into(image);
You need to use a method that will preserve the aspect ratio of your image. I would try
Picasso.with(getApplicationContext())
.load(path)
.placeholder(R.drawable.loading)
.resize(width, height)
.centerInside()
.into(image);
first, but it's not really clear to me what your exact needs are from your code. Do you want there to be padding around the image?
Related
I have added an Image View, and set these values.
<ImageView
android:id="#ivNewsHeader
android:layout_width="match_parent"
android:layout_height="250dp"
android:scaleType="centerCrop" />
I get images from server, and they are very high res images, if i add them as is, i end up getting OutOfMemoryError.
So i learnt that picasso offers a great way to tackle this. I used following, and this scale down any image which is higher than the mentioned dimensions.
Picasso.with(getApplicationContext()).load(imageURL).
resize(180, 130).
onlyScaleDown()
.into(ivNews);
Now when i choose these resize(180, 130), the performance is very good and i don't get any OutOfMemoryError, but the image quality is very poor.
So my question is how to choose resize number, and what equation should is use to put correct width-height numbers in resize() method to get the perfect image without compromising the quality and performance.
My imageView height is 250dp and width is match_parent
Specify scale value based on device size
Device width in pixel
public static int getScreenWidth() {
return Resources.getSystem().getDisplayMetrics().widthPixels;
}
Convert DP to Pixel
public static int dpToPx(int dp)
{
return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}
Now use these two function
Picasso.with(getApplicationContext()).load(imageURL).
resize(getScreenWidth(), dpToPx(250)).
onlyScaleDown()
.into(ivNews);
I think fit() is what you need, because will crop your image to ImageView size on screen. In this case, you don't need do to any more calculations.
Picasso.with(getApplicationContext())
.load(imageURL)
.fit()
.centerInside()
.into(imageView);
Try using fit() which is measuring the dimensions of the target ImageView and internally uses resize() to reduce the image size to the dimensions of the ImageView.
Quoting from this article
I need to load some images from URI to Bitmap variables and perform some operation with them togheter. I need the bitmaps to be squared images with fixes size, scaled down and cropped. By now I use this code:
return Picasso.with(c).load(imageUri).resize(size, size).get();
but, obviously, the image will be resized without keep its aspect ratio.
I want to resize the image with these requirements:
the smaller dimension (width or height) should be equals to size
the greater dimension should be cropped to size, keep image centered
The key is using centerInside after resize. See link
Picasso.with(c).load(imageUri).resize(size, size).centerInside().get()
set your imageview height and width fix inside xml and then set image to imageview like
Picasso.with(YourActivityName.this)
.load(imageUri)
.into(imageview_id);
I am using Glide frame to load a picture, I want to set the height and the width of it, and I know what is the width I want, but how can I set the height of it.
what I have tried is blow,and I got an error of OOM. waiting for help, thanks.
if you want change size
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.override(600, 200) // resizes the image to these dimensions (in pixel). does not respect aspect ratio
.into(imageViewResize);
Reference
Using override method of glide you can set the dimension of image in pixels.
Glide
.with(context)
.load(url)
.override(50 , 50)
.into(imageview);
I have a vertical LinearLayout where one of the items is an ImageView loaded using Picasso. I need to rise the image's width to the full device width, and to display the center part of the image cropped by a fixed height (150dp). I currently have the following code:
Picasso.with(getActivity())
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resize(screenWidth, imageHeight)
.centerInside()
.into(imageView);
Which values should I put into screenWidth and imageHeight (=150dp)?
You are looking for:
.fit().centerCrop()
What these mean:
fit - wait until the ImageView 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.
In some case the fit() is useless. Before you must wait for the width and height measurement to end. So you can use globallayoutlistener. for example;
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
public void onGlobalLayout() {
Picasso.with(getActivity())
.load(imageUrl)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.resize(screenWidth, imageHeight)
.fit
.centerInside()
.into(imageView);
imageView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
I am getting image's height and width from API. I'm using Picasso to load images from url. My query is once we would get the API response how can we draw image as per it's height and width before actual image loading by Picasso. I have set image's height and width to Picasso in resize() method.
But Picasso loads images directly once image successfully downloaded. Here the issue is that I want draw image's height and width in preloading state.
/*
* set image view's height and width from API
*/
int imageViewHeight = getHeight();
int imageViewWidth = getWidth();
// picasso to laod an image from url
Picasso.with(view.getContext())
.load(url).placeholder(getGradientDrawable())
.resize(imageViewWidth, imageViewHeight)
.centerInside()
.into(imageView);
// gradient for placeholder
static GradientDrawable getGradientDrawable() {
GradientDrawable gradientDrawable = new GradientDrawable();
gradientDrawable.setShape(GradientDrawable.RECTANGLE);
gradientDrawable.setColor(Color.GRAY);
return gradientDrawable;
}
Any help appreciated.
I'm not sure if I understood you right but if you want a place holder for your image while Picasso doing a background download, you can use placeholder() function like below:
Picasso.with(context).load(url).placeholder(drawable).into(imageview);
you can create a drawable with your desired width and height or use a resource ID
This helped do it for me, setting the imageview max and min height and width to the image's dimensions before calling the Picasso library to load the URL. It might help someone else who is looking at this.
imageView.setMinimumWidth(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));
imageView.setMinimumHeight(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));
imageView.setMaxWidth(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));
imageView.setMaxHeight(Integer.parseInt(this.reply_list_full.get(position).getImage_width()));