subsampling-scale-image-view icon indicating copy to clipboard operation
subsampling-scale-image-view copied to clipboard

SSIV in Jetpack Compose (AndroidView)

Open lukaspieper opened this issue 4 years ago • 4 comments

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.

lukaspieper avatar Apr 14 '21 15:04 lukaspieper

I'm not familiar with Jetpack Compose, and as I'm not an active Android developer any more I'm not going to be able to learn enough about it to resolve this one. Another new way of building Android apps will be along in a few months :-)

It appears that the behaviour of invalidate() is changed so that the view can't rely on calling invalidate itself and must be invalidated externally. The view completely relies on invalidate for everything - from rendering after tiles are loaded to touch gestures to animation. If it is specifically only fling gestures and animated double tap zoom causing this, it's likely that calling invalidate from within onDraw is the killer.

Are you able to try copying the SubsamplingScaleImageView class into your project, and modifying it? If so you could try changing line 1027 to postInvalidateDelayed(10).

davemorrissey avatar Sep 22 '21 20:09 davemorrissey

If people are still looking for an alternative, I just released telephoto for displaying zoomable images in compose with automatic sub-sampling: https://saket.github.io/telephoto/zoomableimage/

saket avatar May 16 '23 16:05 saket

Actually I'm still using SSIV with AndroidView, the freezing is gone for me, not sure if it was an update of compose or something else. It has been some time since then and btw sorry for not coming back to this issue earlier. Only issue that remains with SSIV and compose are some gestures that are not working.

@saket Looks very promising, will give it a try in near future. Thanks for sharing.

lukaspieper avatar May 22 '23 12:05 lukaspieper

If you are looking for a zoom image component on compose, you can take a look at my ZoomImage (https://github.com/panpf/zoomimage) library, which supports zooming, panning, positioning, rotation, super large image subsampling, and it also integrates Introduced image loaders such as glide, picasso, and coil

panpf avatar Oct 08 '23 03:10 panpf