本篇主要介绍四个图片加载框架,可以在Github上自行搜索,分别是glide、fresco、picasso和UIL,喜欢程度,由左到右
glide
https://github.com/bumptech/glide
今天运行了示例源码,真心不错
- build.gradle
1
2
3
4
5
6
7
8repositories {
mavenCentral()
}
dependencies {
compile 'com.github.bumptech.glide:glide:3.6.1'
compile 'com.android.support:support-v4:23.1.1'
}
- 代码
1
Glide.with(context).load(url).placeholder(R.mipmap.ic_launcher).into((ImageView) view);
fresco
https://github.com/facebook/fresco
http://fresco-cn.org/docs/image-requests.html#_
这个也很不错,很值得研究,但还是用Glide,它跟其他三个有点不一样,用了自定义的SimpleDraweeView
来作为容器显示
- build.gradle
1 | dependencies { |
- 代码
1
2
3
4
5
6
7
8
9
10public class MyApp extends Application {
public void onCreate() {
super.onCreate();
Fresco.initialize(this);
}
}
DraweeController draweeController = Fresco.newDraweeControllerBuilder().setUri(url).build();
((SimpleDraweeView) view).setController(draweeController);
picasso
https://github.com/square/picasso
http://square.github.io/picasso/
build.gradle
1
compile 'com.squareup.picasso:picasso:2.5.2'
代码
1
Picasso.with(context).load(url).placeholder(R.mipmap.ic_launcher).into((ImageView) view);
UIL
https://github.com/nostra13/Android-Universal-Image-Loader
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
initImageLoader(this);
}
public static void initImageLoader(Context context) {
ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
config.threadPriority(Thread.NORM_PRIORITY - 2);
config.denyCacheImageMultipleSizesInMemory();
config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
config.diskCacheSize(50 * 1024 * 1024); // 50 MiB
config.tasksProcessingOrder(QueueProcessingType.LIFO);
config.writeDebugLogs(); // Remove for release app
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config.build());
}
}
ImageLoader.getInstance().displayImage(url, (ImageView) view);
最后
不要忘记添加网络权限
1 | <uses-permission android:name="android.permission.INTERNET"/> |
本篇用途:
- 引导
- 快速查找