问题
I use CrossWalk to display webpage, it works well. I have a feature to capture the whole content as bitmap to save into SDCard, but I cannot found a solution. Using TextureView can only capture a screen size bitmap, anyone has the same issue? BTW, I can capture the whole content using WebView.draw().
回答1:
Here's some pseudo code that may help. You can get the content height and width from the webview and then you can leverage webview.draw()...so something like the following.
Bitmap bitmap = Bitmap.createBitmap( webView.getWidth(), webView.getContentHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);
Then you can output your bitmap file using bitmap.compress and output it to a file output stream
//fos is a FileOutputStream, you can use something else if needed.
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
The only issue I foresee with this is that your bitmap may clip the content horizontally since WebView doesn't have a method to get the content width. Otherwise I think this should work.
回答2:
I never using crosswalk before, but I'm guessing the Crosswalk WebView is also descendant from android.view.View. If so, then you could use android view drawing cache. It look like this
yourWebView.setDrawingCacheEnabled(true);
Bitmap bmp = yourWebView.getDrawingCache();
Then as Paul said, save it through FileOutputStream
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fos);
回答3:
You can't capture XWalkView image by standard ways as described in answers above because Crosswalk XWalkView works with hardware layer (i.e. SurfaceView or TextureView). But you can use getBitmap() method of TextureView class to get it.
In short, you should enable using of TextureView in Crosswalk before XWalkView will be initialized (in Application subclass for example) with:
XWalkPreferences.setValue(XWalkPreferences.ANIMATABLE_XWALK_VIEW, true);
And find TextureView in views tree to call getBitmap() on it.
Something like that:
/**
* Returns TextureView which is used in XWalkView
*
* @param group
* @return
*/
private TextureView findXWalkTextureView(ViewGroup group) {
int childCount = group.getChildCount();
for (int i = 0; i < childCount; i++) {
View child = group.getChildAt(i);
if (child instanceof TextureView) {
String parentClassName = child.getParent().getClass().toString();
boolean isRightKindOfParent = (parentClassName.contains("XWalk"));
if (isRightKindOfParent) {
return (TextureView) child;
}
} else if (child instanceof ViewGroup) {
TextureView textureView = findXWalkTextureView((ViewGroup) child);
if (textureView != null) {
return textureView;
}
}
}
return null;
}
/**
* Example of capturing image from XWalkView based on TextureView
*
* @return
*/
public Bitmap captureImage() {
if (mXWalkView != null) {
Bitmap bitmap = null;
boolean isCrosswalk = false;
try {
Class.forName("org.xwalk.core.XWalkView");
isCrosswalk = true;
} catch (Exception e) {
e.printStackTrace();
}
if (isCrosswalk) {
try {
TextureView textureView = findXWalkTextureView(mXWalkView);
bitmap = textureView.getBitmap();
} catch (Exception e) {
e.printStackTrace();
}
} else {
bitmap = Bitmap.createBitmap(mXWalkView.getWidth(), mXWalkView.getHeight(), Bitmap.Config.ARGB_8888);
Canvas c = new Canvas(bitmap);
mXWalkView.draw(c);
}
return bitmap;
} else {
return null;
}
}
Check this code: https://github.com/gitawego/cordova-screenshot/blob/master/src/android/Screenshot.java for more details.
来源:https://stackoverflow.com/questions/29483028/how-can-i-capture-the-whole-view-into-a-bitmap-when-using-crosswalk-to-display-w