如何在 Jetpack Compose 中显示视频缩略图?
How to display video thumbnail in Jetpack Compose?
我正在使用 Jetpack Compose 实现一个简单的画廊屏幕,它在屏幕上显示所有视频和图像缩略图
我已成功显示文件路径中的图像。但是,我在显示视频缩略图时遇到了麻烦。我怎样才能使用 Coil 做到这一点?
这是我显示图片缩略图的代码:
@Composable
fun ImageLoaderFromLocal(
url: String,
placeHolderResId: Int,
modifier: Modifier,
transformation: Transformation
) {
val painter = rememberImagePainter(data = File(url),
builder = {
placeholder(placeHolderResId)
crossfade(true)
transformations(transformation)
})
Image(
painter = painter,
contentDescription = null,
modifier = modifier,
contentScale = ContentScale.Inside
)
}
根据Coil documentation,你需要添加以下依赖:
implementation("io.coil-kt:coil-video:$coil_version")
并在构建器中指定 fetcher:
val context = LocalContext.current
val painter = rememberImagePainter(
data = url,
builder = {
fetcher(VideoFrameUriFetcher(context))
// optionally set frame location
videoFrameMillis(1000)
placeholder(placeHolderResId)
crossfade(true)
transformations(transformation)
}
)
我正在使用 Jetpack Compose 实现一个简单的画廊屏幕,它在屏幕上显示所有视频和图像缩略图
我已成功显示文件路径中的图像。但是,我在显示视频缩略图时遇到了麻烦。我怎样才能使用 Coil 做到这一点?
这是我显示图片缩略图的代码:
@Composable
fun ImageLoaderFromLocal(
url: String,
placeHolderResId: Int,
modifier: Modifier,
transformation: Transformation
) {
val painter = rememberImagePainter(data = File(url),
builder = {
placeholder(placeHolderResId)
crossfade(true)
transformations(transformation)
})
Image(
painter = painter,
contentDescription = null,
modifier = modifier,
contentScale = ContentScale.Inside
)
}
根据Coil documentation,你需要添加以下依赖:
implementation("io.coil-kt:coil-video:$coil_version")
并在构建器中指定 fetcher:
val context = LocalContext.current
val painter = rememberImagePainter(
data = url,
builder = {
fetcher(VideoFrameUriFetcher(context))
// optionally set frame location
videoFrameMillis(1000)
placeholder(placeHolderResId)
crossfade(true)
transformations(transformation)
}
)