需求:banner中使用Glide加载图片,error/loading placeholder使用一个不撑满ImageView的图片资源,success的时候加载的网络资源需要填充满ImageView
思路1:
Glide.with(imageView.context)
.load(data?.img)
.placeholder(R.drawable.coin_account_roll_banner_cus_bitmap)
.error(R.drawable.coin_account_roll_banner_cus_bitmap)
.listener(object : RequestListener<Drawable>{
override fun onLoadFailed(
e: GlideException?,
model: Any?,
target: Target<Drawable>?,
isFirstResource: Boolean
): Boolean {
imageView.scaleType = ImageView.ScaleType.CENTER_CROP
return false
}
override fun onResourceReady(
resource: Drawable?,
model: Any?,
target: Target<Drawable>?,
dataSource: DataSource?,
isFirstResource: Boolean
): Boolean {
imageView.scaleType = ImageView.ScaleType.FIT_XY
return false
}
})
.into(imageView)
问题就是这有点麻烦吧,一堆回调处理,咱们来想想思路2
思路2:
就使用Glide提供的placeholder & error
Glide.with(imageView.context)
.load(data?.img)
.placeholder(R.drawable.custom_bitmap)
.error(R.drawable.custom_bitmap)
.into(imageView)
但是UI同学给到的图片是如上图所示的小图,so我们可以自定义一个bitmap.drawable文件
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/your_placeholder"
android:gravity="center"
android:antialias="true">
</bitmap>
好了,之前也是没想到这方法,主要平时自定义drawable都是用来画背景画图的,还没用用过bitmap这个标签