Bill Software Engineer
Bill Software Engineer

Reputation: 7782

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. 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);
}

Upvotes: 1

Views: 705

Answers (2)

samgak
samgak

Reputation: 24417

Crop using 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);
}

Upvotes: 2

Greg Ennis
Greg Ennis

Reputation: 15379

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

Upvotes: 0

Related Questions