Create bitmap from a view
Create a bitmap from the view on Android.
Hello everyone! This is my first programming tutorial here in which I will discuss about creating the bitmap from a view at runtime.
I have an app for Android in which users can customize all the theme colors according to their requirements. They can also copy or share their theme in the form of a JSON which can be imported to achieve the exact same look later or can be shared between the other devices.
Recently, I decided to generate an image from the theme preview which users can share along with the JSON to give some idea about the theme before importing it. The in-app preview already shows the theme before applying it but I wanted to provide similar kind of functionality for posts shared on social media and in other communities.
Initially, I tried to use the view drawing cache. Although, it is working fine most of the drawing cache-related methods are deprecated in Android 9 (API 28). Please check the deprecated code below:
As drawing cache API is deprecated, I found that we can achieve a similar result by using the Canvas API. Please check the new code below:
0for height or width to use the current view dimensions. You can check the code for
Outputs from both the methods are similar but it is recommended to use the canvas API as the view drawing cache methods may not work in future releases of Android.
Dynamic Theme Preview
There is another API for Android 7.0 (API 24+) or above devices which you can use to take screenshots of the UI. It seems complicated to me and I have not tried it yet. You can check the official documentation here.
I have created a library for similar kinds of utility methods related to color, device, drawable, package, tasks, SDK, etc. which you can use to perform various runtime (dynamic) operations. Please visit the GitHub page for more info.