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);
Related
I am trying to load very high resolution image (15000 x 8438) with glide and saving it as bitmap.
I am using glide for that. The problem is that when I try to save bitmap image to file I get black image instead of actual image.
I feel maybe this is because of very high resolution. After converting to bitmap these values I have obtained: width 15000 height: 8438 getAllocationInBytes : 506280000.
I tried to downscale image with .downsample(DownsampleStrategy.AT_MOST) but image is still 15000 x 8438.
The code I am using is:
Glide.with(this).asBitmap()
.load(imagePath)
.downsample(DownsampleStrategy.AT_MOST)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.skipMemoryCache(true)
.into(FileTarget(someFile, Bitmap.CompressFormat.JPEG, 90) {}
Can someone please help, how can I down scale the image and save it to the file. Also is it possible to achieve it with glide ?
Thank you.
To make downsample works as expected, you probably forgot to add the maxWidth and maxHeight override:
....
.downsample(DownsampleStrategy.AT_MOST)
.override(maxWidth, maxHeight)
...
The first screenshot is with Picasso, the second one with Coil (both in latest versions). Any idea why is this happening?
Picasso: fit().centerInside()
Coil: scale(Scale.FILL).crossfade(true) (I tried with FIT also, same results)
ImageView: adjustViewBounds = true; scaleType = CENTER_INSIDE with MATCH_PARENT width and constant height in pixels.
Coil automatically adjusts to the scale type of the ImageView so you don't need to configure the scale.
Picasso does not, and Picasso's .fit().centerInside() is actually not equivalent to ImageView's CENTER_INSIDE but to FIT_CENTER (it will enlarge the image so that at least one dimension matches the ImageView). There is no equivalent to CENTER_INSIDE with Picasso but these are the closest options:
You can simply remove .fit().centerInside() and let the ImageView scale down the image if it's larger, but if the image is very large it will consume a lot of memory (and may fail to load if larger than the max texture size of the device).
You can use .resize(width, height).centerInside().onlyScaleDown() after measuring the size of the ImageView manually.
If you want Coil to resize the image the same way Picasso does with .fit().centerInside(), then just change the scale type of the ImageView to FIT_CENTER.
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 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've come across this simple problem with the Picasso library. I haven't find any way to put an image into ImageView center it like android:ScaleType="center" does without cropping. Do you have any ideas about how to solve this ? My problem is that I don't know at the runtime the height and width of the BitMap I download so I can't use resize() properly.
This is a little late, but for anyone who's looking for a solution :
Picasso
.with(context)
.load(ImageURL)
.fit()
// call .centerInside() or .centerCrop() to avoid a stretched image
.into(imageViewFit);
If your ImageView is fixed one dimension and flexible another dimension (ex. android:width="match_parent", android:height="wrap_content"), you should be able to use android:adjustViewBounds="true" to get the ImageView to resize to display your image without cropping.
i am not found fit to center without resizing,
Picasso.get()
.load(source).resize(1650,700)
.centerCrop(Gravity.CENTER).into(imageView, new Callback()