我有以下自定义转换(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
中的说明进行操作。
Glide.with(icon)
.load(url)
.apply(RequestOptions()
.transform(IconTransformation())
.error(errorDrawableRes))
.into(icon)
答案 0 :(得分:0)
问题出在pool.put(img)
行中,以下问题解决了这个问题:
if (img != toTransform) {
pool.put(img)
}
答案 1 :(得分:0)
我的评论here可能会有帮助。
我使用占位符解决闪烁问题。 dontTransform确实 在我的应用中不起作用。在搜索Glide的源代码之后,我 发现问题可能来自ImageViewTarget:
/** * 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中并进行设置 作为下一次加载的占位符。这是代码
// 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);
只需解决。希望对您有所帮助。