最近换新工作,新公司的项目催得非常紧,天天加班,导致很久没更新博客了,现在才刚空一点。
今天来讲下跟Square的网络图片加载库Picasso同样强大的图片加载库Glide的简单使用。
至于其中的区别这篇文章有详细描述:
http://jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0327/2650.html
首先是导入远程库:
compile 'com.github.bumptech.glide:glide:3.7.0'
然后在AndroidManifest.xml中添加用户权限,因为接下来会使用到这2个权限:
android:name="android.permission.READ_EXTERNAL_STORAGE" />
android:name="android.permission.INTERNET" />
然后是布局,一个ImageView即可:
xml version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
android:id="@+id/imageview_activity_main"
android:layout_width="100dp"
android:layout_height="100dp" />
最后是代码中使用,这里就要讲到Glide比Picasso强大的地方了,那就是它可以加载网络Gif图片!
同样的,它也可以像Picasso一样从资源中、从文件中、从Uri中加载,至于加载网络图片,那就更不用提了。
首先是加载资源中的图片:
ImageView iv = (ImageView) findViewById(R.id.imageview_activity_main);
Glide.with(context).load(R.mipmap.ic_launcher).into(iv);
使用就是这么简单,传入Context,资源ID和ImageView的实例即可。
然后是加载文件中的图片:
private String path = "/androidesk/wallpapers/57484f7869401b103938f52a.jpg";
File file = new File(Environment.getExternalStorageDirectory().getPath() + path);
Glide.with(context).load(file).into(iv);
加载Uri中的图片:
private int resId = R.mipmap.ic_launcher;
Uri uri = Uri.parse(RESOURCE + getPackageName() + SLASH + resId);
Glide.with(context).load(uri).into(iv);
加载网络图片:
private String url_img = "http://p4.so.qhimg.com/t010c102c7b029340d4.jpg";
Glide.with(context).load(url_img).into(iv);
最后是加载网络Gif图片:
private String url_gif = "http://pic.uuhy.com/uploads/2011/02/11/005.gif";
Glide.with(context).load(url_gif).into(iv);
要看动图大家可以下载Demo看看。
当然Glide还有许多其他用法和更强大的地方,这里不作深入。
希望对大家有所帮助。
Demo地址:
http://download.csdn.net/detail/qq_23940659/9534440