Last active
October 16, 2023 05:48
-
-
Save iamriajul/e0026c1c8e1783e6ffa2da511d1d4f37 to your computer and use it in GitHub Desktop.
Custom Html.ImageGetter to load images in TextView HTML tags using Glide
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Based on java code by Yaser Rajabi https://gist.github.com/yrajabi | |
*/ | |
package org.dailyislam.android.utils.extenstions | |
import android.graphics.Bitmap | |
import android.graphics.Canvas | |
import android.graphics.drawable.BitmapDrawable | |
import android.graphics.drawable.Drawable | |
import android.text.Html.ImageGetter | |
import android.widget.TextView | |
import com.bumptech.glide.Glide | |
import com.bumptech.glide.request.Request | |
import com.bumptech.glide.request.target.SizeReadyCallback | |
import com.bumptech.glide.request.target.Target | |
import com.bumptech.glide.request.transition.Transition | |
import java.lang.ref.WeakReference | |
class GlideImageGetter( | |
textView: TextView, | |
private val matchParentWidth: Boolean = false, | |
densityAware: Boolean = false, | |
private val imagesHandler: HtmlImagesHandler? = null | |
) : ImageGetter { | |
private val container: WeakReference<TextView> = WeakReference(textView) | |
private var density = 1.0f | |
init { | |
if (densityAware) { | |
container.get()?.let { | |
density = it.resources.displayMetrics.density | |
} | |
} | |
} | |
override fun getDrawable(source: String): Drawable { | |
imagesHandler?.addImage(source) | |
val drawable = BitmapDrawablePlaceholder() | |
// Load Image to the Drawable | |
container.get()?.apply { | |
post { | |
Glide.with(context) | |
.asBitmap() | |
.load(source) | |
.into(drawable) | |
} | |
} | |
return drawable | |
} | |
private inner class BitmapDrawablePlaceholder : BitmapDrawable(container.get()?.resources, Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)), Target<Bitmap> { | |
private var drawable: Drawable? = null | |
set(value) { | |
field = value | |
value?.let { drawable -> | |
val drawableWidth = (drawable.intrinsicWidth * density).toInt() | |
val drawableHeight = (drawable.intrinsicHeight * density).toInt() | |
val maxWidth = container.get()!!.measuredWidth | |
if (drawableWidth > maxWidth || matchParentWidth) { | |
val calculatedHeight = maxWidth * drawableHeight / drawableWidth | |
drawable.setBounds(0, 0, maxWidth, calculatedHeight) | |
setBounds(0, 0, maxWidth, calculatedHeight) | |
} else { | |
drawable.setBounds(0, 0, drawableWidth, drawableHeight) | |
setBounds(0, 0, drawableWidth, drawableHeight) | |
} | |
container.get()?.text = container.get()?.text | |
} | |
} | |
override fun draw(canvas: Canvas) { | |
drawable?.draw(canvas) | |
} | |
override fun onLoadStarted(placeholderDrawable: Drawable?) { | |
placeholderDrawable?.let { | |
drawable = it | |
} | |
} | |
override fun onLoadFailed(errorDrawable: Drawable?) { | |
errorDrawable?.let { | |
drawable = it | |
} | |
} | |
override fun onResourceReady(bitmap: Bitmap, transition: Transition<in Bitmap>?) { | |
drawable = BitmapDrawable(container.get()!!.resources, bitmap) | |
} | |
override fun onLoadCleared(placeholderDrawable: Drawable?) { | |
placeholderDrawable?.let { | |
drawable = it | |
} | |
} | |
override fun getSize(sizeReadyCallback: SizeReadyCallback) { | |
sizeReadyCallback.onSizeReady(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) | |
} | |
override fun removeCallback(cb: SizeReadyCallback) {} | |
override fun setRequest(request: Request?) {} | |
override fun getRequest(): Request? { | |
return null | |
} | |
override fun onStart() {} | |
override fun onStop() {} | |
override fun onDestroy() {} | |
} | |
interface HtmlImagesHandler { | |
fun addImage(uri: String?) | |
} | |
} |
You're Welcome 😊.
@iamriajul Svg format not supported. glide throwing error.
@iamriajul Svg format not supported. glide throwing error.
You may need to use SvgDrawableTranscoder
Check the Issue here: bumptech/glide#1239
@iamriajul
Hi, I am newly using this ImageGetter. My images within HTML shown in textview cannot display in the format of their correct sizes.
Could you offer a solution to display different sizes of images within HTML in TextView? Thanksss!!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for writing this. Very helpful