Android实现图片转高斯模糊以及高斯模糊布局

Android实现图片转高斯模糊以及高斯模糊布局,第1张

概述第一个为大家介绍图片如何转高斯模拟:1.方法的实现:publicstaticvoidupdateBgToBlur(Activitya,BitmapbmpToBlur,Viewview,intresId){

第一个为大家介绍图片如何转高斯模拟:

1.方法的实现:

public static voID updateBgToBlur(Activity a,Bitmap bmpToBlur,VIEw vIEw,int resID) {    BitmapFactory.Options opt = new BitmapFactory.Options();    opt.inJustDecodeBounds = true;    opt.inSampleSize = 8;    opt.inJustDecodeBounds = false;    Bitmap bmp = BitmapFactory.decodeResource(a.getResources(),resID,opt);    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.JELLY_BEAN) {      vIEw.setBackground(null);    } else {      vIEw.setBackgroundDrawable(null);    }    if (bmpToBlur != null && !bmpToBlur.isRecycled()) {      bmpToBlur.recycle();    }    bmpToBlur = blurBitmap(a,bmp);    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {      vIEw.setBackground(new BitmapDrawable(a.getResources(),bmpToBlur));    } else {      vIEw.setBackgroundDrawable(new BitmapDrawable(a.getResources(),bmpToBlur));    }  }  public static Bitmap blurBitmap(Context c,Bitmap bitmap) {    //Let's create an empty bitmap with the same size of the bitmap we want to blur    Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWIDth(),bitmap.getHeight(),Bitmap.Config.ARGB_4444);    //Instantiate a new Renderscript    RenderScript rs = RenderScript.create(c.getApplicationContext());    //Create an Intrinsic Blur Script using the Renderscript    ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs,Element.U8_4(rs));    //Create the Allocations (in/out) with the Renderscript and the in/out bitmaps    Allocation allin = Allocation.createFromBitmap(rs,bitmap);    Allocation allOut = Allocation.createFromBitmap(rs,outBitmap);    //Set the radius of the blur    blurScript.seTradius(25.f);    //Perform the Renderscript    blurScript.setinput(allin);    blurScript.forEach(allOut);    //copy the final bitmap created by the out Allocation to the outBitmap    allOut.copyTo(outBitmap);    //recycle the original bitmap    bitmap.recycle();    //After finishing everything,we destroy the Renderscript.    rs.destroy();    return outBitmap;  }

2 调用:

 Bitmap bitmap=null;    if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {      ImageUtil.updateBgToBlur(getActivity(),bitmap,slIDingUpPanelLayout,R.drawable.bg_tageditor);    } else {      slIDingUpPanelLayout.setBackgroundResource(R.drawable.bg_tageditor);    }

二、高斯模糊布局:

项目需求: 现有一个紫色背景图片,相册图片覆盖在背景图片,一个Framlayout 覆盖在这个含有相册图片的背景图中,实现模糊盖在上面的高斯模拟效果:

1 引用BlurVIEw:

 compile 'com.eightbitlab:supportrenderscriptblur:1.0.0' compile 'com.eightbitlab:blurvIEw:1.3.3' defaultConfig {    renderscriptTargetAPI 25 //must match target sdk and build tools,23+    renderscriptSupportModeEnabled true }

2 .调用:

final float radius = 20;    final VIEw decorVIEw = getActivity().getwindow().getDecorVIEw();    //Activity's root VIEw. Can also be root VIEw of your layout (preferably)    final VIEwGroup rootVIEw = (VIEwGroup) decorVIEw.findVIEwByID(androID.R.ID.content);    //set background,if your root layout doesn't have one    final Drawable windowBackground = decorVIEw.getBackground();    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {      mBlurVIEw.setupWith(rootVIEw)          .windowBackground(windowBackground)          .blurAlgorithm(new RenderScriptBlur(getActivity()))          .blurRadius(radius);    }else {      mBlurVIEw.setupWith(rootVIEw)          .windowBackground(windowBackground)          .blurAlgorithm(new SupportRenderScriptBlur(getActivity()))          .blurRadius(radius);    }

3 xml

 <eightbitlab.com.blurvIEw.BlurVIEw   androID:ID="@+ID/blurVIEw"   androID:layout_wIDth="match_parent"   androID:layout_height="wrap_content"   app:blurOverlaycolor="@color/colorOverlay">    <!--Any child VIEw here,TabLayout for example--> </eightbitlab.com.blurvIEw.BlurVIEw>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程小技巧。

总结

以上是内存溢出为你收集整理的Android实现图片转高斯模糊以及高斯模糊布局全部内容,希望文章能够帮你解决Android实现图片转高斯模糊以及高斯模糊布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1144942.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存