Api upgrade 28 adaptation pit filling (1)

There was a function in the previous project to convert the view into a bitmap and save it locally or share it with a third party. The methods used before were

                    view.setDrawingCacheEnabled(true);
                    view.buildDrawingCache();
                    Bitmap bitmap = view.getDrawingCache();
                    view.setDrawingCacheEnabled(false);

After the upgrade, these all show "is deprecated". I went to stackoverflow and found the following method to replace it.

public Bitmap getBitmapFromView(View view)
{
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    return bitmap;
}

public Bitmap getBitmapFromView(View view,int defaultColor)
{
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(defaultColor);
    view.draw(canvas);
    return bitmap;
}
//Example 
ConstraintLayout root = findViewById(R.id.root);
getBitmapFromView(root)
getBitmapFromView(root,Color.WHITE)

Available for personal testing

Link below

https://stackoverflow.com/questions/52642055/view-getdrawingcache-is-deprecated-in-android-api-28

Guess you like

Origin blog.csdn.net/u010055598/article/details/103563903