调用 draw 方法后 Webview 不可用
Webview unusable after draw method called
我想提取 webview 屏幕截图(即使有滚动条)并保存。
我的问题是在调用 webview.draw()
方法后,webview 现在是一个图像,无法用作 webview。
如果有办法防止这种行为?
重要提示:我无法重新加载网络视图,因为用户输入了字段。
这是我的代码:
protected void onClicked() {
mWebView.measure(View.MeasureSpec.makeMeasureSpec(
View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
mWebView.layout(0, 0, mWebView.getMeasuredWidth(),
mWebView.getMeasuredHeight());
mWebView.setDrawingCacheEnabled(true);
mWebView.buildDrawingCache();
generateWebViewCapture();
}
/**
* Create capture of all webview.
*/
@Background
void generateWebViewCapture() {
Bitmap bm = Bitmap.createBitmap(mWebView.getMeasuredWidth(),
mWebView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
int iHeight = bm.getHeight();
canvas.drawBitmap(bm, 0, iHeight, paint);
mWebView.draw(canvas);
runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView.setDrawingCacheEnabled(false);
mWebView.invalidate();
}
});
try {
String path = getReportPath();
File file = new File(path, "/report-"+System.currentTimeMillis()+".png");
OutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
编辑:
要解决此问题,请删除该行:
mWebView.layout(0, 0, mWebView.getMeasuredWidth(),
mWebView.getMeasuredHeight());
获得 bitmap
!
后 mWebView.setDrawingCacheEnabled(false)
mWebView.setDrawingCacheEnabled(true);
// remove mWebView.buildDrawingCache();
Bitmap bm= mWebView.getDrawingCache();
//remove generateWebViewCapture();
try {
String path = getReportPath();
File file = new File(path, "/report-"+System.currentTimeMillis()+".png");
OutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
mWebView.setDrawingCacheEnabled(false);
此代码适用于我的手机:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testm);
final WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.loadUrl("http://www.csdn.net/");
findViewById(R.id.iv_0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWebView.setDrawingCacheEnabled(true);
// remove mWebView.buildDrawingCache();
Bitmap bm= mWebView.getDrawingCache();
//remove generateWebViewCapture();
try {
String path = getFilesDir().getPath();
File file = new File(path, "/report-"+System.currentTimeMillis()+".png");
OutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
mWebView.setDrawingCacheEnabled(false);
}
});
}
}
它不适用于 Android R.
WebView.enableSlowWholeDocumentDraw()
添加ScrollView,成功了。
<ScrollView
android:layout_marginStart="@dimen/margin_8"
android:layout_marginEnd="@dimen/margin_8"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
我想提取 webview 屏幕截图(即使有滚动条)并保存。
我的问题是在调用 webview.draw()
方法后,webview 现在是一个图像,无法用作 webview。
如果有办法防止这种行为?
重要提示:我无法重新加载网络视图,因为用户输入了字段。
这是我的代码:
protected void onClicked() {
mWebView.measure(View.MeasureSpec.makeMeasureSpec(
View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
mWebView.layout(0, 0, mWebView.getMeasuredWidth(),
mWebView.getMeasuredHeight());
mWebView.setDrawingCacheEnabled(true);
mWebView.buildDrawingCache();
generateWebViewCapture();
}
/**
* Create capture of all webview.
*/
@Background
void generateWebViewCapture() {
Bitmap bm = Bitmap.createBitmap(mWebView.getMeasuredWidth(),
mWebView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bm);
Paint paint = new Paint();
int iHeight = bm.getHeight();
canvas.drawBitmap(bm, 0, iHeight, paint);
mWebView.draw(canvas);
runOnUiThread(new Runnable() {
@Override
public void run() {
mWebView.setDrawingCacheEnabled(false);
mWebView.invalidate();
}
});
try {
String path = getReportPath();
File file = new File(path, "/report-"+System.currentTimeMillis()+".png");
OutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
}
编辑:
要解决此问题,请删除该行:
mWebView.layout(0, 0, mWebView.getMeasuredWidth(),
mWebView.getMeasuredHeight());
获得 bitmap
!
mWebView.setDrawingCacheEnabled(false)
mWebView.setDrawingCacheEnabled(true);
// remove mWebView.buildDrawingCache();
Bitmap bm= mWebView.getDrawingCache();
//remove generateWebViewCapture();
try {
String path = getReportPath();
File file = new File(path, "/report-"+System.currentTimeMillis()+".png");
OutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
mWebView.setDrawingCacheEnabled(false);
此代码适用于我的手机:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testm);
final WebView mWebView = (WebView) findViewById(R.id.webview);
mWebView.loadUrl("http://www.csdn.net/");
findViewById(R.id.iv_0).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mWebView.setDrawingCacheEnabled(true);
// remove mWebView.buildDrawingCache();
Bitmap bm= mWebView.getDrawingCache();
//remove generateWebViewCapture();
try {
String path = getFilesDir().getPath();
File file = new File(path, "/report-"+System.currentTimeMillis()+".png");
OutputStream fOut = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.PNG, 100, fOut);
fOut.flush();
fOut.close();
} catch (Exception e) {
e.printStackTrace();
}
mWebView.setDrawingCacheEnabled(false);
}
});
}
}
它不适用于 Android R.
WebView.enableSlowWholeDocumentDraw()
添加ScrollView,成功了。
<ScrollView
android:layout_marginStart="@dimen/margin_8"
android:layout_marginEnd="@dimen/margin_8"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>