android 如何使用'ImageRequest.Builder.target'在新的线圈版本在jetpack合成?

n53p2ov0  于 2023-02-11  发布在  Android
关注(0)|答案(2)|浏览(126)

我的成绩

// Coil
implementation "io.coil-kt:coil-compose:1.4.0"

问题描述

以前我将coil与Google的accompanist一起使用,但当我按照文档所述迁移到新版本的coil时,target方法出现了问题:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.pokedex, PID: 13502
    java.lang.IllegalArgumentException: request.target must be null.
        at coil.compose.ImagePainterKt.rememberImagePainter(ImagePainter.kt:94)
...

线圈实现

当浏览ImagePainter(coil类)的内部代码时,您可以看到target方法出于某种原因确实需要为null:

@Composable
fun rememberImagePainter(
     request: ImageRequest,
     imageLoader: ImageLoader,
     onExecute: ExecuteCallback = ExecuteCallback.Default,
): ImagePainter {
     requireSupportedData(request.data)
     require(request.target == null) { "request.target must be null." }
...

我的代码

下面是我在jetpack compose中使用的组件(图像组件位于列中):

Image(
    modifier = Modifier
        .size(120.dp)
        .align(Alignment.CenterHorizontally),
    painter = rememberImagePainter(
        data = entry.imageUrl,
        builder = {
            crossfade(true)
            target {
                viewModel.calcDominantColor(it) { color ->
                    dominantColor = color
                }
            }
            transformations(CircleCropTransformation())
        },
    ),
    contentDescription = entry.pokemonName
)

我需要目标方法根据它作为参数传递的drawable在我的viewModel上执行内部操作。有人能帮助我吗?

7rtdyuoh

7rtdyuoh1#

在Coil2.0.0中,AsyncImagerememberAsyncImagePainter都有onSuccess回调参数,使用该参数可以获得可绘制对象,如下所示:

AsyncImage(
    model = imageURL,
    contentDescription = null,
    onSuccess = { success ->
        val drawable = success.result.drawable
    }
)

线圈1.4.0版本:
这是预期行为,因为rememberImagePainter在内部设置target
您可以跟踪绘图器状态,等待Success并从中获取drawable。还可以将其与LaunchedEffect一起使用以防止重新计算:

val painter = rememberImagePainter(
    data = imageUrl,
    builder = {
        ...
    },
)
(painter.state as? ImagePainter.State.Success)
    ?.let { successState ->
        LaunchedEffect(Unit) {
            val drawable = successState.result.drawable
            viewModel.calcDominantColor(drawable) { color ->
                dominantColor = color
            }
        }
    }
Image(
    painter = painter,
    contentDescription = "...",
    modifier = Modifier
        ...
)
j2cgzkjk

j2cgzkjk2#

我知道你遵循这个https://www.youtube.com/watch?v=jrIfGAk8PyQ&list=PLQkwcJG4YTCTimTCpEL5FZgaWdIZQuB7m&index=5由Philipp Lackner尝试此代码

AsyncImage(
            model = ImageRequest.Builder(LocalContext.current)
                .data(entry.imageUrl)
                .crossfade(true)
                .build(),
            contentDescription = entry.pokemonName,
            onSuccess = {
                viewModel.calcDominantColor(it.result.drawable) { color ->
                    dominantColor = color
                }
            },
            modifier = Modifier
                .size(120.dp)
                .align(CenterHorizontally)
        )

并使用此库实现(“io.coil-kt:coil-compose:2.2.2”)以及更多信息https://coil-kt.github.io/coil/compose/

相关问题