[英]How do I turn all of a webview content into a bitmap?
我的應用程序生成大文本字符串,我希望能夠允許用戶在他們選擇時將它們輸出為圖像。
我使用 webview 來顯示字符串,因為它允許嵌入錨點,處理長字符串中的新行並以非常簡潔的方式顯示所有內容。
在我簡單地將 webview 轉換為位圖之前。 這很有效,但現在不再有效。 我對實現我的目標的其他方法持開放態度(盡管我是免費應用程序的業余開發者,所以如果我遺漏了一些明顯的東西,我深表歉意)
舊代碼保存了 webview 的完整內容,包括屏幕外的內容。 新代碼(盡管接近 Identical 沒有)。 問題似乎是 webview 的高度沒有正確報告(如果我添加到高度然后它工作,但由於我不知道字符串的大小,我不能總是預測高度 * 見下文)
我嘗試了 PixelShot 庫,這出現了同樣的問題。 在計算位圖的高度時,我嘗試添加到 webview 的高度......這顯示了進展,但不是一個好的解決方法,因為我們不知道文本塊的高度。 (字體高度 x 每
聽起來不錯,但有些句子很長,因此高度會被切斷)我嘗試完全使用舊方法,但有些代碼已貶值我曾嘗試將 webview 包含在滾動視圖中
fun displayResults(input:String){
var visabilityBackground = PreferenceManager.getDefaultSharedPreferences(context).getBoolean("webPageVis",false)
if (visabilityBackground) {generator_webview.setBackgroundColor(Color.WHITE) }else{generator_webview.setBackgroundColor(Color.TRANSPARENT)}
generator_webview.loadUrl("about:blank")
generator_webview.settings.javaScriptEnabled
generator_webview.loadUrl("about:blank")
var output=input
.replace("/n","<br>")
lastweb=input
.replace("/n",System.lineSeparator()) // not working....arghhh todo fix later or threaten ide with deletion
generator_webview.loadData(output,"text/html", "UTF-8")
Log.d("height","height : "+generator_webview.height)
Log.d("height","height measured : "+generator_webview.measuredHeight)
Log.d("height","height content: "+generator_webview.contentHeight)
Log.d("height","height scroll: "+generator_webview.scrollView().height)
Log.d("height","height min: "+generator_webview.minimumHeight)
Log.d("height","height s: "+generator_scrollview.height)
Log.d("height","height s measured: "+generator_scrollview.measuredHeight)
Log.d("height","height s min: "+generator_scrollview.minimumHeight)
}
高度日志的結果始終如下:
height: height : 2116
height: height measured : 2116
height: height content: 605
height: height scroll: 0
height: height min: 0
height: height s: 2116
height: height s measured: 2116
height: height s min: 0
這與生成的內容是一行還是 2 頁/屏幕內容無關。
(當我閱讀時,webview 目前被包含在滾動視圖中,這將解決問題......它沒有)
制作位圖的代碼目前是:
fun screenshot (webView:WebView):Bitmap?{
try {
val bitmap = Bitmap.createBitmap(webView.width, webView.height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(-0x1)
webView.draw(canvas)
return bitmap
}
catch (e: Exception) {
e.printStackTrace()
}
return null
}
這在別處被調用,傳遞 webview id webView.measuredHeight .contentHeight
webview 是用以下方法創建的:
<ScrollView
android:id="@+id/generator_scrollview"
android:layout_width="0dp"
android:layout_height="0dp"
android:fillViewport="true"
app:layout_constraintTop_toBottomOf="@+id/generator_adView"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="16dp"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="@id/generator_button_generate"
android:layout_marginEnd="16dp">
<WebView
android:id="@+id/generator_webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawingCacheQuality="high"
android:saveEnabled="true">
</WebView>
</ScrollView>
我也試過它沒有在滾動視圖中嵌入(它的高度和寬度設置為 0dp,因為它擴展以適應空間......根據新指南)
預期結果是一個位圖,其中包含視圖的全部內容,包括屏幕之外的內容。 高度未正確報告是 100% 的問題(我正在使用 WebView.enableSlowWholeDocumentDraw() 因此呈現完整視圖(如圖所示,當我添加到高度並能夠保存更高的圖像位圖時顯示的內容...正在渲染位圖只是沒有獲得正確的高度。
我已經在這里呆了 3 天了,我就是想不通
我們遇到了類似的問題,解決方案是將 webview 放在您在問題中提到的ScrollView
中,也許下面的layout_height
有一些不同。
這是解決我們問題的視圖 XML:
<ScrollView android:layout_width="match_parent" android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp">
<WebView
android:id="@+id/webView"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</WebView>
</LinearLayout>
</ScrollView>
我實際上在側面的相關鏈接中找到了一個解決方案.....似乎更像是一種解決方法而不是解決方案,但它有效,所以我很高興
fun screenshot (webView:WebView):Bitmap?{
webView.measure(View.MeasureSpec.makeMeasureSpec(
View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
webView.layout(0, 0, webView.measuredWidth, webView.measuredHeight)
try {
val bitmap = Bitmap.createBitmap(webView.width, webView.measuredHeight,
Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
canvas.drawColor(-0x1)
webView.draw(canvas)
return bitmap
}
catch (e: Exception) {
e.printStackTrace()
}
return null
}
這是從如何在android中獲取Web View的全部內容以生成PDF文件?
並且更改意味着代碼現在可以工作。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.