简体   繁体   English

如何裁剪WebView屏幕截图?

[英]How to crop a WebView screen capture?

I would like to take a screenshot of my WebView, then crop it at x,y,w,h. 我想截取WebView的屏幕截图,然后将其裁剪为x,y,w,h。 Here is what I got: 这是我得到的:

private WebView mWebView;
public void onSavePhoto(int top, int left, int width, int height){
    Bitmap bitmap = Bitmap.createBitmap(mWebView.getWidth(), mWebView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    mWebView.draw(canvas);
}

Crop using createBitmap 使用createBitmap裁剪

private WebView mWebView;
public void onSavePhoto(int top, int left, int width, int height){
    Bitmap bitmap = Bitmap.createBitmap(mWebView.getWidth(), mWebView.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    mWebView.draw(canvas);

    // crop bitmap:
    Bitmap croppedBitmap = Bitmap.createBitmap(bitmap, left, top, width, height);
}

You can do this with these steps 您可以按照以下步骤进行操作

  1. Create a new bitmap that is the dimensions you want 创建一个新的位图,它是您想要的尺寸
  2. Use drawBitmap to copy the section you want from the original bitmap to the new one 使用drawBitmap将所需部分从原始位图复制到新位图

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM