代码之家  ›  专栏  ›  技术社区  ›  Biscuit Samir

Android GlideTransformation垃圾收集器问题

  •  0
  • Biscuit Samir  · 技术社区  · 5 年前

    我对我的 Glide Transformation garbage collected 太频繁了。我在连续创建和显示位图 GlideTransformation 负责使用 BitmapPool link

    Profiler

    滑动变换

    class GlideThumbnailTransformation(square: Int) : BitmapTransformation() {
    
        private val x: Int = square % MAX_COLUMNS
        private val y: Int = 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 GlideThumbnailTransformation) {
                return false
            }
            return other.x == x && other.y == y
        }
    }
    

    我如何使用它

    imageView

    GlideApp.with(imageView).asBitmap()
        .apply(glideOptions)
        .load(thumbnailsUrl)
        .override(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
        .apply(RequestOptions.bitmapTransform(GlideThumbnailTransformation(square)))
        .into(imageView)
    
    0 回复  |  直到 5 年前