您好,欢迎来到二三四教育网。
搜索
您的当前位置:首页图片加载框架Glide使用教程

图片加载框架Glide使用教程

来源:二三四教育网

Gradle:

compile'com.github.bumptech.glide:glide:3.6.1'

Maven:

com.github.bumptech.glideglide3.6.1aar

Eclipse:


和Picasso一样,Glide也使用流式的接口。Glide 至少需要三个参数构造一个完整的图片加载请求:

with(Context context) - 上下文环境

load(String imageUrl) - 需要加载图片的URL.

into(ImageView targetImageView) - 图片显示的ImageView.

下面是最简单加载网络图片的用法:

ImageView targetImageView = (ImageView) findViewById(R.id.imageView);

Glide

.with(context)

.load(internetUrl)

.into(targetImageView);

从资源文件中加载:

int resourceId = R.mipmap.ic_launcher;

Glide

.with(context)

.load(resourceId)

.into(imageViewResource);

从文件中加载图片:

Filefile=newFile(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"Running.jpg");

Glide

.with(context)

.load(file)

.into(imageViewFile);

从URI中加载图片:

Uri uri = resourceIdToUri(context, R.mipmap.future_studio_launcher);

Glide

.with(context)

.load(uri)

.into(imageViewUri);

Glide

.with(context)

.load(UsageExampleListViewAdapter.eatFoodyImages[0])

.placeholder(R.mipmap.ic_launcher) //设置占位图

.error(R.mipmap.future_studio_launcher)

//设置错误图片

.crossFade() //设置淡入淡出效果,默认300ms,可以传参

//.dontAnimate() //不显示动画效果

.into(imageViewFade);

Glide 会根据ImageView的大小,自动限制图片缓存和内存中的大小,当然也可以通过调用override(horizontalSize, verticalSize)限制图片的大小:

Glide

.with(context)

.load(UsageExampleListViewAdapter.eatFoodyImages[0])

.placeholder(R.mipmap.ic_launcher) //设置占位图

.error(R.mipmap.future_studio_launcher)

//设置错误图片

.crossFade() //设置淡入淡出效果,默认300ms,可以传参

//.dontAnimate() //不显示动画效果

.into(imageViewFade);

当不知道ImageView的大小的时候,这个选项是非常有用的,我们可以设置需要加载的图片尺寸。

Glide支持两种图片缩放形式,CenterCrop 和 FitCenter

CenterCrop:等比例缩放图片,直到图片的狂高都大于等于ImageView的宽度,然后截取中间的显示。

Glide

.with(context)

.load(UsageExampleListViewAdapter.eatFoodyImages[0])

.override(600,200) // resizes

the image to these dimensions (inpixel)

.centerCrop() // this

cropping technique scales the image so that it fills the requested boundsandthen cropsthe extra.

.into(imageViewResizeCenterCrop);

FitCenter:等比例缩放图片,宽或者是高等于ImageView的宽或者是高。

Glide

.with(context)

.load(UsageExampleListViewAdapter.eatFoodyImages[0])

.override(600,200)

.fitCenter()

.into(imageViewResizeFitCenter);

Fresco支持加载GIF,并且使用的方式和加载图片一样:

Glide

.with( context )

.load( gifUrl )

.asGif()

.error( R.drawable.full_cake )

.into(

imageViewGif );

Glide可以加载视频的缩略图:

StringfilePath ="/storage/emulated/0/Pictures/example_video.mp4";

Glide

.with(context)

.load( Uri.fromFile(newFile( filePath )) )

.into(imageViewGifAsBitmap );

Glide默认开启磁盘缓存和内存缓存,当然也可以对单张图片进行设置特定的缓存策略。

设置图片不加入到内存缓存

Glide

.with( context )

.load( eatFoodyImages[0] )

.skipMemoryCache(true)

.into( imageViewInternet );

设置图片不加入到磁盘缓存

Glide

.with( context )

.load(

eatFoodyImages[0] )

.diskCacheStrategy(

DiskCacheStrategy.NONE)

.into(

imageViewInternet );


Glide支持多种磁盘缓存策略:

DiskCacheStrategy.NONE :不缓存图片

DiskCacheStrategy.SOURCE :缓存图片源文件

DiskCacheStrategy.RESULT:缓存修改过的图片

DiskCacheStrategy.ALL:缓存所有的图片,默认


Glide支持为图片加载设置优先级,优先级高的先加载,优先级低的后加载:

private void loadImageWithHighPriority() {

Glide

.with( context )

.load(

UsageExampleListViewAdapter.eatFoodyImages[0] )

.priority( Priority.HIGH)

.into(

imageViewHero );

}

private void loadImagesWithLowPriority() {

Glide

.with( context )

.load(

UsageExampleListViewAdapter.eatFoodyImages[1] )

.priority( Priority.LOW)

.into(

imageViewLowPrioLeft );

Glide

.with( context )

.load(

UsageExampleListViewAdapter.eatFoodyImages[2] )

.priority( Priority.LOW)

.into(

imageViewLowPrioRight );

}

Glide通过Target的回调获取Bitmap,最常用的是SimpleTarget:

privateSimpleTarget

target =newSimpleTarget() {

@Override

publicvoidonResourceReady(Bitmapbitmap, GlideAnimation glideAnimation) {

// do something with the bitmap

// for demonstration purposes, let's

just set it to an ImageView

imageView1.setImageBitmap( bitmap );

}

};

privatevoidloadImageSimpleTarget() {

Glide

.with( context )// could be an issue!

.load(eatFoodyImages[0] )

.asBitmap()

.into( target );

}

-

设置Bitmap的大小:

privateSimpleTarget

target2 =newSimpleTarget(250,250) {

@Override

publicvoidonResourceReady(Bitmapbitmap, GlideAnimation glideAnimation) {

imageView2.setImageBitmap( bitmap );

}

};

privatevoidloadImageSimpleTargetApplicationContext() {

Glide

.with(context.getApplicationContext() )// safer!

.load(eatFoodyImages[1] )

.asBitmap()

.into( target2 );

}

Copyright © 2019- how234.cn 版权所有 赣ICP备2023008801号-2

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务