2012-08-15 63 views
-1

我想在我的應用程序中畫線。完成後,我想將屏幕保存爲圖像。但我不想讓按鈕在圖像中。我想裁剪它然後保存它。我曾嘗試使用此代碼在onclicklistener中裁剪我的位圖。但它沒有奏效。 Bitmap.createBitmap(bitmap,5,5,5,5); 這是我的所有代碼:如何在Android中裁剪截圖

content.setDrawingCacheEnabled(true); 
    content.buildDrawingCache(); 
    Bitmap bitmap = Bitmap.createBitmap(content.getDrawingCache()); // Bitmap 
    Bitmap.createBitmap(bitmap, 5, 5, 5, 5); 
    content.setDrawingCacheEnabled(false); 

    File file = new File("/sdcard/SurucuImza.jpg"); 
    FileOutputStream ostream = null; 



    try { 
     if (bitmap != null) { 
      file.createNewFile(); 
      ostream = new FileOutputStream(file); 
      bitmap.compress(CompressFormat.JPEG, 100, ostream); 
      ostream.flush(); 
      ostream.close(); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    bitmap.recycle(); 
    bitmap = null; 
    ostream = null; 
    content.setDrawingCacheEnabled(false); 

} 
+0

http://stackoverflow.com/q/2085003/1007273 – hovanessyan 2012-08-15 12:31:29

回答

0

您應該指定此行給一個變量的結果:

Bitmap.createBitmap(bitmap, 5, 5, 5, 5); 

//Ex: 
Bitmap croppedBitmap = Bitmap.createBitmap(bitmap, 5, 5, 5, 5); 

,然後保存的croppedBitmap代替bitmap.

還有一兩件事,是你確定你的按鈕是5x5像素? createBitmap()的最後兩個參數指定了widthheight

+0

仍然沒有工作:( – Patrick 2012-08-15 12:42:29

0

由於Android的屏幕大小和密度的大小可變,我建議你不要使用content,但在你的XML佈局你有一個ViewGroup,這將是「printscreenable」和其他區域(ViewGroup中以外)與按鈕。

這樣,你就必須做同樣的操作,但是使用viewGroup代替content