apply plugin: 'kotlin-kapt'
dependencies {
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
kapt 'com.github.bumptech.glide:compiler:4.12.0'
}
//加载url
Glide.with(this)
.load(url)
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.into(imageView)
//加载本地drawable资源
Glide.with(this)
.load(R.mipmap.ic_launcher)
.into(imageView)
//加载gif,Glide会智能判断
Glide.with(this)
.load(R.drawable.a)
.into(imageView)
//asBitmap只加载静态图片,如果图片为gif则加载第一帧
Glide.with(this)
.asBitmap()
.load(R.drawable.a)
.into(imageView)
//后备回调符
Glide.with(this)
.load(url)
.fallback(R.mipmap.ic_launcher) //当url为null时显示
.into(imageView)
RequestOptions
Glide的配置都可以通过RequestOptions配置,用于提取公共属性
val options = RequestOptions()
.placeholder(R.drawable.placeholder)
.error(R.drawable.error)
.fallback(R.mipmap.ic_launcher)
Glide.with(this)
.load(url)
.apply(options)
.into(imageView)
Glide.with(this)
.load(url)
.override(100, 100)
.into(imageView)
缩略图可以从网络中加载,缩略图加载完后才会显示原图
Glide.with(this)
.load(url)
.thumbnail(0.2F) //只会显示原图20%
.into(imageView)
先加载缩略图url,再加载原图url
val thumbnailRequest = Glide.with(this).load(url2)
Glide.with(this)
.load(url1)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.thumbnail(thumbnailRequest)
.into(imageView2)
Glide默认开启内存缓存和硬盘缓存
//禁用内存缓存
Glide.with(this)
.load(url)
.skipMemoryCache(true)
.into(imageView)
Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(imageView)
Glide将图片加载入ImageView中
val target = object : CustomViewTarget<ImageView, Drawable>(imageView) {
override fun onResourceLoading(placeholder: Drawable?) {
super.onResourceLoading(placeholder)
Log.e(TAG, "开始加载")
}
override fun onLoadFailed(errorDrawable: Drawable?) {
Log.e(TAG, "加载失败")
(view as ImageView).setImageResource(R.drawable.error)
}
override fun onResourceReady(resource: Drawable, transition: Transition<in Drawable>?) {
Log.e(TAG, "加载成功")
(view as ImageView).setImageDrawable(resource)
}
override fun onResourceCleared(placeholder: Drawable?) {
Log.e(TAG, "资源清理")
}
}
Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.into(target)
Glide提供了预加载
Glide.with(this)
.load(url)
.preload()
可以用于文件下载
thread {
val target = Glide.with(applicationContext)
.asFile()
.load(url)
.submit()
val imageFile = target.get()
val file = File(cacheDir, "hello222.png")
imageFile.copyTo(file)
runOnUiThread {
Toast.makeText(applicationContext, "成功", Toast.LENGTH_SHORT).show()
}
}
监听Glide加载图片的状态
Glide.with(this)
.load(url)
.listener(object : RequestListener<Drawable> {
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
isFirstResource: Boolean
): Boolean {
Log.e(TAG, "加载失败")
return false
}
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
Log.e(TAG, "加载成功")
return false
}
})
.into(imageView)
Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.transition(withCrossFade(1000)) //默认为300ms
.into(imageView)
定义动画
设置动画
Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.transition(GenericTransitionOptions.with(R.anim.anim_alpha))
.into(imageView2)
Glide.with(this)
.load(url)
//.centerCrop() //居中剪裁
//.fitCenter() // 默认
.circleCrop() //圆形图片
.into(imageView)
class CircleCrop(context: Context) : BitmapTransformation() {
private val ID = "com.example.myapplication"
override fun updateDiskCacheKey(messageDigest: MessageDigest) {
messageDigest.update(ID.toByteArray())
}
override fun transform(
pool: BitmapPool,
toTransform: Bitmap,
outWidth: Int,
outHeight: Int
): Bitmap {
if (toTransform.width == outWidth && toTransform.height == outHeight) {
return toTransform
}
return Bitmap.createScaledBitmap(
toTransform,
10, 10, false
)
}
override fun equals(other: Any?): Boolean {
return other is CircleCrop
}
override fun hashCode(): Int {
return ID.hashCode()
}
}
Glide.with(this)
.load(url)
.transform(CircleCrop(this))
.into(imageView)
implementation 'jp.wasabeef:glide-transformations:4.3.0'
implementation 'jp.co.cyberagent.android:gpuimage:2.1.0'
Glide.with(this)
.load(url)
.apply(RequestOptions.bitmapTransform(BlurTransformation(25, 3)))
.into(imageView)
//模糊效果+底部圆角效果
val multi = MultiTransformation<Bitmap>(
BlurTransformation(25),
RoundedCornersTransformation(128, 0, RoundedCornersTransformation.CornerType.BOTTOM)
)
Glide.with(this)
.load(url)
.diskCacheStrategy(DiskCacheStrategy.NONE)
.apply(RequestOptions.bitmapTransform(multi))
.into(imageView)
网上出现了很多Glide的图片变换开源库,其中做的最出色的应该要数glide-transformations