1在泰國舉行的谷歌開發(fā)者論壇上,谷歌為我們介紹了一個(gè)名叫Glide的圖片加載庫,作者是bumptech。這個(gè)庫被廣泛的運(yùn)用在google的開源項(xiàng)目中,包括2014年google I/O大會(huì)上發(fā)布的官方app。現(xiàn)在這款框架非常的火,再次提供給大家使用流程。
1 導(dǎo)入 ?簡單粗暴
2 ?進(jìn)行封裝
packagecom.fufeng.util;
importandroid.content.Context;
importandroid.widget.ImageView;
importcom.bumptech.glide.Glide;
importcom.bumptech.glide.load.resource.drawable.GlideDrawable;
importcom.bumptech.glide.request.RequestListener;
importjp.wasabeef.glide.transformations.BlurTransformation;
/**
* Glide圖片加載工具類
* Created by limoran on 2017/3/14.
*/
public classGlideUtils {
/**
*默認(rèn)加載圖片
*@parammContext
*@parampath
*@paramimageView
*/
public static voidloadImageView(Context mContext, String path, ImageView imageView){
Glide.with(mContext).load(path).into(imageView);
}
/**
*帶默認(rèn)圖片的加載方式
*@parammContext
*@parampath
*@parammImageView
*@paramlodingImage
*@paramerrorImageView
*/
public static voidloadImageView(Context mContext, String path, ImageView mImageView,intlodingImage,interrorImageView) {
Glide.with(mContext).load(path).placeholder(lodingImage).error(errorImageView).into(mImageView);
}
/**
*加載指定大小的圖片
*@parammContext
*@parampath
*@paramwidth
*@paramheight
*@parammImageView
*/
public static voidloadImageView(Context mContext, String path,intwidth,intheight, ImageView mImageView) {
Glide.with(mContext).load(path).override(width, height).into(mImageView);
}
/**
*加載gif圖
*@parammContext
*@parampath
*@parammImageView
*/
public static voidloadImageViewGif(Context mContext, String path, ImageView mImageView) {
Glide.with(mContext).load(path).asGif().into(mImageView);
}
/**
*加載crop模式
*@parammContext
*@parampath
*@parammImageView
*/
public static voidloadImageViewCrop(Context mContext, String path, ImageView mImageView) {
Glide.with(mContext).load(path).centerCrop().into(mImageView);
}
/**
*圖片加載帶監(jiān)聽接口 方便監(jiān)聽異常來源
*@parammContext
*@parampath
*@parammImageView
*@paramrequstlistener
*/
public static voidloadImageView(Context mContext, String path, ImageView mImageView, RequestListener requstlistener) {
Glide.with(mContext).load(path).listener(requstlistener).into(mImageView);
}
/**
*清理圖片內(nèi)存緩存
*@parammContext
*/
public static voidGuideClearMemory(Context mContext) {
//清理內(nèi)存緩存? 可以在UI主線程中進(jìn)行
Glide.get(mContext).clearMemory();
}
/**
*模糊圖片
*@parammContext
*@parampath
*@parammImageView
*/
public static voidfuzzyLoadImageView(Context mContext,String path,ImageView mImageView){
Glide.with(mContext)
.load(path)
.bitmapTransform(newBlurTransformation(mContext,25,4))// “23”:設(shè)置模糊度(在0.0到25.0之間),默認(rèn)”25";"4":圖片縮放比例,默認(rèn)“1”。
.into(mImageView);
}
}
這個(gè)是個(gè)封裝好的實(shí)體類,以上十幾款比較常用的加載方式 如加載固定大小圖片,加載圓形圖片(他自己給你裁剪),加載成毛玻璃央視的,支持的很多,還有一些動(dòng)態(tài)圖,非常的多。具體需求可以去官方查看指定的文檔,加載圖片的時(shí)候只需要傳入指定的imageview的對(duì)象,mcontext,還有圖片路徑即可,非常簡單。還有如果是加載高清長圖也可以用Glide非常的簡單,不過需要你把圖片先要下載到本地再去進(jìn)行g(shù)lide加載,如下是加載高清長圖的過程,最后給大家推薦一個(gè)高清長圖控件? com.davemorrissey.labs:subsampling-scale-image-view:3.5.0,導(dǎo)入即可使用,具體請(qǐng)百度。
finalFile downDir = Environment.getExternalStorageDirectory();
//使用Glide下載圖片,保存到本地
Glide.with(this)
.load(imageUrl)
.asBitmap()
.into(newSimpleTarget() {
@Override
public voidonResourceReady(Bitmap resource, GlideAnimation glideAnimation) {
File file =newFile(downDir,"/Glide/");
if(!file.exists()) {
try{
file.createNewFile();
}catch(IOException e) {
e.printStackTrace();
}
}
FileOutputStream fout =null;
try{
//保存圖片
fout =newFileOutputStream(file);
resource.compress(Bitmap.CompressFormat.JPEG,100, fout);
//將保存的地址給SubsamplingScaleImageView,這里注意設(shè)置ImageViewState
scaleImageView.setImage(ImageSource.uri(file.getAbsolutePath()),newImageViewState(0.5F,newPointF(0,0),0));
}catch(FileNotFoundException e) {
e.printStackTrace();
}finally{
try{
if(fout !=null) fout.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
});