Recently, the company has been working on a poster production function, which is to write a layout and plug in various data locally, and then save the whole layout as an image to the phone, so you need to turn a view into a bitmap.
Convert a View into a Bitmap
Convert a View into a Bitmap
public static Bitmap createBitmapFromView(View view) {
//If the ImageView is getting directly
if (view instanceof ImageView) {
Drawable drawable = ((ImageView) view).getDrawable();
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
}
view.clearFocus();
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
if (bitmap != null) {
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
canvas.setBitmap(null);
}
return bitmap;
}
The above method can be used to turn a view into a bitmap, which can meet the needs of most people
However, in my actual development, the layout had rounded corners, and all the pictures saved by this method were filled with black. After trying many methods, I finally solved my problem by drawing a background color with canvas. The code is as follows.
Handle the black edge of the rounded View
public static Bitmap createBitmapFromView(View view) {
if (view instanceof ImageView) {
Drawable drawable = ((ImageView) view).getDrawable();
if (drawable instanceof BitmapDrawable) {
return ((BitmapDrawable) drawable).getBitmap();
}
}
view.clearFocus();
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
if (bitmap != null) {
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(Color.WHITE);//Here you can change to other colors
view.draw(canvas);
}
return bitmap;
}
Read More:
- hive: How to Process Data (Delete, View, Query, and etc)
- [Linux] ps+awk +while View process memory usage in real time
- The prefix “mvc” for element “mvc:view-controller” is not bound
- Firewall Status View Error: service iptables status [How to Solve]
- Android: How to Start App Automatically
- Ubuntu: How to deal with the fatal: the remote end hung up unexpected error of GIT clone Android kernel
- How to Solve Android Linux5.10 Kernel do_gettimeofday Function Error
- The use of Android setgravity()
- [Solved] Ubuntu20.04 Error: “Failed to install the following Android SDK packages as some licences have not..“error
- C#: How to Use Itextsharp to Manipulate PDF Files
- Linux Error: Failed to connect to ::1: No route to the host
- Ubuntu18.04 Compile Error: android 7 FAILED [How to Solve]
- [Solved] Ubuntu Error: Failed to connect to 127.0.0.1 port xxxxx: Connection refused
- How to Export All data in DataGridView to Local Excel
- Ubuntu Startup Error: warning failed to connect to lvmetad,falling back to device scanning
- [Solved] Failed to connect to 127.0.0.1 port XXXX: Connection refused
- [ERROR SystemVerification]: failed to parse kernel config: unable to load kernel module: “configs“
- Error in compiling Android AOSP by Ubantu [How to Solve]
- [Solved] Git Error: OpenSSL SSL_read: Connection was reset, errno 10054和Failed to connect to github.com port 443
- The upgrade of Ubuntu results in an error in the compilation of Android Jack [Two Method to Solve]