我發現了兩種從視圖創建位圖的方法。但一旦我這樣做,視圖就消失了,我不能再使用它了。如何在生成位圖後重新繪製視圖?從視圖創建位圖使視圖消失,如何獲取視圖畫布?
1:
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;
}
第二:
Bitmap viewCapture = null;
theViewYouWantToCapture.setDrawingCacheEnabled(true);
viewCapture = Bitmap.createBitmap(theViewYouWantToCapture.getDrawingCache());
theViewYouWantToCapture.setDrawingCacheEnabled(false);
編輯
所以,我想我明白在第一個會發生什麼,我們基本上消除了認爲從它的原始畫布並將其繪製在與該位圖相關的其他位置。可以以某種方式存儲原始畫布,然後將視圖設置爲在那裏重繪?
Thanks @Guy Smith,工作正常,我缺少的是在提取位圖後重新佈局視圖! view.layout(0,0,width,height);! – caiocpricci2