Skip to content

Instantly share code, notes, and snippets.

@umpteenthdev
Created January 21, 2019 09:08
Show Gist options
  • Select an option

  • Save umpteenthdev/f9c944e97c34036745c694bbb756d384 to your computer and use it in GitHub Desktop.

Select an option

Save umpteenthdev/f9c944e97c34036745c694bbb756d384 to your computer and use it in GitHub Desktop.
Glide blur transformation
import android.content.Context
import android.graphics.Bitmap
import android.renderscript.Allocation
import android.renderscript.Element
import android.renderscript.RenderScript
import android.renderscript.ScriptIntrinsicBlur
import com.bumptech.glide.load.Key
import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation
import java.security.MessageDigest
class BlurTransformation(context: Context) : BitmapTransformation() {
private val renderScript = RenderScript.create(context)
override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap {
val blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true)
val input = Allocation.createFromBitmap(
renderScript,
blurredBitmap,
Allocation.MipmapControl.MIPMAP_FULL,
Allocation.USAGE_SHARED
)
val output = Allocation.createTyped(renderScript, input.type)
with(ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript))) {
setInput(input)
setRadius(25f)
forEach(output)
}
output.copyTo(blurredBitmap)
return blurredBitmap
}
override fun updateDiskCacheKey(messageDigest: MessageDigest) {
messageDigest.update(ID_BYTES)
}
companion object {
private const val ID = "ru.napoleonit.youfix.lib.glide.BlurTransformation"
private val ID_BYTES = ID.toByteArray(Key.CHARSET)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment