Android项目实战之Glide 高斯模糊效果的实例代码
核心需要高斯模糊的库
compile'jp.wasabeef:glide-transformations:2.0.1'
针对于3.7的版本
使用方法为:
//加载背景, Glide.with(MusicPlayerActivity.this) .load(service.getImageUri()) .dontAnimate() .error(R.drawable.no_music_rotate_img) //设置高斯模糊,模糊程度(最大25)缩放比例 .bitmapTransform(newBlurTransformation(this,14,3)) .into(allBg);
针对4.+
Glide.with(context) .load(url) .apply(RequestOptions.bitmapTransform(newBlurTransformation(25,8))) .into(view);
BlurTransformation类源码:
importandroid.graphics.Bitmap; importandroid.graphics.Canvas; importandroid.graphics.Paint; importandroid.support.annotation.NonNull; importcom.bumptech.glide.load.engine.bitmap_recycle.BitmapPool; importcom.bumptech.glide.load.resource.bitmap.BitmapTransformation; importjava.security.MessageDigest; importjp.wasabeef.glide.transformations.internal.FastBlur; publicclassBlurTransformationextendsBitmapTransformation{ privatestaticfinalintVERSION=1; privatestaticfinalStringID="BlurTransformation."+VERSION; privatestaticintMAX_RADIUS=25; privatestaticintDEFAULT_DOWN_SAMPLING=1; privateintradius; privateintsampling; publicBlurTransformation(){ this(MAX_RADIUS,DEFAULT_DOWN_SAMPLING); } publicBlurTransformation(intradius){ this(radius,DEFAULT_DOWN_SAMPLING); } publicBlurTransformation(intradius,intsampling){ this.radius=radius; this.sampling=sampling; } @Override protectedBitmaptransform(@NonNullBitmapPoolpool,@NonNullBitmaptoTransform,intoutWidth,intoutHeight){ intwidth=toTransform.getWidth(); intheight=toTransform.getHeight(); intscaledWidth=width/sampling; intscaledHeight=height/sampling; Bitmapbitmap=pool.get(scaledWidth,scaledHeight,Bitmap.Config.ARGB_8888); Canvascanvas=newCanvas(bitmap); canvas.scale(1/(float)sampling,1/(float)sampling); Paintpaint=newPaint(); paint.setFlags(Paint.FILTER_BITMAP_FLAG); canvas.drawBitmap(toTransform,0,0,paint); bitmap=FastBlur.blur(bitmap,radius,true); returnbitmap; } @OverridepublicStringtoString(){ return"BlurTransformation(radius="+radius+",sampling="+sampling+")"; } @Overridepublicbooleanequals(Objecto){ returnoinstanceofBlurTransformation&& ((BlurTransformation)o).radius==radius&& ((BlurTransformation)o).sampling==sampling; } @OverridepublicinthashCode(){ returnID.hashCode()+radius*1000+sampling*10; } @OverridepublicvoidupdateDiskCacheKey(@NonNullMessageDigestmessageDigest){ messageDigest.update((ID+radius+sampling).getBytes(CHARSET)); } }
总结
以上所述是小编给大家介绍的Android项目实战之Glide高斯模糊效果的实例代码,希望对大家有所帮助,如果大家有任何疑问欢迎给我留言,小编会及时回复大家的!