|
2 | 2 |
|
3 | 3 |  |
4 | 4 |
|
5 | | -A Jetpack Compose utility library for converting Composable content into Bitmap image 🖼️📸. |
6 | | -_Made with ❤️ for Android Developers and Composers_ |
| 5 | +🚀A Jetpack Compose utility library for converting Composable content into Bitmap image 🖼️📸. |
| 6 | +_Made with ❤️ for Android Developers and Composers_ |
| 7 | + |
| 8 | +## 💡Introduction |
| 9 | + |
| 10 | +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. |
| 11 | +It's built upon the `ComposeView` and uses `View`'s APIs to draw the Bitmap image. |
| 12 | + |
| 13 | +## 🚀 Implementation |
| 14 | + |
| 15 | +You can check [/app](/app) directory which includes example application for demonstration. |
| 16 | + |
| 17 | +### Gradle setup |
| 18 | + |
| 19 | +In `build.gradle` of app module, include this dependency |
| 20 | + |
| 21 | +```gradle |
| 22 | +dependencies { |
| 23 | + implementation "dev.shreyaspatil:capturable:1.0.0" |
| 24 | +} |
| 25 | +``` |
| 26 | + |
| 27 | +_You can find latest version and changelogs in the [releases](https://github.com/PatilShreyas/Capturable/releases)_. |
| 28 | + |
| 29 | +### Usage |
| 30 | + |
| 31 | +#### 1. Setup the controller |
| 32 | + |
| 33 | +To be able to capture Composable content, you need instance of [`CaptureController`](https://patilshreyas.github.io/Capturable/capturable/dev.shreyaspatil.capturable.controller/-capture-controller/index.html) by which you can decide when to capture the content. You can get the instance as follow. |
| 34 | + |
| 35 | +```kotlin |
| 36 | +@Composable |
| 37 | +fun TicketScreen() { |
| 38 | + val captureController = rememberCaptureController() |
| 39 | +} |
| 40 | +``` |
| 41 | + |
| 42 | +_`rememberCaptureController()` is a Composable function._ |
| 43 | + |
| 44 | +#### 2. Add the content |
| 45 | + |
| 46 | +The component which needs to be captured should be placed inside [`Capturable`](https://patilshreyas.github.io/Capturable/capturable/dev.shreyaspatil.capturable/-capturable-kt/-capturable.html) composable as follows. |
| 47 | + |
| 48 | +```kotlin |
| 49 | +@Composable |
| 50 | +fun TicketScreen() { |
| 51 | + val captureController = rememberCaptureController() |
| 52 | + |
| 53 | + Capturable( |
| 54 | + controller = captureController, |
| 55 | + onCaptured = { bitmap -> |
| 56 | + // This is captured bitmap of a content inside Capturable Composable. |
| 57 | + // Do something with [bitmap] |
| 58 | + } |
| 59 | + ) { |
| 60 | + // Composable content to be captured. |
| 61 | + // Here, `MovieTicketContent()` will be get captured |
| 62 | + MovieTicketContent(...) |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +#### 3. Capture the content |
| 68 | + |
| 69 | +To capture the content, use [`CaptureController#capture()`](https://patilshreyas.github.io/Capturable/capturable/dev.shreyaspatil.capturable.controller/-capture-controller/capture.html) as follows. |
| 70 | + |
| 71 | +```kotlin |
| 72 | +Button(onClick = { captureController.capture() }) { ... } |
| 73 | +``` |
| 74 | + |
| 75 | +On calling this method, request for capturing the content will be sent and event will be received in callback `onCaptured` with `ImageBitmap` as a parameter in the `Capturable` function. |
| 76 | + |
| 77 | +_Make sure to call this method as a part of **callback function** and **not as a part of the Composable function itself**. Otherwise, it'll lead to capture bitmaps unnecessarily in recompositions which can degrade the performance of the application._ |
| 78 | + |
| 79 | +That's all needed! |
| 80 | + |
| 81 | +## 📄 API Documentation |
| 82 | + |
| 83 | +[**Visit the API documentation of this library**](https://patilshreyas.github.io/Capturable) to get more information in detail. |
| 84 | + |
| 85 | +--- |
| 86 | + |
| 87 | +## 🙋♂️ Contribute |
| 88 | + |
| 89 | +Read [contribution guidelines](CONTRIBUTING.md) for more information regarding contribution. |
| 90 | + |
| 91 | +## 💬 Discuss? |
| 92 | + |
| 93 | +Have any questions, doubts or want to present your opinions, views? You're always welcome. You can [start discussions](https://github.com/PatilShreyas/Capturable/discussions). |
| 94 | + |
| 95 | +## 📝 License |
| 96 | + |
| 97 | +``` |
| 98 | +MIT License |
| 99 | +
|
| 100 | +Copyright (c) 2022 Shreyas Patil |
| 101 | +
|
| 102 | +Permission is hereby granted, free of charge, to any person obtaining a copy |
| 103 | +of this software and associated documentation files (the "Software"), to deal |
| 104 | +in the Software without restriction, including without limitation the rights |
| 105 | +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 106 | +copies of the Software, and to permit persons to whom the Software is |
| 107 | +furnished to do so, subject to the following conditions: |
| 108 | +
|
| 109 | +The above copyright notice and this permission notice shall be included in all |
| 110 | +copies or substantial portions of the Software. |
| 111 | +
|
| 112 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 113 | +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 114 | +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 115 | +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 116 | +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 117 | +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 118 | +SOFTWARE. |
| 119 | +``` |
0 commit comments