Created
July 9, 2019 20:16
-
-
Save jd-alexander/b9967ce6123f3078469944dbf5352921 to your computer and use it in GitHub Desktop.
This file contains 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
package com.automattic.freshlypressed.util | |
import android.content.Context | |
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 com.bumptech.glide.Glide | |
import com.bumptech.glide.request.Request | |
import com.bumptech.glide.request.RequestOptions | |
import com.bumptech.glide.request.target.SizeReadyCallback | |
import com.bumptech.glide.request.target.Target | |
import com.bumptech.glide.request.transition.Transition | |
class GlideImageGetter internal constructor(private val context: Context, private val textView: TextView) : Html.ImageGetter { | |
override fun getDrawable(url: String): Drawable { | |
val drawable = BitmapDrawablePlaceholder() | |
Glide.with(context) | |
.asBitmap() | |
.apply(RequestOptions().override(600, 200)) | |
.load(url) | |
.into(drawable) | |
return drawable | |
} | |
private inner class BitmapDrawablePlaceholder internal constructor() : BitmapDrawable(context.resources, Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888)), Target<Bitmap> { | |
private var bitmapDrawable: Drawable? = null | |
override fun draw(canvas: Canvas) { | |
if (bitmapDrawable != null) { | |
bitmapDrawable!!.draw(canvas) | |
} | |
} | |
private fun setDrawable(drawable: Drawable) { | |
this.bitmapDrawable = drawable | |
val drawableWidth = drawable.intrinsicWidth | |
val drawableHeight = drawable.intrinsicHeight | |
drawable.setBounds(0, 0, drawableWidth, drawableHeight) | |
setBounds(0, 0, drawableWidth, drawableHeight) | |
drawable.invalidateSelf() | |
textView.text = textView.text | |
} | |
override fun onLoadStarted(placeholderDrawable: Drawable?) { | |
if (placeholderDrawable != null) { | |
setDrawable(placeholderDrawable) | |
} | |
} | |
override fun onLoadFailed(errorDrawable: Drawable?) { | |
if (errorDrawable != null) { | |
setDrawable(errorDrawable) | |
} | |
} | |
override fun onResourceReady(bitmap: Bitmap, transition: Transition<in Bitmap>?) { | |
setDrawable(BitmapDrawable(context.resources, bitmap)) | |
} | |
override fun onLoadCleared(placeholderDrawable: Drawable?) { | |
if (placeholderDrawable != null) { | |
setDrawable(placeholderDrawable) | |
} | |
} | |
override fun getSize(cb: SizeReadyCallback) { | |
textView.post { cb.onSizeReady(textView.width, textView.height) } | |
} | |
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() {} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment