天天看點

WebView截屏

WebView的截圖有幾種方式

(一)這種方式要求WebView必須設定setDrawingCacheEnabled(true)

/**
     * 這種方法要求webview要設定setDrawingCacheEnabled(true);
     * @return
     */
    public Bitmap getWebViewFromCache(){
        Bitmap drawingCacheBitmap = mWebView.getDrawingCache();
        return drawingCacheBitmap;
    }
           

(二)這種方式在安卓4.4已經廢除,因為capturePicture方法廢除了

public Bitmap getWebViewCapturePicture(){

        Picture picture = mWebView.capturePicture();
        Bitmap bitmap = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        mWebView.draw(canvas);
        return bitmap;
    }
           

(三)直接給目前頁面截屏

/**
     * 截屏
     * @return
     */
    public Bitmap getWebViewDecorView(){

        View decorView = this.getWindow().getDecorView();
        Bitmap bitmap = Bitmap.createBitmap(decorView.getWidth(), decorView.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        decorView.draw(canvas);
        return bitmap;
    }
           

繼續閱讀