[Android] Dynamically Loading Recycler View Images in Android

之前都是使用 universal image loader,但最近發現效能在 Recycler View 上其差無比,隔了 5秒圖片才出來,改服用 glide 之後,居然一秒內都顯示出來 @[email protected]

glide 網址:
https://github.com/bumptech/glide


You should check the universal image loader. It has memory cache, disk cache and it loads your images asynchronously so doesn’t block the ui. You can set default image and/or failed to fetch image etc. It can sample down your image to decrease the memory footprint of the bitmap. I really recommend you to use it for images.

Do not disable recyclable for your case because it is pointless. Images must be recycled because their bitmap drawables generate very high memory overload if not properly sampled.

Sample usage in RecyclerViewAdapter:

@Override
public void onBindViewHolder(CustomViewHolder viewHolder, int position) {
    String imageUri = "";//local or remote image uri address
    //viewHolder.imgView: reference to your imageview
    //before you call the displayImage you have to 
    //initialize imageloader in anywhere in your code for once.   
    //(Generally done in the Application class extender.)
    ImageLoader.getInstance().displayImage(imageUri, viewHolder.imgView);
}

edit: Nowadays, I consider Glide as my main image loading and caching library. You can use it like this:

Glide.with(context)
    .load(imageUri)
    .placeholder(R.drawable.myplaceholder)
    .into(imageView);

相關文章

寫留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *