When using a recycleView with many images, Glide takes a while to load the image for the first time, how can I preload the image's height and width by setting the imageView to that size before using glide to load it in?
Glide.with(mContext)
.load(link)
.apply(RequestOptions.centerInsideTransform())
.into(holder.vhImageView);
Reddit also does it this way
Glide may have an equivalent method, but I use Picasso, and you can set a .placeHolder(R.drawable.placeholder) image that will be in place until the image actually loads.
You could assign holder.vhImageView a default image in xml that should also work as a placeholder as long as Glide doesn't clear it as it's loading the new image?
You could set a minHeight on the holder.vhImageView in XML but that can cause problems if the views loaded end up being shorter than that.
Related
I have an image that I'm trying to load with Picasso that I want to crop without scaling.
Is this possible?
For example, if I had:
an image that's 400x800
an image view that's 200x400
Any questions I've seen regarding cropping suggest using resize(x, y).centreCrop(alignGravity) (i.e. scaling and cropping rather than only cropping).
In the example, you'd get the entire image scaled to 50%, as the JavaDocs for centreCrop mentions:
This cropping technique scales the image so that it fills the requested bounds and then crops the extra, preferring the contents at alignGravity.
I want to display the middle 200x400 pixels of the image and lose the edges entirely (similar to how ImageMagick's crop would work).
Is there a way to crop without scaling?
So it seems that out-of-the-box, no it doesn't, but you can use the Picasso Transformations library to do so:
implementation 'jp.wasabeef:picasso-transformations:2.4.0'
and then:
Picasso.get()
.load(largeImage)
.transform(new CropTransformation(width, height, GravityVertical.CENTER, GravityVertical.CENTER))
.into(imageView, /* callback */);
If you are using resize() or centreCrop() you need to use after the call to transform().
I am just curious if Glide is caching placeholder in the size of image views that request belongs to. I mean, if I have a recycler view and load some image URIs to image view in list items and, say, use a placeholder with size of 512x512 px for each request, will it affect the performance? Or will the placeholder cached in the given size once and then reused every time?
Yes, you are right. Glide will cash only once 512x512 image and use it every time when needed. Comparing to Picasso Glide cashes every resized image, when Picasso resizes on the fly. Glide cashing can also be configured depending on your need using methods like diskCacheStrategy(), onlyRetrieveFromCache() etc. See this link for more information.
How do I display images in a facebook like feed? So far I've been using match_parent for width and wrap_content or fixed height. This works well but I need to show proper blank space for image feeds while it's getting lazy loaded. Like how do I set the dimensions properly for the image before loading it from the internet.
I think about getting the aspect ratio or the image dimensions itself via the api. Is it the only correct way or is there some other better way to achieve it using glide or any image loading library?
If you are using glide, you can set a place holder image or drawable with the dimensions you need while the real image is being downloaded.
Glide
.with(context)
.load(UsageExampleListViewAdapter.eatFoodyImages[0])
.placeholder(R.mipmap.ic_launcher) // can also be a drawable
.into(imageViewPlaceholder);
I have a list view where I load image with person name. There are around 538 items in the list view. I get the images of persons from a url. Currently I am doing the following:-
Picasso.with(getContext()).load(url).resize(70,70).onlyScaleDown().into(im1);
The images present in the URL are very large in size and loading of some images takes a lot of time and it eats up my memory also while loading. Can some one please help me in loading the images efficiently and fastly.
Example image for one person can be found in the following URL:-
https://theunitedstates.io/images/congress/original/D000626.jpg
You can use onlyScaleDown() to resize
Picasso
.with(context)
.load(imageUrl)
.resize(6000, 2000)
.onlyScaleDown() // the image will only be resized if it's bigger than 6000x2000 pixels.
.into(imageViewResizeScaleDown);
Or you can use fit()
Picasso
.with(context)
.load(imageUrl)
.fit()
// call .centerInside() or .centerCrop() to avoid a stretched image
.into(imageViewFit);
fit() is measuring the dimensions of the target ImageView and internally uses resize() to reduce the image size to the dimensions of the ImageView. There are two things to know about fit(). First, calling fit() can delay the image request since Picasso will need to wait until the size of the ImageView can be measured. Second, you only can use fit() with an ImageView as the target (we'll look at other targets later).
The advantage is that the image is at the lowest possible resolution, without affecting its quality. A lower resolution means less data to be hold in the cache. This can significantly reduce the impact of images in the memory footprint of your app. In summary, if you prefer a lower memory impact over a little faster loading times, fit() is a great tool.
As per the new version of Picasso
Picasso.get().load(galleryList[position].mediaUrl).fit()
.placeholder(R.mipmap.ic_launcher).into(holder?.ivImags)
I am currently using the Picasso to load the image file to image view on android. I tried to have an image resized then using fit to fit on the imageView. The fit cannot be used with resize.
Picasso.With().Load().Resize().Fit().Into()
Does the fit function will resize the image.
From the Document of Picasso
Attempt to resize the image to fit exactly into the target ImageView's
bounds. This will result in delayed execution of the request until the
ImageView has been laid out.
So i think the fit function will resize the image.