|
|
@ -13,16 +13,23 @@ import com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool |
|
|
|
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation |
|
|
|
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation |
|
|
|
|
|
|
|
|
|
|
|
import java.security.MessageDigest |
|
|
|
import java.security.MessageDigest |
|
|
|
|
|
|
|
import kotlin.math.min |
|
|
|
|
|
|
|
import kotlin.math.roundToInt |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* 模糊 |
|
|
|
* 模糊 |
|
|
|
|
|
|
|
* @radius: 0..25 |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
class BlurTransformation(val context: Context, private val radius: Int) : BitmapTransformation() { |
|
|
|
class BlurTransformation(context: Context, private val radius: Int) : BitmapTransformation() { |
|
|
|
|
|
|
|
private val rs: RenderScript = RenderScript.create(context) |
|
|
|
|
|
|
|
|
|
|
|
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
|
|
|
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) |
|
|
|
override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap { |
|
|
|
override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap { |
|
|
|
val blurredBitmap = toTransform.copy(Bitmap.Config.ARGB_8888, true) |
|
|
|
//图片缩小1/2 |
|
|
|
val rs: RenderScript = RenderScript.create(context) |
|
|
|
val width = (min(outWidth, toTransform.width) / 2f).roundToInt() |
|
|
|
|
|
|
|
val height = (min(outHeight, toTransform.height) / 2f).roundToInt() |
|
|
|
|
|
|
|
val blurredBitmap = Bitmap.createScaledBitmap(toTransform, width, height, false); |
|
|
|
// Allocate memory for Renderscript to work with |
|
|
|
// Allocate memory for Renderscript to work with |
|
|
|
//分配用于渲染脚本的内存 |
|
|
|
//分配用于渲染脚本的内存 |
|
|
|
val input = Allocation.createFromBitmap( |
|
|
|
val input = Allocation.createFromBitmap( |
|
|
@ -39,7 +46,7 @@ class BlurTransformation(val context: Context, private val radius: Int) : Bitmap |
|
|
|
script.setInput(input) |
|
|
|
script.setInput(input) |
|
|
|
|
|
|
|
|
|
|
|
// Set the blur radius |
|
|
|
// Set the blur radius |
|
|
|
//设置模糊半径 |
|
|
|
//设置模糊半径0..25 |
|
|
|
script.setRadius(radius.toFloat()) |
|
|
|
script.setRadius(radius.toFloat()) |
|
|
|
|
|
|
|
|
|
|
|
// Start the ScriptIntrinsicBlur |
|
|
|
// Start the ScriptIntrinsicBlur |
|
|
@ -49,7 +56,6 @@ class BlurTransformation(val context: Context, private val radius: Int) : Bitmap |
|
|
|
// Copy the output to the blurred bitmap |
|
|
|
// Copy the output to the blurred bitmap |
|
|
|
//将输出复制到模糊的位图 |
|
|
|
//将输出复制到模糊的位图 |
|
|
|
output.copyTo(blurredBitmap) |
|
|
|
output.copyTo(blurredBitmap) |
|
|
|
rs.destroy() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return blurredBitmap |
|
|
|
return blurredBitmap |
|
|
|
} |
|
|
|
} |
|
|
|