2012-03-20 103 views
1

有什麼方法可以將我剛繪製的畫布內容複製到位圖中?如何將畫布複製到位圖

編輯:只是爲了澄清,我當前的繪圖過程如下:

Canvas canvas = holder.lockCanvas(); 

if (canvas != null) 
{ 
    onDraw(canvas); // this does all the drawing 

    // at this point I would like to copy the canvas into a bitmap 

    holder.unlockCanvasAndPost(canvas); // this makes the canvas visible to the user 
} 

編輯:看一下第一個答案貼出來,我寫了下面的代碼:

if (grab_screen == -1) // draw and show on screen as normal 
{ 
    Canvas canvas = holder.lockCanvas(); 
    if(canvas != null) 
    { 
     onDraw(canvas); 
     holder.unlockCanvasAndPost(canvas); 
    }     
} 
else // draw into screengrab_bitmap[grab_screen] 
{ 
    Canvas canvas = new Canvas(screengrab_bitmap[grab_screen]); 
    onDraw(canvas); 
    grab_screen = -1; 
} 

這應該意味着如果我將grab_screen設置爲n,那麼在下次調用onDraw時,圖像將被寫入screengrab_bitmap [n]。

回答

6

創建一個「空的」位圖,將其分配給畫布(new Canvas(newBitmap)),在該畫布中繪製事物。您在畫布上繪製的所有內容都將繪製在位圖中。

+0

看起來不錯。據推測,我不會同時看到onDraw完成的繪圖(請參閱我的編輯)。那麼unlockCanvasAndPost呢?我認爲我不應該這樣稱呼它。 – Mick 2012-03-20 17:30:54

+0

看看,也許幫助:http://javafriend.yolasite.com/myandroid/saving-bitmap-from-surface-view http://www.tutorialforandroid.com/2010/11/drawing-with-canvas-in -Android-saving.html – 2012-03-20 20:24:42

相關問題