java - 将 webview 转换为位图的高度问题
问题描述
我正在将 webview 转换为位图进行打印,我的问题是当我单击 printButton 整个网页不打印时,仅打印设备高度剩余的将是第一次尝试打印的空白,再次单击打印按钮将打印整个网页。请告诉我在第一次尝试时打印整个 webview。我在下面提到了我使用的代码
protected void onCreate(Bundle savedInstanceState) {
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.Main-activity);
bt_print.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Bitmap bmp = getScreenBitmap();``
}
});
}
public Bitmap getScreenBitmap() {
final WebView webView = (WebView) findViewById(R.id.webview);
webView.setDrawingCacheEnabled(true);
webView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight());
webView.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(webView.getDrawingCache());
webView.setDrawingCacheEnabled(false); // clear drawing cache
return b;
}```
Below code is Main-activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="@style/AppTheme"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.fusion.print" >
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
<Button
android:id="@+id/btn_print"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#1C5C1C"
android:layout_alignParentBottom="true"
android:contentDescription="@string/btn_button"
android:text="@string/btn_button"
android:textColor="#ffffff"/>
</RelativeLayout>
解决方案
推荐阅读
- sql - 透视嵌套列(按列分组) - SQL Server
- grails - 服务器无法启动:没有这样的属性:类的 localHttpPort:org.grails.jetty.JettyServer
- javascript - 如何将 Webpack 4 SplitChunksPlugin 与 HtmlWebpackPlugin 一起用于多页应用程序?
- python - 无法从网站相应地获取两个字段
- url-rewriting - 为 Kentico 中特定页面类型的所有新实例设置 url 模式?
- node.js - Kubernetes 相当于 `docker run --init`
- r - 如何使用 docker run 更改容器内文件夹的权限?
- python - 在 Python 3 中使用和不使用 return() 时得到不同的结果
- ruby-on-rails - 触摸记录
- c# - 在c#中的winform应用程序中获取在OCR期间提取的第一个字母的坐标