0
RecyclerView.Adapter
onBindViewHolder
現在看起來是這樣的:在Android的內存中緩存圖像有意義嗎?
@Override
public void onBindViewHolder(UserSettingHolder userSettingHolder, int i) {
// .. some code
try {
// TODO: Image loader need
File file = new File(MainActivity.context.getFilesDir(), _id + ".photo");
InputStream in = new BufferedInputStream(new FileInputStream(file));
byte[] buf = new byte[(int) file.length()];
int numRead = in.read(buf);
Bitmap bitmap = BitmapFactory.decodeByteArray(buf, 0, numRead);
userSettingHolder.icon.setImageBitmap(bitmap);
} catch (Exception e) {
System.out.print(e);
}
}
我想這是沒有效率的,從文件系統總是讀文件especiall不onBindViewHolder
這是相當頻繁調用。你會建議使用'內存緩存'來存儲圖像嗎?在iOS中爲此提供了NSCache
。 NSCache
有一個優勢,如果你內存不足,它將首先被清除。
Android中的替代技術是什麼?
LruCache ///// .... – Selvin
也是一些提示...不onBindViewHolder也不IO操作使用decodeByteArray ...使用另一個線程爲此...但...由於視圖回收很難實現良好(將image1加載到view1已啓動...視圖1已回收..將image2加載到view1已啓動...加載image2完成... image1的加載完成...顯示錯誤的圖像)...所以使用已有的圖像加載器庫之一...其中主要有內存緩存 – Selvin
畢加索,Glide,通用圖像加載程序。帶你挑。 –