package com.xty.common

import android.app.Activity
import android.content.Context
import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.graphics.drawable.LevelListDrawable
import android.text.Html
import android.widget.TextView
import com.bumptech.glide.Glide
import com.bumptech.glide.request.target.SimpleTarget
import com.bumptech.glide.request.transition.Transition


/**
 * textView  加载带img标签的html文字
 */
class URLImageParser(var v: TextView,var context: Context) : Html.ImageGetter {
    override fun getDrawable(source: String?): Drawable {
        val drawable = LevelListDrawable()
        Glide.with(context ).asBitmap().load(source).into(object : SimpleTarget<Bitmap?>() {
            override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap?>?) {
                if (resource != null) {
                    val bitmapDrawable = BitmapDrawable(resource)
                    drawable.addLevel(1, 1, bitmapDrawable)
                    drawable.setBounds(0, 0, resource.getWidth(), resource.getHeight())
                    drawable.setLevel(1)
                    //防止图文重叠
                    v.invalidate()
                    v.setText(v.getText())
                }
            }
        })
        return drawable
    }

}