5

I was successful show animated webp using coil on image composable.
However, my goal is to only run this animated webp once.
Now it is repeating infinitely.
Is there a way?

Is my code.

animated webp

class MainActivity : ComponentActivity() {
    val resourceAnimated =
        "https://www.bandisoft.com/honeycam/help/file_format/sample.webp"

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            MaterialTheme {
                // A surface container using the 'background' color from the theme
                Surface(color = MaterialTheme.colors.background) {
                    MyApp()
                }
            }
        }
    }

    @Composable
    private fun MyApp() {
        val imageLoader = ImageLoader.Builder(this)
            .components {
                if (SDK_INT >= 28) {
                    add(ImageDecoderDecoder.Factory())
                } else {
                    add(GifDecoder.Factory())
                }
            }
            .build()

        val painter = rememberAsyncImagePainter(
            model = resourceAnimated,
            imageLoader = imageLoader,
        )

        Test(painter)
    }

    @Composable
    private fun Test(painter: AsyncImagePainter) {
        Box(modifier = Modifier.padding(16.dp)) {
            Image(
                painter = painter, contentDescription = null,
                modifier = Modifier.fillMaxSize()
            )
        }
    }
2
  • I have the same problem. Have you found a solution? Oct 18, 2022 at 10:35
  • 3
    yes. first add "implementation("io.coil-kt:coil-gif:2.2.2")" in gradle, and just use repeatCount() of ImageRequest.Builder extension function
    – 권오성
    Oct 19, 2022 at 11:39

1 Answer 1

4

You can animate webp only once in coil.

Use this extension function

Image(painter =  rememberAsyncImagePainter(ImageRequest.Builder(LocalContext.current)
                 .data(data= "your image url")
                 .repeatCount(0)
                 .build())
)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.