当前位置: 移动技术网 > 移动技术>移动开发>Android > Android实现图片转高斯模糊以及高斯模糊布局

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

2019年07月24日  | 移动技术网移动技术  | 我要评论

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

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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网