我想将我的Linearlayout保存为bmp.
因此,我使用以下代码执行此操作:
public Bitmap viewToBitmap(View view) { Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); return bitmap; }
但如果我运行此代码,我的位图只包含ImageView
(ImageView_qrcode).我检查了一下.如果我给Linearview充气TextViews
正确显示,但输出bmp只包含ImageView
有人知道的原因吗?
你可以尝试这个解决方案:
public static Bitmap getBitmapFromView(View view) { Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(returnedBitmap); Drawable bgDrawable =view.getBackground(); if (bgDrawable!=null) { bgDrawable.draw(canvas); } else { canvas.drawColor(Color.WHITE); } view.draw(canvas); return returnedBitmap; }
有绘图缓存的解决方案:
public Bitmap getBitmapFromView(View view){ view.setDrawingCacheEnabled(true); return view.getDrawingCache(); }