下面列出了android.webkit.WebView#draw ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private Bitmap captureWebView3(WebView wv) {
try {
float scale = wv.getScale();
int height = (int) (wv.getContentHeight() * scale + 0.5);
Bitmap bitmap = Bitmap.createBitmap(wv.getWidth(), height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
wv.draw(canvas);
return bitmap;
} catch (Exception e) {
try {
openCache(wv);
return wv.getDrawingCache();
} finally {
closeCache(wv);
}
}
}
/**
* Convert view to an image. Can be used to make animations smoother.
*
* @param context The current Context or Activity that this method is called from
* @param viewToBeConverted View to convert to a Bitmap
* @return Bitmap object that can be put in an ImageView. Will look like the converted viewToBeConverted.
*/
public static Bitmap viewToImage(Context context, WebView viewToBeConverted) {
int extraSpace = 2000; //because getContentHeight doesn't always return the full screen height.
int height = viewToBeConverted.getContentHeight() + extraSpace;
Bitmap viewBitmap = Bitmap.createBitmap(viewToBeConverted.getWidth(), height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap);
viewToBeConverted.draw(canvas);
//If the view is scrolled, cut off the top part that is off the screen.
try {
int scrollY = viewToBeConverted.getScrollY();
if (scrollY > 0) {
viewBitmap = Bitmap.createBitmap(viewBitmap, 0, scrollY, viewToBeConverted.getWidth(), height - scrollY);
}
} catch (Exception ex) {
Log.e("PercolateAndroidUtils", "Could not remove top part of the webview image. ex=" + ex);
}
return viewBitmap;
}
/**
* Convert view to an image. Can be used to make animations smoother.
*
* @param context The current Context or Activity that this method is called from
* @param viewToBeConverted View to convert to a Bitmap
* @return Bitmap object that can be put in an ImageView. Will look like the converted viewToBeConverted.
*/
public static Bitmap viewToImage(Context context, WebView viewToBeConverted) {
int extraSpace = 2000; //because getContentHeight doesn't always return the full screen height.
int height = viewToBeConverted.getContentHeight() + extraSpace;
Bitmap viewBitmap = Bitmap.createBitmap(viewToBeConverted.getWidth(), height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap);
viewToBeConverted.draw(canvas);
//If the view is scrolled, cut off the top part that is off the screen.
try {
int scrollY = viewToBeConverted.getScrollY();
if (scrollY > 0) {
viewBitmap = Bitmap.createBitmap(viewBitmap, 0, scrollY, viewToBeConverted.getWidth(), height - scrollY);
}
} catch (Exception ex) {
Log.e("PercolateAndroidUtils", "Could not remove top part of the webview image. ex=" + ex);
}
return viewBitmap;
}
/**
* Convert view to an image. Can be used to make animations smoother.
*
* @param context The current Context or Activity that this method is called from.
* @param viewToBeConverted View to convert to a Bitmap.
* @return Bitmap object that can be put in an ImageView. Will look like the converted viewToBeConverted.
*/
public static Bitmap viewToImage(Context context, WebView viewToBeConverted) {
int extraSpace = 2000; //because getContentHeight doesn't always return the full screen height.
int height = viewToBeConverted.getContentHeight() + extraSpace;
Bitmap viewBitmap = Bitmap.createBitmap(viewToBeConverted.getWidth(), height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(viewBitmap);
viewToBeConverted.draw(canvas);
//If the view is scrolled, cut off the top part that is off the screen.
try {
int scrollY = viewToBeConverted.getScrollY();
if (scrollY > 0) {
viewBitmap = Bitmap.createBitmap(viewBitmap, 0, scrollY, viewToBeConverted.getWidth(), height - scrollY);
}
} catch (Exception ex) {
Log.e("Caffeine", "Could not remove top part of the webview image. ex=" + ex);
}
return viewBitmap;
}
@SuppressLint("WrongCall")
private Bitmap captureWebView2(@NonNull final WebView view) {
openCache(view);
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
view.measure(measureSpec, measureSpec);
if (view.getMeasuredWidth() <= 0 || view.getMeasuredHeight() <= 0) {
return null;
}
Bitmap bm;
try {
bm = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError e) {
System.gc();
try {
bm = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.RGB_565);
} catch (OutOfMemoryError ee) {
closeCache(view);
return captureWebView3(view);
}
}
Canvas bigCanvas = new Canvas(bm);
Paint paint = new Paint();
int iHeight = bm.getHeight();
bigCanvas.drawBitmap(bm, 0, iHeight, paint);
view.draw(bigCanvas);
closeCache(view);
return bm;
}
private static void createType2(WebView webView, FileHelper.OnSavedToGalleryListener listener) {
float scale = webView.getScale();
int webViewHeight = (int) (webView.getContentHeight() * scale + 0.5);
Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(), webViewHeight, Config.RGB_565);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);
try {
FileHelper.saveImageToGallery(PalmApp.getContext(), bitmap, true, listener);
bitmap.recycle();
} catch (Exception e) {
LogUtils.e(e.getMessage());
}
}
/**
* Create a picture out of {@link WebView}'s whole content
*
* @param webView The WebView to get contents from
* @return A {@link Bitmap} or null
*/
@Nullable
public static Bitmap getBitmapFromWebView(final WebView webView) {
try {
//Measure WebView's content
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
webView.measure(widthMeasureSpec, heightMeasureSpec);
webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight());
//Build drawing cache and store its size
webView.buildDrawingCache();
int measuredWidth = webView.getMeasuredWidth();
int measuredHeight = webView.getMeasuredHeight();
//Creates the bitmap and draw WebView's content on in
Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, bitmap.getHeight(), new Paint());
webView.draw(canvas);
webView.destroyDrawingCache();
return bitmap;
} catch (Exception | OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
/**
* Create a picture out of {@link WebView}'s whole content
*
* @param webView The WebView to get contents from
* @return A {@link Bitmap} or null
*/
@Nullable
public static Bitmap getBitmapFromWebView(final WebView webView) {
try {
//Measure WebView's content
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
webView.measure(widthMeasureSpec, heightMeasureSpec);
webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight());
//Build drawing cache and store its size
webView.buildDrawingCache();
int measuredWidth = webView.getMeasuredWidth();
int measuredHeight = webView.getMeasuredHeight();
//Creates the bitmap and draw WebView's content on in
Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, bitmap.getHeight(), new Paint());
webView.draw(canvas);
webView.destroyDrawingCache();
return bitmap;
} catch (Exception | OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}
/**
* Create a picture out of {@link WebView}'s whole content
*
* @param webView The WebView to get contents from
* @return A {@link Bitmap} or null
*/
@Nullable
public static Bitmap getBitmapFromWebView(WebView webView) {
try {
//Measure WebView's content
int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
webView.measure(widthMeasureSpec, heightMeasureSpec);
webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight());
//Build drawing cache and store its size
webView.buildDrawingCache();
int measuredWidth = webView.getMeasuredWidth();
int measuredHeight = webView.getMeasuredHeight();
//Creates the bitmap and draw WebView's content on in
Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawBitmap(bitmap, 0, bitmap.getHeight(), new Paint());
webView.draw(canvas);
webView.destroyDrawingCache();
return bitmap;
} catch (Exception | OutOfMemoryError e) {
e.printStackTrace();
return null;
}
}