【发布时间】:2019-10-06 04:03:12
【问题描述】:
我的应用生成大文本字符串,我希望能够允许用户在他们选择时将它们输出为图像。
我使用 webview 来显示字符串,因为它允许嵌入锚点,处理长字符串中的新行并以非常简洁的方式显示所有内容。
在我简单地将 webview 转换为位图之前。这很好用,但现在不再有效。我对实现目标的替代方法持开放态度(虽然我是一个免费应用程序的业余开发者,所以如果我遗漏了一些公然的东西,我深表歉意)
旧代码保存了 webview 的完整内容,包括屏幕外的内容。新代码(尽管在 Identical 附近没有)。问题似乎是 webview 的高度没有正确报告(如果我添加到高度然后它可以工作,但由于我不知道字符串的大小我不能总是预测高度 *见下文)
我尝试了 PixelShot 库,这提出了同样的问题。
在计算位图的高度时,我尝试添加 webview 的高度……这显示了进展,但不是一个好的解决方法,因为我们不知道文本块的高度。 (font height x each
听起来不错,但有些句子很长,因此高度会被截断)
我尝试完全使用旧方法,但某些代码已贬值
我尝试将 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
这与生成的内容是一行还是两页/屏幕的内容无关。
(当我阅读时,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 天了,我就是想不通
【问题讨论】:
标签: android kotlin webview bitmap height