首页 > 解决方案 > 将 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>

标签: javaandroid

解决方案


推荐阅读