android动态高斯模糊,Android高斯模糊实现方案

1、运用Glide

Glide.with(this)

.load(service.getImageUri())

.dontAnimate()

.error(R.drawable.error_img)//设置高斯含糊.bitmapTransform(newBlurTransformation(this,14,3))

.into(imageview);

适用场景:动态配置的背景图片27522de87f233dfef214c3f7ac93d13f.png

2、对图片高斯含糊,需求先将图片转成bitmap目标

mportandroid.annotation.TargetApi;

importandroid.content.Context;

importandroid.graphics.Bitmap;

importandroid.os.Build;

importandroid.renderscript.Allocation;

importandroid.renderscript.Element;

importandroid.renderscript.RenderScript;

importandroid.renderscript.ScriptIntrinsicBlur;publicclassBlurBitmapUtil{//图片缩放比例(即含糊度)privatestaticfinalfloatBITMAP_SCALE=0.4f;/**

*@paramcontext上下文目标

*@paramimage需求含糊的图片

*@return含糊处理后的Bitmap*/@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)publicstaticBitmapblurBitmap(Contextcontext,Bitmapimage,floatblurRadius){//核算图片缩小后的长宽intwidth=Math.round(image.getWidth()*BITMAP_SCALE);intheight=Math.round(image.getHeight()*BITMAP_SCALE);//将缩小后的图片做为预烘托的图片BitmapinputBitmap=Bitmap.createScaledBitmap(image,width,height,false);//创立一张烘托后的输出图片BitmapoutputBitmap=Bitmap.createBitmap(inputBitmap);//创立RenderScript内核目标RenderScriptrs=RenderScript.create(context);//创立一个含糊作用的RenderScript的东西目标ScriptIntrinsicBlurblurScript=ScriptIntrinsicBlur.create(rs,Element.U8_4(rs));//因为RenderScript并没有运用VM来分配内存,所以需求运用Allocation类来创立和分配内存空间//创立Allocation目标的时分其实内存是空的,需求运用copyTo()将数据填充进去AllocationtmpIn=Allocation.createFromBitmap(rs,inputBitmap);

AllocationtmpOut=Allocation.createFromBitmap(rs,outputBitmap);//设置烘托的含糊程度,25f是最大含糊度blurScript.setRadius(blurRadius);//设置blurScript目标的输入内存blurScript.setInput(tmpIn);//将输出数据保存到输出内存中blurScript.forEach(tmpOut);//将数据填充到Allocation中tmpOut.copyTo(outputBitmap);returnoutputBitmap;

}

}

不引荐:运用bitmap,频繁操作的话比较耗性能。

3、运用高斯含糊遮罩,可以对指定区域进行含糊,不需求处理单张图片(引荐!!)

引荐一个github上的项目,亲测有效。https://github.com/mmin18/RealtimeBlurView

android:id=”@+id/blurview”android:layout_width=”match_parent”android:layout_height=”210dp”android:visibility=”gone”app:realtimeBlurRadius=”5dp”app:realtimeOverlayColor=”#00000000″/>