今天主要研究了Glide获取图片Path、Bitmap用法,相信也困扰了大家很久,我在网上也找了很久,基本没有,后来研究了下,也参考了下api文档,总结了以下几个方式:
- 获取Bitmap:
1)在图片下载缓存好之后获取
Glide.with(mContext).load(url).asBitmap().into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
image.setImageBitmap(resource);
}
}); //方法中设置<span style="font-family: Arial, Helvetica, sans-serif;">asBitmap可以设置回调类型</span>
上面是简单方法,下面有全面的方法,可以完美控制:
Glide.with(mContext).load(url).asBitmap().into(new Target<Bitmap>() {
@Override
public void onLoadStarted(Drawable placeholder) {
}
@Override
public void onLoadFailed(Exception e, Drawable errorDrawable) {
}
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
//TODO set bitmap
}
@Override
public void onLoadCleared(Drawable placeholder) {
}
@Override
public void getSize(SizeReadyCallback cb) {
}
@Override
public void setRequest(Request request) {
}
@Override
public Request getRequest() {
return null;
}
@Override
public void onStart() {
}
@Override
public void onStop() {
}
@Override
public void onDestroy() {
}
});
2)通过url获取
Bitmap myBitmap = Glide.with(applicationContext)
.load(yourUrl)
.asBitmap() //必须
.centerCrop()
.into(500, 500)
.get()
- 获取图片缓存路径
FutureTarget<File> future = Glide.with(mContext)
.load("url")
.downloadOnly(500, 500);
try {
File cacheFile = future.get();
String path = cacheFile.getAbsolutePath();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}