這里只整理使用方法驻债,方便自己使用,想看詳細的解析請轉(zhuǎn)閱
作者:MrTrying
鏈接:http://www.reibang.com/p/7ce7b02988a4
來源:簡書
作者:Jiun俊
鏈接:http://www.reibang.com/p/e8c204a29623
來源:簡書
非常感謝作者的分享惕鼓!
一.基本方法
String url = "http://img1.dzwww.com:8080/tupian_pl/20150813/16/7858995348613407436.jpg";
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Glide.with(context)
.load(url)
.into(imageView);
說明:
1.圖片的加載會和Activity/Fragment的生命周期保持一致
二. 占位圖設置
String url = "http://img1.dzwww.com:8080/tupian_pl/20150813/16/7858995348613407436.jpg";
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Glide.with(context)
.load(url)
.placeholder(R.drawable.place_image)//圖片加載出來前荡碾,顯示的圖片
.error(R.drawable.error_image)//圖片加載失敗后相恃,顯示的圖片
.into(imageView);
三. 縮略圖
Glide.with( context )
.load( url )
.thumbnail( 0.2f )//參數(shù)是 float 類型,作為其倍數(shù)大小
.into( imageView );
說明:1.簡單粗暴,但是如果需要通過網(wǎng)絡加載相同的全尺寸圖片奸披,就不會很快顯示收厨。
private void loadImageThumbnailRequest(){
// setup Glide request without the into() method
DrawableRequestBuilder<String> thumbnailRequest = Glide.with( context ).load( url );
// pass the request as a a parameter to the thumbnail request
Glide.with( context )
.load( url )
.thumbnail( thumbnailRequest )
.into( imageView );
}
說明:1.縮略圖可以是不同的資源圖片驹暑,也可以對縮略圖做不同的轉(zhuǎn)換揪垄。
四. 動畫
Glide.with(context)
.load(url)
.crossFade()//crossFade(int duration)設置動畫時間逊移,單位ms,默認300ms
//.dontAnimate()//設置關閉動畫效果
.placeholder(R.drawable.place_image)
.error(R.drawable.error_image)
.into(imageView);
//自定義動畫
Glide.with(context)
.load(mUrl)
.transform(new RoundTransformation(this , 20))
.animate( R.anim.zoom_in )//自定義動畫的資源文件
.into(mImageView);
//使用的Target是自定義情況鳞绕,需要用到ViewPropertyAnimation.Animator()
ViewPropertyAnimation.Animator animator = new ViewPropertyAnimation.Animator() {
@Override
public void animate(View view) {
view.setAlpha( 0f );
ObjectAnimator fadeAnim = ObjectAnimator.ofFloat( view, "alpha", 0f, 1f );
fadeAnim.setDuration( 2500 );
fadeAnim.start();
}
};
Glide.with(context)
.load(mUrl)
.animate( animator )
.into(viewTarget);
五. 圖片大小與裁剪
Glide.with(context)
.load(url)
.override(width,height)//這里的單位是px
.into(imageView);
說明:1.Glide提供兩個方法設置圖片顯示方式:CenterCrop() 和 FitCenter()失仁,作用與imageView的屬性一樣。
六. 圖片的緩存處理
1.內(nèi)存緩存
Glide.with(context)
.load(url)
.skipMemoryCache(true)//關閉內(nèi)存緩存猾昆,默認是開啟的
.into(imageView);
2.磁盤緩存
Glide.with(context)
.load(url)
.diskCacheStrategy( DiskCacheStrategy.NONE )//設置磁盤緩存的方式
.into(imageView);
說明:
1.Glide 不僅緩存了全尺寸的圖陶因,還會根據(jù) ImageView 大小所生成的圖也會緩存起來骡苞。
DiskCacheStrategy 的枚舉意義:
DiskCacheStrategy.NONE 什么都不緩存
DiskCacheStrategy.SOURCE 只緩存全尺寸圖
DiskCacheStrategy.RESULT 只緩存最終的加載圖
DiskCacheStrategy.ALL 緩存所有版本圖(默認行為)
七. 圖片請求的優(yōu)先級
//設置 HIGH 優(yōu)先級
Glide.with( context )
.load( highPriorityImageUrl )
.priority (Priority.HIGH )//Priority.LOW垂蜗、Priority.NORMAL、Priority.HIGH解幽、Priority.IMMEDIAT四種優(yōu)先級
.into( imageView );
說明:1.優(yōu)先級并不是完全嚴格遵守的贴见。
八. 顯示GIf和Video
//如果圖片類型不是gif會當做load失敗來處理
Glide.with( context )
.load( gifUrl )
.asGif()
.error( R.drawable.error )
.into( imageView );
//只會顯示靜態(tài)的圖片,如果是gif躲株,會顯示第一幀
Glide.with( context )
.load( gifUrl )
.asBitmap()
.error( R.drawable.error )
.into( imageView );
//加載視頻片部,但只能加載本地視頻
String filePath = "/storrage/emulated/0/Pictures/video.mp4";
Glide.with( context )
.load( Uri.fromFile( new File( filePath ) ) )
.into( imageView );
九. Target
1.SimpleTarget
private SimpleTarget<Bitmap> mSimpleTarget = new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> animation) {
mImageView.setImageBitmap(resource);
}
};
private void loadImageSimpleTarget() {
//使用context.getApplicationContext(),這樣只有在應用完全停止時 Glide 才會殺死這個圖片請求
Glide.with(mContext.getApplicationContext())
.load( mUrl )
.asBitmap()
.into( mSimpleTarget );
}
//還可以指定圖片的尺寸
private SimpleTarget<Bitmap> mSimpleTarget = new SimpleTarget<Bitmap>(500,500) {
@Override
public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> animation) {
mImageView.setImageBitmap(resource);
}
};
說明:1.這種方法可以獲取加載完的Bigmap
2.ViewTarget
public void loadImageTarget(Context context){
//自定義View,無法使用into霜定,就可以使用ViewTarget
CustomView mCustomView = (CustomView) findViewById(R.id.custom_view);
ViewTarget viewTarget = new ViewTarget<CustomView,GlideDrawable>( mCustomView ) {
@Override
public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
//自定義View加載圖片的方法
this.view.setImage(resource);
}
};
Glide.with(context)
.load(mUrl)
.into(viewTarget);
}
十. Transformations
Glide.with(context)
.load(mUrl)
//傳自定義的Transformation,多個用逗號隔開
.transform(new RoundTransformation(context , 20) 档悠, new RotateTransformation(context , 90f))
.into(mImageView);
//設置圓角
Glide.with(mContext)
.load(mUrl)
.transforms(new CenterCrop(), new RoundedCorners(mContext.getResources().getDimensionPixelOffset(R
.dimen.normal_10dp))))
.into(mImageView);
說明:1.https://github.com/wasabeef/glide-transformations
glide-transformations 這個庫有兩個不同的版本,擴展版本包含了更多的 Transformation
十一. Modules
舉例望浩,增加Glide的圖片質(zhì)量
public class QualityModule implements GlideModule{
@Override
public void applyOptions(Context context , GlideBuilder builder){
builder.setDecodeFormat(DecodeFormat.PREFER_ARGB_8888);
}
@Override
public void registerComponents(Context context , Glide glide){
// nothing to do here
}
}
需要在AndroidManifest.xml配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mrtrying.demoglide">
<application>
<meta-data
android:name="com.mrtrying.demoglide.module.ExampleModule"
android:value="GlideModule" />
...
</application>
...
</manifest>
十二. Glide 4
1.Glide4語法上做了一些修改辖所,引入了RequestOptions對象。
RequestOptions options
= new RequestOptions()
.transforms()
//.circleCrop()//設置圖片顯示為圓形
.placeholder(R.drawable.place_holder)
.error(R.drawable.error)
// 可以指定加載圖片的大小磨德,不讓 Glide 根據(jù)控件大小來決定圖片大小
.override(200,200)
// 加載原圖缘回,Glide 不會自動壓縮吆视,容易 OOM
.override(Target.SIZE_ORIGINAL)
// 緩存策略默認開啟,禁用方法
.skipMemoryCache(true)
// 禁用掉 Glide 的緩存功能
.diskCacheStrategy(DiskCacheStrategy.NONE);
Glide.with(this)
.asXxx() // asGif / asFile / asBitmap / asDrawable
.load(url)
.apply(options)
.listener()
// .preload//提前對圖片進行一個預加載
// .submit()//用于下載圖片酥宴,不能預加載啦吧。關于圖片緩存的路徑、訪問獲取緩存文件的方法
.into(ImageView / Target);
//監(jiān)聽 Glide 加載圖片的狀態(tài)
Glide.with(this)
.load("http://www.guolin.tech/book.png")
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
return false;
}
})
.into(imageView);