Html.ImageGetter TextView

前端 未结 5 1686
猫巷女王i
猫巷女王i 2020-11-27 17:58

So Im using ImageGetter to display the images from JSON blog posts. Im getting the correct source in the log but the URL changes when it reaches setBounds. Any ideas?

<
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 18:26

    An alternative solution using Glide and Coroutines with the assumption that a retry is not required:

    import android.content.res.Resources
    import android.graphics.Bitmap
    import android.graphics.Canvas
    import android.graphics.drawable.BitmapDrawable
    import android.graphics.drawable.Drawable
    import android.text.Html
    import android.widget.TextView
    import androidx.lifecycle.LifecycleCoroutineScope
    import com.bumptech.glide.RequestManager
    import kotlinx.coroutines.Dispatchers
    import kotlinx.coroutines.launch
    import kotlinx.coroutines.withContext
    import kotlin.math.roundToInt
    
    class HtmlImageGetter(
        private val scope: LifecycleCoroutineScope,
        private val res: Resources,
        private val glide: RequestManager,
        private val htmlTextView: TextView
    ) : Html.ImageGetter {
    
        override fun getDrawable(url: String): Drawable {
            val holder = BitmapDrawablePlaceHolder(res, null)
    
            scope.launch(Dispatchers.IO) {
                runCatching {
                    val bitmap = glide
                        .asBitmap()
                        .load(url)
                        .submit()
                        .get()
    
                    val drawable = BitmapDrawable(res, bitmap)
    
                    val scale = 1.25 // This makes the image scale in size.
                    val width = (drawable.intrinsicWidth * scale).roundToInt()
                    val height = (drawable.intrinsicHeight * scale).roundToInt()
                    drawable.setBounds(0, 0, width, height)
    
                    holder.setDrawable(drawable)
                    holder.setBounds(0, 0, width, height)
    
                    withContext(Dispatchers.Main) { htmlTextView.text = htmlTextView.text }
                }
            }
    
            return holder
        }
    
        internal class BitmapDrawablePlaceHolder(res: Resources, bitmap: Bitmap?) : BitmapDrawable(res, bitmap) {
            private var drawable: Drawable? = null
    
            override fun draw(canvas: Canvas) {
                drawable?.run { draw(canvas) }
            }
    
            fun setDrawable(drawable: Drawable) {
                this.drawable = drawable
            }
        }
    }
    

    In a Fragment or Activity use with HtmlCompat

    val imageGetter = HtmlImageGetter(lifecycleScope, resources, glide, htmlTextView)
    val styledText = HtmlCompat.fromHtml(htmlString, flags, imageGetter, null)
    htmlTextView.text = styledText
    

提交回复
热议问题