Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SSIV in Jetpack Compose (AndroidView) #548

Open
lukaspieper opened this issue Apr 14, 2021 · 4 comments
Open

SSIV in Jetpack Compose (AndroidView) #548

lukaspieper opened this issue Apr 14, 2021 · 4 comments

Comments

@lukaspieper
Copy link

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.

@davemorrissey
Copy link
Owner

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).

@saket
Copy link

saket commented May 16, 2023

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/

@lukaspieper
Copy link
Author

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.

@panpf
Copy link

panpf commented Oct 8, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants