【问题标题】:How can I avoid image flickering with Glide如何使用 Glide 避免图像闪烁
【发布时间】:2020-09-12 03:18:17
【问题描述】:

我正在我的应用程序上进行类似 youtube/netflix 的预览。

为此,我得到了sprite of thumbnail(一张大图,由许多对应于时间范围的小图组成)。使用GlideCustom Transformation,我设法隔离了这些时间范围并将它们显示在我的progressbar 上方的框架中。

即使我在第一次显示拇指时预先加载并创建了拇指,图像之间的过渡也会使它们闪烁。

我创建了一个 imgur 来向您展示行为:Example in video

我一直在看stackoverflow,发现this postGlide issues发现是由于:

闪烁是由于您将图像连续快速加载到同一视图中引起的。

我不知道如何改进当前的行为,所有的拇指都是在 exoplayer 开始播放之前创建的,而 Glide 只需要显示它们。

这是我的代码:

自定义转换

private const val MAX_LINES = 7
private const val MAX_COLUMNS = 7
private const val THUMBNAILS_EACH = 5000 // milliseconds

class GlideThumbnailTransformationFull(position: Long) : BitmapTransformation() {

    private val x: Int
    private val y: Int

    init {
        val square = position.toInt() / THUMBNAILS_EACH
        y = square / MAX_COLUMNS
        x = square % MAX_COLUMNS
    }

    override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap {
        val width = toTransform.width / MAX_COLUMNS
        val height = toTransform.height / MAX_LINES

        return Bitmap.createBitmap(toTransform, x * width, y * height, width, height)
    }

    override fun updateDiskCacheKey(messageDigest: MessageDigest) {
        val data: ByteArray = ByteBuffer.allocate(8).putInt(x).putInt(y).array()
        messageDigest.update(data)
    }

    override fun hashCode(): Int {
        return (x.toString() + y.toString()).hashCode()
    }

    override fun equals(other: Any?): Boolean {
        if (other !is GlideThumbnailTransformationFull) {
            return false
        }
        return other.x == x && other.y == y
    }
}

加载我使用的图片

GlideApp.with(imageView)
            .load(thumbnailsUrl)
            .dontAnimate()
            .skipMemoryCache(false)
            .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
            .transform(GlideThumbnailTransformationFull(currentPosition))
            .into(imageView)

【问题讨论】:

    标签: android caching android-glide


    【解决方案1】:

    https://futurestud.io/tutorials/glide-caching-basics 看看这个。这给出了一个关于缓存和滑翔中其他东西的详细想法

    Glide.with(imageView)
        .load(url)
        .transition(withCrossFade(factory))
        .apply(RequestOptions().placeholder(placeholder))
        .apply(RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL))
        .into(imageView)
    

    【讨论】:

    • 我已经阅读了这篇文章和 this one 以及 glide documentation,我看不到任何地方可以强制将图像放在内存缓存中
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-01
    • 2023-04-02
    • 1970-01-01
    • 2013-05-02
    • 2012-10-28
    • 2016-06-30
    相关资源
    最近更新 更多