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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章