2017-07-25 196 views
0

我使用屏幕截圖,並且想要以編程方式裁剪位圖從屏幕底部 150dp。 (擦除從屏幕底部的位圖150dp)從屏幕底部到屏幕頂部裁剪位圖150dp

如何做到這一點?

這是形象的解釋:http://imgur.com/TP2ouVp

編輯。對於採取截屏全碼:

public void takeScreenshot() { 
    Date now = new Date(); 
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); 

    try { 

     String folder_main = "APP_FOLDER"; 
     File f = new File(Environment.getExternalStorageDirectory(), folder_main); 
     if (!f.exists()) { 
      f.mkdirs(); 
     } 

     // image naming and path to include sd card appending name you choose for file 
     String mPath = Environment.getExternalStorageDirectory().toString() + "/APP_FOLDER/" + now + ".jpg"; 

     // create bitmap screen capture 
     View v1 = getWindow().getDecorView().getRootView(); 
     v1.setDrawingCacheEnabled(true); 

     Bitmap source = v1.getDrawingCache(); 
     int x = 0; 
     int y = v1.getHeight() ; 
     int width = source.getWidth() - x; 
     int height = source.getHeight() - y; 
     Bitmap bitmap = Bitmap.createBitmap(source, x, y, width, height); 

     v1.setDrawingCacheEnabled(false); 

     File imageFile = new File(mPath); 

     FileOutputStream outputStream = new FileOutputStream(imageFile); 
     int quality = 100; 
     bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); 
     outputStream.flush(); 
     outputStream.close(); 

     openScreenshotWhatsApp (imageFile); 
    } catch (Throwable e) { 
     // Several error may come out with file handling or OOM 
     e.printStackTrace(); 
    } 
} 

我很困惑。 感謝

+0

你有沒有嘗試過這個int height = source.getHeight() - 150; –

+0

你是否能夠截圖的任何部分? –

+0

使用該代碼,無法創建屏幕截圖。 但與此代碼完成的:。 //創建位圖截屏 \t查看V1 = getWindow()getDecorView()getRootView(); \t v1.setDrawingCacheEnabled(真); \t位圖的位圖= Bitmap.createBitmap(v1.getDrawingCache()); \t v1.setDrawingCacheEnabled(假); – Bonnie7

回答

1

試試這個代碼

調用此方法,在你想要的屏幕截圖最外層的ViewGroup傳遞:

public Bitmap screenShot(View view) { 
    Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), 
      150, Config.ARGB_8888); 
    Canvas canvas = new Canvas(bitmap); 
    view.draw(canvas); 
    return bitmap; 
} 

欲瞭解更多,您可以檢查此answers以及

+0

請檢查我編輯完整的代碼拍攝屏幕截圖。 謝謝 – Bonnie7