目录:
在app/build.gradle文件中添加以下依赖:
implementation 'com.github.bumptech.glide:glide:4.7.1'
annotationProcessor 'com.github.bumptech.glide:compiler:4.7.1'
如果你要访问的数据都存储在你的应用中,则不要求任何权限。
如果从Internet 上加载图片,则必须添加INTERNET权限到AndroidManifest.xml文件中,如下:
<uses-permission android:name="android.permission.INTERNET"/>
此外,还可以添加ACCESS_NETWORK_STATE权限,不是必需的,但是它将帮助 Glide 处理片状网络(flaky network)和飞行模式。
如果你正在从URL加载图片,Glide 可以自动帮助你处理片状网络连接:它可以监听用户的连接状态并在用户重新连接到网络时重启之前失败的请求。如果 Glide 检测到你的应用拥有ACCESS_NETWORK_STATE权限,Glide 将自动监听连接状态而不需要额外的改动。
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
从本地文件夹或DCIM或图库中加载图片,则需要添加READ_EXTERNAL_STORAGE权限,如下:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
如果要使用ExternalPreferredCacheDiskCacheFactory来将 Glide 的缓存存储到公有 SD 卡上,则还需要添加WRITE_EXTERNAL_STORAGE权限。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
布局文件activity_glide.xml,目前只有一个Button和ImageView。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="@+id/load_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="下载图片" />
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
GlideActivity.java
package com.example.androidtest.Glide;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.example.androidtest.R;
public class GlideActivity extends AppCompatActivity implements View.OnClickListener{
private Button mLoadBtn;
private ImageView mImageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_glide);
mLoadBtn=(Button)findViewById(R.id.load_btn);
mImageView=(ImageView)findViewById(R.id.img);
mLoadBtn.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v.getId()==R.id.load_btn){
String url="https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
Glide.with(this)
.load(url)
.into(mImageView);
}
}
}
注:这里我准备了一张图片并放在了github上,要下载下来需要添加INTERNET权限。
运行就可以看到以下效果:
主要代码如下:
String url="https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
RequestOptions options=new RequestOptions()
.placeholder(R.drawable.loading)
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(this)
.load(url)
.apply(options)
.into(mImageView);
注:
效果如下:
主要代码如下:
String url="https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
RequestOptions options=new RequestOptions()
.error(R.drawable.error)
.placeholder(R.drawable.loading)
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(this)
.load(url)
.apply(options)
.into(mImageView);
注:
把网络关掉,测试效果如下:
String url = "https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_test.gif";
Glide.with(this)
.asBitmap()
.load(url)
.into(mImageView);
注:
String url = "https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
RequestOptions options=new RequestOptions()
.error(R.drawable.error)
.placeholder(R.drawable.loading);
Glide.with(this)
.asGif()
.load(url)
.apply(options)
.into(mImageView);
注:
https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_test.gif
,就能成功加载出来了。主要代码如下:
String url="https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
RequestOptions options=new RequestOptions()
.override(100,100)
.error(R.drawable.error)
.placeholder(R.drawable.loading)
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(this)
.load(url)
.apply(options)
.into(mImageView);
注:
.override(Target.SIZE_ORIGINAL)
。Glide默认开启了。
禁用它的方法如下:
String url="https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
RequestOptions options=new RequestOptions()
.skipMemoryCache(true);
Glide.with(this)
.load(url)
.apply(options)
.into(mImageView);
注:
String url="https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
RequestOptions options=new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(this)
.load(url)
.apply(options)
.into(mImageView);
注:
into()方法可以让加载的图片显示到ImageView上。
自定义Target:
String url = "https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
SimpleTarget<Drawable> simpleTarget=new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable resource, @Nullable Transition<? super Drawable> transition) {
mImageView.setImageDrawable(resource);
}
};
Glide.with(this)
.load(url)
.into(simpleTarget);
注:
使用submit()方法来下载图片。
new Thread(new Runnable() {
@Override
public void run() {
try {
String url = "https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
final Context context=getApplicationContext();
FutureTarget<File> target=Glide.with(context).asFile().load(url).submit();
final File file=target.get();
runOnUiThread(new Runnable() {
@Override
public void run() {
Bitmap bitmap= BitmapFactory.decodeFile(file.getPath());
mImageView.setImageBitmap(bitmap);
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
注:
listener()方法可以结合into()和preload()方法使用。
String url = "https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
Glide.with(this)
.load(url)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
Toast.makeText(GlideActivity.this,e+"---onLoadFailed",Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Toast.makeText(GlideActivity.this,resource+"---onResourceReady",Toast.LENGTH_SHORT).show();
return false;
}
})
.into(mImageView);
注:
用preload()方法来预加载图片,等需要用的时候Glide就会直接从缓存中获取图片。
预加载图片:
String url = "https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
Glide.with(this)
.load(url)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
Toast.makeText(GlideActivity.this, "onLoadFailed", Toast.LENGTH_SHORT).show();
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
Toast.makeText(GlideActivity.this, "onResourceReady", Toast.LENGTH_SHORT).show();
return false;
}
})
.preload();
使用图片:
String url = "https://raw.githubusercontent.com/lcfu1/Image/master/Android/glide_logo.png";
Glide.with(this)
.load(url)
.into(mImageView);
注: