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:
- The prefix “mvc” for element “mvc:view-controller” is not bound
- Vue implements page caching with keep alive
- PM2 user defined Log, PID and other Log File Locations
- Vector series in actual C + +_ To_ fit()
- Chinese garbled problem when running. C file in Linux
- linux tomcat Run (DWP exit error AGENT_ERROR_TRANSPORT_INIT(197): No transports initialized [debugIni)
- How to Fix xterm Xt error: Can’t open display: xterm: DISPLAY is not set
- How to Use Apt get Command Under Mac OSX
- You might want to save ‘/run/in itramf s/rdsosreport.txt“ to a USB stick or bootaf ter mounting
- Summary of unity3d 11 SceneManager scene management usage
- How to Skip testing when Maven is packaged
- Nginx configuration 80 can be accessed by forcing jump 443 or not
- How to Fix Ubuntu(Linux) mount error(22)
- Error report when executing ifconfig under centos7
- How to Add Dfrours Model
- Installation and configuration of redis in Linux
- Vue uses localstorage and sessionstorage to store data
- Linux Nagios failed to log in to internal server error (Fixed)
- Sublime text 3 compiles and executes C/C++ programs directly
- Linux changing password enter new UNIX password: passwd: authentication token manipulation error