最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

android webView截圖的4種方法

 更新時間:2018年05月05日 15:06:37   作者:PalmerYang  
這篇文章主要為大家詳細介紹了android webView截圖的4種方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下

android 在webView里面截圖大概有四種方式,具體內容如下

1.獲取到DecorView然后將DecorView轉換成bitmap然后寫入到文件里面.

View view = getWindow().getDecorView();
  Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
   Canvas canvas = new Canvas(bitmap);
    view.draw(canvas);
    Log.d(TAG,"bitmap--"+bitmap);
    try {
      String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg";
      FileOutputStream fos = new FileOutputStream(fileName);
      //壓縮bitmap到輸出流中
      bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
      fos.close();
      Toast.makeText(WebviewFromGetDecorView.this, "截屏成功", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
      Log.e(TAG, e.getMessage());
    }finally {
      if(bitmap!=null) {
     bitmap.recycle();
 }

}

2.使用webViewpicture來實現(xiàn)該功能.(該方法被廢棄了因此不建議使用)

 Picture picture = webView.capturePicture();
  int width = picture.getWidth();
  int height = picture.getHeight();
   if (width > 0 && height > 0) {
   Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
   Canvas canvas = new Canvas(bitmap);
   picture.draw(canvas);
    try {
     String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg";
     FileOutputStream fos = new FileOutputStream(fileName);
     //壓縮bitmap到輸出流中
     bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
     fos.close();
     Toast.makeText(WebviewFromCapture.this, "截屏成功", Toast.LENGTH_LONG).show();
     bitmap.recycle();
     } catch (Exception e) {
   Log.e(TAG, e.getMessage());
  }
}

3.使用webViewDraw來實現(xiàn).(該方法被廢棄了因此不建議使用)

float scale = webView.getScale();
  int webViewHeight = (int) (webView.getContentHeight()*scale+0.5);
   Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(),webViewHeight, Bitmap.Config.ARGB_8888);
   Canvas canvas = new Canvas(bitmap);
   webView.draw(canvas);
    try {
     String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg";
     FileOutputStream fos = new FileOutputStream(fileName);
     //壓縮bitmap到輸出流中
      bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
      fos.close();
      Toast.makeText(WebviewFromDraw.this, "截屏成功", Toast.LENGTH_LONG).show();
      bitmap.recycle();
      } catch (Exception e) {
  Log.e(TAG, e.getMessage());
}

4.使用webViewDrawCache來實現(xiàn)(建議使用).

Bitmap bitmap = webView.getDrawingCache();
  try {
    String fileName = Environment.getExternalStorageDirectory().getPath() + "/webview_jietu.jpg";
    FileOutputStream fos = new FileOutputStream(fileName);
    //壓縮bitmap到輸出流中
    bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
    bitmap.recycle();
    fos.close();
   Toast.makeText(WebviewFromDrawCache.this, "截屏成功", Toast.LENGTH_LONG).show();
} catch (Exception e) {
      Log.e(TAG, e.getMessage());
    } finally {
      bitmap.recycle();
}

注意:

在android5.0及以上版本使用webView進行截長圖時,默認是截取可是區(qū)域內的內容.因此需要在支撐窗體內容之前加上如下方法.

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  WebView.enableSlowWholeDocumentDraw();
   }
 setContentView(R.layout.activity_webview);

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論

潜江市| 普陀区| 建宁县| 芜湖县| 临湘市| 中牟县| 墨脱县| 青海省| 寻乌县| 偏关县| 青浦区| 南澳县| 许昌市| 广平县| 吴堡县| 宿迁市| 汉沽区| 定南县| 泗阳县| 万载县| 青岛市| 满城县| 成安县| 太仆寺旗| 富裕县| 福海县| 泸州市| 彝良县| 开封县| 炉霍县| 都匀市| 天台县| 根河市| 平果县| 屯留县| 祁阳县| 三河市| 塔河县| 志丹县| 凤台县| 双辽市|