2017-07-25 198 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