问题描述
我有以下自定义转换(kotlin):
I have the following custom transformation (kotlin):
private class IconTransformation : BitmapTransformation() {
companion object {
private const val ID = "com.example.widget.IconView\$IconTransformation"
private val ID_BYTES = ID.toByteArray()
private const val PAINT_FLAGS = Paint.DITHER_FLAG or Paint.FILTER_BITMAP_FLAG
private const val CIRCLE_CROP_PAINT_FLAGS = PAINT_FLAGS or Paint.ANTI_ALIAS_FLAG
private val CIRCLE_CROP_SHAPE_PAINT = Paint(CIRCLE_CROP_PAINT_FLAGS)
private val CIRCLE_CROP_BITMAP_PAINT = Paint(CIRCLE_CROP_PAINT_FLAGS).apply {
xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
}
private val CLEAR_PAINT = Paint().apply {
xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR)
}
}
override fun updateDiskCacheKey(messageDigest: MessageDigest) {
messageDigest.update(ID_BYTES)
}
override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int,
outHeight: Int): Bitmap {
val img = TransformationUtils.fitCenter(pool, toTransform, outWidth, outHeight)
val result = pool.get(outWidth, outHeight, Bitmap.Config.ARGB_8888)
val left = (result.width - img.width) / 2f
val top = (result.height - img.height) / 2f
result.setHasAlpha(true)
Canvas(result).apply {
drawColor(Color.TRANSPARENT)
drawCircle(outWidth / 2f, outHeight / 2f,
Math.min(outWidth, outHeight) / 2f, CIRCLE_CROP_SHAPE_PAINT)
drawBitmap(img, left, top, CIRCLE_CROP_BITMAP_PAINT)
if (left > 0f) {
drawRect(0f, 0f, left, outHeight.toFloat(), CLEAR_PAINT)
drawRect(left + img.width - 1, 0f,
outWidth.toFloat(), outHeight.toFloat(), CLEAR_PAINT)
}
if (top > 0f) {
drawRect(0f, 0f, outWidth.toFloat(), top, CLEAR_PAINT)
drawRect(0f, top + img.height - 1,
outWidth.toFloat(), outHeight.toFloat(), CLEAR_PAINT)
}
}
pool.put(img)
return result
}
override fun hashCode(): Int = ID.hashCode()
override fun equals(other: Any?): Boolean = other is IconTransformation
}
当我像下面在回收站视图中那样使用它时,图像会闪烁并弹跳.从我阅读的所有内容来看,这与错误地实现updateDiskCacheKey
,hashCode
和equals
有关.在AFAIK中,我完全按照BitmapTransformation
中的说明进行操作.
When I use it like below inside a recycler view, the images flicker and bounce around. From everything I read it has to do with improperly implementing updateDiskCacheKey
, hashCode
, and equals
. From AFAIK I followed the instructions in BitmapTransformation
exactly.
Glide.with(icon)
.load(url)
.apply(RequestOptions()
.transform(IconTransformation())
.error(errorDrawableRes))
.into(icon)
推荐答案
我的评论这里可能会有帮助.
My comment here might help.
/**
* Sets the given {@link android.graphics.drawable.Drawable} on the view using {@link
* android.widget.ImageView#setImageDrawable(android.graphics.drawable.Drawable)}.
*
* @param placeholder {@inheritDoc}
*/
@Override
public void onLoadStarted(@Nullable Drawable placeholder) {
super.onLoadStarted(placeholder);
setResourceInternal(null);
setDrawable(placeholder);
}
因此,如果占位符为null,ImageView将在紧随其后变为空白 每次加载.所以我将最后一张图片缓存在ImageView中并进行设置 作为下一次加载的占位符.这是代码
Thus if placeholder is null, ImageView will turn blank right after every loading. So I cache the last image in the ImageView and set it as placeholder for next loading. Here is the code
// init
mGlideOpt = new RequestOptions().dontAnimate().skipMemoryCache(false);
mGlideRB = Glide.with(imageView).asBitmap();
// loading
Drawable drawable = mImageView.getDrawable();
if (drawable != null) {
mGlideOpt = mGlideOpt.placeholder(drawable);
}
mGlideRB.apply(mGlideOpt).load(mCacheFile).into(mImageView);
只需解决.希望对您有所帮助.
Just a work around. Hope it helps.
这篇关于自定义转换时滑行4.6.1图像闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!