需自行在 build.gradle 中引入依赖,版本以 releases 页最新为准;captureAsync 返回的是异步结果,需在协程中 await 并处理异常

Capturable
PatilShreyasMIT长期未更新Jetpack Compose 工具库,能把任意 Composable 内容直接截取并转成 Bitmap 图片,省去自己处理视图截图的麻烦。
🚀Jetpack Compose utility library for capturing Composable content and transforming it into Bitmap Image🖼️
dependencies {
implementation "dev.shreyaspatil:capturable:2.1.0"
}README 未说明
Jetpack Compose 工具库,把 Composable 内容截取成 Bitmap 图片
Jetpack Compose 工具库,能把任意 Composable 内容直接截取并转成 Bitmap 图片,省去自己处理视图截图的麻烦。
核心能力
- 用 rememberCaptureController 获取 CaptureController 实例
- 给 Composable 加 capturable 修饰符标记截取范围
- 调用 captureAsync 异步返回 ImageBitmap
- 可在主线程安全调用截取方法
容易踩的坑
- v3.0.1Capturable v3.0.1最新正式版2024-12-14 · 2024-12-14 12:23 UTC
Fixes 🐞
Earlier crash was occurring when same
CaptureControllerinstance is re-used within composables:java.lang.IllegalStateException: ReceiveChannel.consumeAsFlow can be collected just onceMany thanks to @MiSikora for contributing in this release 🎉.
Dependent Targets
- Jetpack Compose: 1.7.x (BoM: 2024.10.00)
- Kotlin: 2.0.21
Full Changelog: https://github.com/PatilShreyas/Capturable/compare/v3.0.0...v3.0.1
- v3.0.0Capturable v3.0.02024-09-22 · 2024-09-22 09:13 UTC
查看更新说明
What's new 🆕
- [#194] Using Compose's latest Graphics API to capture the content.
Fixes 🐞
- [#170]: Fix issue of edges getting cut off and there is a white background when capturing in devices below API 29.
- [#193]: Fix a issue of capturing with inverted colors
- [#202]: Fix issue of CaptureRequest is no getting handled in case when user captures before the request flow starts collection.
Breaking changes 🔥
- Composable method
Capturable()(which was already deprecated) removed in the favour of Modifier-based API. - Removed method
CaptureRequest#capture. - Removed method for passing
BitmapConfigtocaptureAsyncmethod.
Dependent Targets
- Jetpack Compose: 1.7.x (BoM: 2024.09.02)
- Kotlin: 2.0.22
Full Changelog: https://github.com/PatilShreyas/Capturable/compare/v2.1.0...v3.0.0
- v2.1.0Capturable v2.1.02024-02-25 · 2024-02-25 12:26 UTC
查看更新说明
Changes
- [#140] Fix: Stateful UI not updating when
Modifier.capturable()is used. - [#145] Exposed default public constructor of class
CaptureController. - [#146] Fix: Update the instance of
CaptureControllerproperly without losing capture requests insideModifierupdates viaModifierNodeElement#update.
Dependent Targets
- Jetpack Compose: 1.6.x (BoM: 2024.02.01)
- Kotlin: 1.9.22
Full Changelog: https://github.com/PatilShreyas/Capturable/compare/v2.0.0...v2.1.0
- [#140] Fix: Stateful UI not updating when
- v2.0.0Capturable v2.0.02024-01-25 · 2024-01-25 15:48 UTC
查看更新说明
Changes
- [#40] [#59] Fixes the issue of capturing the scrollable content
- [#100] Added new
ModifierAPI for capturing the content and deprecated old@Composablemethod. - [#101] Removed the usage of PixelCopy API fixing that fixes the issue of capturing unwanted content
Dependent Targets
- Jetpack Compose: 1.6.0
- Kotlin: 1.9.22
Full Changelog: https://github.com/PatilShreyas/Capturable/compare/v1.0.3...v2.0.0
- v1.0.3Capturable v1.0.32022-03-20 · 2022-03-20 12:52 UTC
查看更新说明
Changes
Fixed the following issues
- [#13] Fixed capturing composable clipped inside Window / Screen.
- [#22] Fixed crash while capturing composable inside
Fragment.
Dependent Targets
- Jetpack Compose: 1.1.0
- Kotlin: 1.6.10
Full Changelog: https://github.com/PatilShreyas/Capturable/compare/v1.0.2...v1.0.3
用户点评
登录用户的真实使用记录,可按版本和硬件筛选还没有人点评。第一个说出真实体验的人最有价值。
Capturable

🚀A Jetpack Compose utility library for converting Composable content into Bitmap image 🖼️.
Made with ❤️ for Android Developers and Composers
💡Introduction
In the previous View system, drawing Bitmap Image from View was very straightforward. But that's not the case with Jetpack Compose since it's different in many aspects from previous system. This library helps easy way to achieve the same results.
🚀 Implementation
You can check /app directory which includes example application for demonstration.
Gradle setup
In build.gradle of app module, include this dependency
dependencies {
implementation "dev.shreyaspatil:capturable:2.1.0"
}
You can find latest version and changelogs in the releases.
Usage
1. Setup the controller
To be able to capture Composable content, you need instance of CaptureController by which you can decide when to capture the content. You can get the instance as follow.
@Composable
fun TicketScreen() {
val captureController = rememberCaptureController()
}
rememberCaptureController() is a Composable function.
2. Add the content
The component which needs to be captured, a capturable() Modifier should be applied on that @Composable component as follows.
@Composable
fun TicketScreen() {
val captureController = rememberCaptureController()
// Composable content to be captured.
// Here, everything inside below Column will be get captured
Column(modifier = Modifier.capturable(captureController)) {
MovieTicketContent(...)
}
}
3. Capture the content
To capture the content, use CaptureController#captureAsync() as follows.
// Example: Capture the content when button is clicked
val scope = rememberCoroutineScope()
Button(onClick = {
// Capture content
scope.launch {
val bitmapAsync = captureController.captureAsync()
try {
val bitmap = bitmapAsync.await()
// Do something with `bitmap`.
} catch (error: Throwable) {
// Error occurred, do something.
}
}
}) { ... }
On calling this method, request for capturing the content will be sent and ImageBitmap will be
returned asynchronously. This method is safe to be called from Main thread.
📄 API Documentation
Visit the API documentation of this library to get more information in detail.
🙋♂️ Contribute
Read contribution guidelines for more information regarding contribution.
💬 Discuss?
Have any questions, doubts or want to present your opinions, views? You're always welcome. You can start discussions.
📝 License
MIT License
Copyright (c) 2022 Shreyas Patil
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.