I am using Glide for displaying images in a listview.
How can I use LruBitmapPool to reduce GC (Garbage Collection)?
There is no sample around.
Glide Page :
https://github.com/bumptech/glide
Glide uses the LruBitmapPool automatically so you don't normally need to worry about it. It looks like you posted an issue on Glide's Github page, so let's follow up there: https://github.com/bumptech/glide/issues/326.
Related
I'm new to Kotlin and Java and don't know how to scale an image in an image view.
I want something like this: MyImageView.setHeight(300)
I don't want to distort the image.
Thanks in advance!
This solution might be overkill for your current question, but if you have to show images in your app, I recommend using an image handling library such as Picasso or Glide.
If you are to use Picasso, add it to your dependencies by adding the following line to your gradle files :
dependencies {
implementation 'com.squareup.picasso:picasso:2.71828'
}
Then in your Kotlin file, when you are about to load the picture onto an ImageView,
do it using Picasso using the following lines :
Picasso
.get()
.load(imageUrl)
.fit()
.centerInside()
.into(imageView)
Yes, I agree with rgv. Use an image handling library, preferably Picasso. It can reduce a lot of hardwork and also, the result is better than what you would have got by scaling image manually.
I have a decent android app which is using two fragments one of them is Listfragment. And my question is, can I use Asynctask to download the image and compress it to a thumbnail and then assign it to an imageview ? (Imagine there are like 100 items in ListView). Or is this even possible to call the asynctask each time ?
You don't need to reinvent the wheel. There are libraries which already can download, cache and resize image for you. For instance Picasso:
Picasso
.with(context)
.load(url)
.resizeDimen(width, height)
.centerCrop()
.error(R.drawable.error_img);
.into(placeholder);
For this task, I would strongly recommend the use of an external library.
There are many to choose from that can be used to download, display and cache images from an online source. As many of these libraries have been developed for many years, they are usually going to be better than any bespoke code you write.
I am referring to this post Getting Picasso to pre-fetch forthcoming images where #BillMote uses fetch() with centerCrop() and further options:
Picasso.with(getApplicationContext())
.load(url)
.resizeDimen(R.dimen.article_image_preview_width, R.dimen.article_image_preview_height)
.centerCrop()
.fetch();
Will Picasso honor such options at all, or will Picasso save the image straight to the cache so these options are ignored and should be used later when the image is pulled from the cache and viewed?
It still isn't clear to me (and after 6 months I haven't found any documentation where this was clearly defined [I admit I haven't investigated into this either]) if Picasso applies any such options before saving the file to the cache, or repeatedly every time it displays an image.
Something like centerCrop() makes no sense to me in above statement, because the size of the target ImageView isn't clear at this point. This might be different for resizeDimen(), but I stil doubt Picasso will apply the dimensions because JakeWharton once said in a post that Picasso relies on okhttp for image caching.
Can anybody shed light please.
I have an image that I want to download from online.
http://luxproperty.kaytami.com/platform/media/image/jpeg/2014/12/24 Repulse Bay Road.jpg
I have replaced the space with %20, so it becomes
http://luxproperty.kaytami.com/platform/media/image/jpeg/2014/12/24%20Repulse%20Bay%20Road.jpg
The image is not large and so I assume Picasso should be able to load it.
To fit my imageview, I have fit() the image, and the code is as follow:
Picasso.with(mContext).load(UrlEncoder.encode(district.getImage_urls().get(0))).fit().centerCrop().into(holder.image);
However, the image does not appear.
There is a list of 4 items, each containing an image that I load from online. three of them were loaded properly, and the remaining one (http://luxproperty.kaytami.com/platform/media/image/jpeg/2014/12/24%20Repulse%20Bay%20Road.jpg) just does not show up.
Any idea?
I am using Picasso 2.4.0, okhttp-2.1.0 have a look this
It's a bug that is reported to be fixed in the next release of the lib.
You can clone the repo of the lib and compile your own jar or wait.
I recommend you to take a look at Glide. Migrating from Picasso is quite trivial, it has better performance and it makes a nice smooth scrolling on lists.
I had the same problemm when i tried to use Picasso to load images but i used this Library
Its the simplest thing so far by me.
Compile:
compile 'com.koushikdutta.ion:ion:2.+'
Then:
//for activity
ImageView myImage = (ImageView)findViewById(R.id.my_image);
//for fragment
ImageView myImage = (ImageView)rootView.findViewById(R.id.my_image);
Ion.with(myImage)
.placeholder(R.drawable.placeholder_image)
.error(R.drawable.error_image)
.load("http://example.com/image.png");
Hope it helps!!!
I want to load the Async image loading for grid view. The image loading should be Async and should only when grid view scrolled. And when scroll up the image should be loaded from cache. Simply i need a loading style just like fb,pinterest.
Use Jake Wharton's Picasso Library. (A Perfect ImageLoading Library form the developer of ActionBarSherlock)
A powerful image downloading and caching library for Android.
Images add much-needed context and visual flair to Android applications. Picasso allows for hassle-free image loading in your application—often in one line of code!
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
Many common pitfalls of image loading on Android are handled automatically by Picasso:
Handling ImageView recycling and download cancelation in an adapter. Complex image transformations with minimal memory use. Automatic memory and disk caching.
Picasso Jake Wharton's Library
Your exact requirement in serviced by Android Universal Image Loader Library. Check out the github repository here
There is a sample code given here which shows the working of the loader for a grid layout.
Hopefully this will serve your need.