在如何使用LRUCache中, 土法煉鋼自己控制下載網路圖片(當然還有很多方法可以實現),
不過如果不是喜歡控制一些複雜的程序,
其實可以直接使用第三方做掉,
像Glide就是一個很好的工具。
下載一模一樣的圖
private String[] mImgsPath = {
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/01.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/02.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/03.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/04.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/05.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/06.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/07.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/08.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/09.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/10.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/11.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/12.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/13.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/14.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/15.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/16.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/17.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/18.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/19.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/20.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/21.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/22.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/23.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/24.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/25.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/26.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/27.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/28.jpg?raw=true",
"https://github.com/givemepassxd999/blog_files/blob/master/Android_AS/LRUCacheDemo/29.jpg?raw=true"};
我們只需要改變讀圖方式, 在getView加入
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final Holder holder;
if(null == v){
v = LayoutInflater.from(MainActivity.this).inflate(R.layout.list_item, null);
holder = new Holder();
holder.img = (ImageView) v.findViewById(R.id.img);
v.setTag(holder);
} else{
holder = (Holder) v.getTag();
}
holder.img.setImageResource(R.drawable.default_img);
Glide.with(MainActivity.this)
.load(mImgsPath[position])
.error(R.drawable.default_img)
.centerCrop()
.fitCenter()
.into(holder.img);
return v;
}
就可以完成讀圖了, 就是如此簡單。