如何更改通过 Html.fromHtml(text) Spanned 在 TextView 中加载的默认图像(占位符)

GLA*_*ile 5 html java android spanned kotlin

我的应用程序中有一个新闻部分,它从我的网站加载一些新闻,其中一些包含图像,所以我从互联网加载它们。但是当图像没有加载时,有一个绿色方块只有在图像加载时才会消失。

图片未加载:

小绿广场

然后加载图像:

图片已加载

我想让那个绿色方块不可见。

为简单起见,假设我什至不加载图像,只是想让绿色方块不可见,而不用空文本替换图像标签。

代码:

val exampleText =  "Example <br> <img src=\"https://www.w3schools.com/images/w3schools_green.jpg\" alt=\"W3Schools.com\"> <br> Example"
    tv_body.text = fromHtml(exampleText)

fun fromHtml(html: String?): Spanned? {
    return if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        Html.fromHtml(html, Html.FROM_HTML_MODE_LEGACY);
    } else {
        Html.fromHtml(html);
    }
}
Run Code Online (Sandbox Code Playgroud)

有没有办法在不做任何恶作剧的情况下更改默认图像?

我的解决方法:

我为解决这个问题所做的是调整自定义 fromHtml 函数。

private var drawable: Drawable? = null
fun fromHtml(context: Activity?, tv: TextView?, text: String?) {
    if (TextUtils.isEmpty(text) || context == null || tv == null) return

   //Replace all image tags with an empty text
    val noImageText = text!!.replace("<img.*?>".toRegex(), "") 

        //Set the textview text with the imageless html
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            tv.text = Html.fromHtml(noImageText, Html.FROM_HTML_MODE_LEGACY)
        } else {
            tv.text = Html.fromHtml(noImageText)
        }

        Thread {
            //Creating the imageGetter
            val imageGetter = ImageGetter { url ->
                drawable = getImageFromNetwork(url)

                if (drawable != null) {
                    var w = drawable!!.intrinsicWidth
                    var h = drawable!!.intrinsicHeight
                    // Scaling the width and height
                    if (w < h && h > 0) {
                        val scale = 400.0f / h
                        w = (scale * w).toInt()
                        h = (scale * h).toInt()
                    } else if (w > h && w > 0) {
                        val scale = 1000.0f / w
                        w = (scale * w).toInt()
                        h = (scale * h).toInt()
                    }
                    drawable!!.setBounds(0, 0, w, h)
                } else if (drawable == null) {
                    return@ImageGetter null
                }
                drawable!!
            }


            val textWithImage = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                Html.fromHtml(text, Html.FROM_HTML_MODE_LEGACY, imageGetter, null)
            } else {
                Html.fromHtml(text, imageGetter, null)
            }

            // update runOnUiThread and change the textview text from the textWithoutImage to the textWithImage
            context.runOnUiThread(Runnable { tv.text = textWithImage })
        }.start()
}

private fun getImageFromNetwork(imageUrl: String): Drawable? {
    var myFileUrl: URL? = null
    var drawable: Drawable? = null
    try {
        myFileUrl = URL(imageUrl)
        val conn = myFileUrl
                .openConnection() as HttpURLConnection
        conn.doInput = true
        conn.connect()
        val `is` = conn.inputStream
        drawable = Drawable.createFromStream(`is`, null)
        `is`.close()
    } catch (e: Exception) {
        e.printStackTrace()
        return null
    }
    return drawable
}
Run Code Online (Sandbox Code Playgroud)

所以当我打电话给这个

  val exampleText =  "Example <br> <img src=\"https://www.w3schools.com/images/w3schools_green.jpg\" alt=\"W3Schools.com\"> <br> Example"
    fromHtml((activity as NewsActivity?), tv_body, exampleText)
Run Code Online (Sandbox Code Playgroud)

它首先显示了这一点:

无图像文本视图

(因为我用空文本替换了图像标签)

然后当图像加载它显示这个:

图像正确显示

我仍然认为制作无图像文本更像是一种解决方法而不是正确的解决方案,我认为可能有一些更简单的方法,例如:

<style name="LIGHT" parent="Theme.AppCompat.DayNight.DarkActionBar">
    <item name="android:placeHolderDefaultImage">@drawable/invisible</item>
Run Code Online (Sandbox Code Playgroud)

所以绿色方块将是一个不可见的可绘制对象,我不需要两次设置 html 文本,尽管我真的不知道如何更改默认的占位符图像。猜猜我会坚持解决方法