Fix image transition animation.

This commit is contained in:
Greyson Parrelli
2026-04-09 22:02:46 -04:00
parent 1844b128e1
commit 001896d244

View File

@@ -1,8 +1,9 @@
package org.thoughtcrime.securesms.mediapreview
import android.graphics.Canvas
import android.graphics.ColorFilter
import android.graphics.PixelFormat
import android.graphics.drawable.Drawable
import android.graphics.drawable.DrawableWrapper
/**
* A wrapper that skips drawing upon failure. This is to guard against situations where we may
@@ -12,12 +13,43 @@ import android.graphics.drawable.DrawableWrapper
* (like transition animations) where having a bitmap isn't strictly necessary, and we'd rather
* show nothing than crash or have to manage the bitmap lifecycle ourselves.
*/
class RecycledBitmapGuardDrawable(drawable: Drawable) : DrawableWrapper(drawable) {
class RecycledBitmapGuardDrawable(private val inner: Drawable) : Drawable() {
init {
val b = inner.bounds
setBounds(b.left, b.top, b.right, b.bottom)
}
override fun draw(canvas: Canvas) {
val savedBounds = inner.copyBounds()
inner.setBounds(bounds.left, bounds.top, bounds.right, bounds.bottom)
try {
super.draw(canvas)
inner.draw(canvas)
} catch (_: RuntimeException) {
// Bitmap was recycled — nothing to draw.
} finally {
inner.setBounds(savedBounds.left, savedBounds.top, savedBounds.right, savedBounds.bottom)
}
}
override fun getIntrinsicWidth(): Int {
return inner.intrinsicWidth
}
override fun getIntrinsicHeight(): Int {
return inner.intrinsicHeight
}
override fun setAlpha(alpha: Int) {
inner.alpha = alpha
}
override fun setColorFilter(colorFilter: ColorFilter?) {
inner.colorFilter = colorFilter
}
@Deprecated("Deprecated in Java", ReplaceWith("PixelFormat.TRANSLUCENT", "android.graphics.PixelFormat"))
override fun getOpacity(): Int {
return PixelFormat.TRANSLUCENT
}
}