我有这样的功能
/**
* Extends the [Modifier] to capture and save the composable content into a [Picture].
* This function uses the [drawWithCache] to intercept the drawing process. It redirects
* the drawing commands into a [Picture], which can be used later for operations like
* creating a bitmap or drawing onto a canvas. This is particularly useful for capturing
* a snapshot of the composable content without needing to display it on the screen.
*
* @param picture The [Picture] instance where the composable content will be recorded.
* It acts as a recording canvas to capture the drawing commands.
* Make sure to initialize this [Picture] before passing it to the modifier.
* The content captured by this [Picture] can be used for various purposes,
* such as generating a bitmap for saving or sharing the content.
*
* @param keys Optional array of keys that trigger recomposition and re-capture of the content
* when any of them changes. This is useful when you want to capture the content
* again due to changes in the state or data that affects the appearance or
* composition of the captured content. The capture process will be reinvoked
* upon any change in the values of these keys, ensuring the captured [Picture]
* is up to date. This mechanism is similar to the way keys are used in
* [LaunchedEffect] for controlling effects based on state changes.
*
* Example usage:
* ```
* val picture = remember { Picture() }
*
* Box(modifier = Modifier.captureDrawableContent(picture)) {
* Text("Hello, World!")
* }
* ```
*
* Note: The capturing happens during the drawing phase of the composable. Therefore,
* modifications to the [Picture] after the composable is drawn will not reflect in the
* captured content. If you need to update the captured content, you should trigger a
* recomposition with an updated [Picture] instance.
*
* based on https://developer.android.com/jetpack/compose/graphics/draw/modifiers#composable-to-bitmap
*/
public fun Modifier.captureDrawableContent(picture: Picture, vararg keys: Any?): Modifier = composed {
val keyHash = keys.contentHashCode()
remember(keyHash) {
Modifier.drawWithCache {
val width = size.width.toInt()
val height = size.height.toInt()
onDrawWithContent {
val pictureCanvas = Canvas(picture.beginRecording(width, height))
draw(this, layoutDirection, pictureCanvas, size) {
[email protected]()
}
picture.endRecording()
drawIntoCanvas { canvas -> canvas.nativeCanvas.drawPicture(picture) }
}
}
}
}
这适用于除以下设备之外的所有设备:
此代码产生异常:
Fatal Exception: java.lang.IllegalStateException: Software rendering doesn't support hardware bitmaps
在这个区块内:
draw(this, layoutDirection, pictureCanvas, size) {
[email protected]()
}
有人知道如何修复 Android 8 设备吗?
我希望找到一些方法在 Android Jetpack Compose 上无需硬件位图即可绘制内容
我发现问题了。在我的应用程序中,我使用 Coil lib 来显示图片。 当我将
Modifier.drawWithCache
与屏幕 coil.compose.AsyncImage
一起使用时,会产生错误 Fatal Exception: java.lang.IllegalStateException: Software rendering doesn't support hardware bitmaps
。
为 Android 8 及更低版本添加
allowHardware(false)
修复了此问题。
AsyncImage(
model = ImageRequest.Builder(LocalContext.current)
.data(iconUrl)
.allowHardware(Build.VERSION.SDK_INT > Build.VERSION_CODES.O)
.build(),
contentDescription = null
)