之前项目中有个功能是把view转换成bitmap保存到本地或者分享给第三方,之前用的方法都是
view.setDrawingCacheEnabled(true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
view.setDrawingCacheEnabled(false);
升级以后这几个都显示“is deprecated”,去stackoverflow找到了如下方法替换
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)
亲测可用
下面附链接
https://stackoverflow.com/questions/52642055/view-getdrawingcache-is-deprecated-in-android-api-28