SSIV in Jetpack Compose (AndroidView)

Author: lukaspieperCreated Apr 14, 2021Updated Oct 8, 2023

Expected behaviour

SSIV is working as it does outside of jetpack compose.

Actual behaviour

SSIV freezes after some time when zooming in/out and dragging the image around. Then it does not respond to further gestures. More technically, onDraw() is no longer called once SSIV is frozen. Only when I call invalidate() from a button, SSIV moves again. But after that it freezes again pretty fast. This process can be repeated for as long as desired.

No exception or other warning occurs. For me personally it almost looks like SSIV is running in an infinite loop?

Steps to reproduce

private lateinit var ssiv: SubsamplingScaleImageView

@Composable
fun SsivCompose() {
    AndroidView(
        modifier = Modifier.fillMaxSize(),
        factory = { context ->
            SubsamplingScaleImageView(context).apply {

                ssiv = this
                setDebug(true)

                setOnTouchListener { _, _ ->
                    // After SSIV is frozen, this message is still logged
                    logDebug("Touched!!!")
                    return@setOnTouchListener false
                }

                // ZOOM_FOCUS_CENTER_IMMEDIATE is the only zoom style that does not freeze SSIV immediately
                // setDoubleTapZoomStyle(SubsamplingScaleImageView.ZOOM_FOCUS_CENTER_IMMEDIATE)

                setImage(ImageSource.uri(file.uri))
            }
        }
    )

    Button(onClick = { ssiv.invalidate() }) {

    }
}

Affected devices

Tested on Pixel 3, emulator

Affected images

All images

Many thanks.

Source: davemorrissey/subsampling-scale-image-view