欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android中实现iOS中的毛玻璃效果

程序员文章站 2023-11-12 11:37:34
为了实现毛玻璃效果,我们需要一组compute kernels(.rs文件中编写),及一组用于控制renderscript相关的javaapi(.rs文件自动生成为java...

为了实现毛玻璃效果,我们需要一组compute kernels(.rs文件中编写),及一组用于控制renderscript相关的javaapi(.rs文件自动生成为java类)。 由于compute kernels的编写需要一定的学习成本,从jelly_bean_mr1开始,androied内置了一些compute kernels用于常用的操作,其中就包括了gaussian blur。

下面,通过实操来讲解一下renderscript来实现高斯模糊,最终实现效果(讲文字背景进行模糊处理):

Android中实现iOS中的毛玻璃效果Android中实现iOS中的毛玻璃效果

实现代码:

<html><head> 
<meta http-equiv="content-type" content="text/html; charset=utf-8"></head><body><textarea style="width:99%;height:99%">private 
 void applyblur() { 
  image.getviewtreeobserver().addonpredrawlistener(new  
viewtreeobserver.onpredrawlistener() { 
    @override 
    public boolean onpredraw() { 
      image.getviewtreeobserver().removeonpredrawlistener(this); 
      image.builddrawingcache(); 
      bitmap bmp = image.getdrawingcache(); 
      blur(bmp, text, true); 
      return true; 
    } 
  }); 
} 
@targetapi(build.version_codes.jelly_bean_mr1) 
private void blur(bitmap bkg, view view) { 
  long startms = system.currenttimemillis(); 
  float radius = 20; 
  bitmap overlay = bitmap.createbitmap((int)(view.getmeasuredwidth()), 
 (int)(view.getmeasuredheight()), bitmap.config.argb_8888); 
  canvas canvas = new canvas(overlay); 
  canvas.translate(-view.getleft(), -view.gettop()); 
  canvas.drawbitmap(bkg, 0, 0, null); 
  renderscript rs = renderscript.create(secondactivity.this); 
  allocation overlayalloc = allocation.createfrombitmap(rs, overlay); 
  scriptintrinsicblur blur = scriptintrinsicblur.create(rs,  
overlayalloc.getelement()); 
  blur.setinput(overlayalloc); 
  blur.setradius(radius); 
  blur.foreach(overlayalloc); 
  overlayalloc.copyto(overlay); 
  view.setbackground(new bitmapdrawable(getresources(), overlay)); 
  rs.destroy(); 
  statustext.settext("cost " + (system.currenttimemillis() - startms) + 
 "ms"); 
}</textarea></body></html> 

布局如下:

<?xml version="1.0" encoding="utf-8"?>
<framelayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
  <imageview 
    android:id="@+id/picture" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:src="@drawable/splash" 
    android:scaletype="centercrop" />
  <textview 
    android:id="@+id/text"
    android:gravity="center_horizontal" 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="gaussian blur"
    android:textcolor="@android:color/black"
    android:layout_gravity="center_vertical"
    android:textstyle="bold"
    android:textsize="48sp" />
  <linearlayout 
    android:id="@+id/controls" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:background="#7f000000" 
    android:orientation="vertical"
    android:layout_gravity="bottom" />
</framelayout>

以上所述是小编给大家介绍的android中实现ios中的毛玻璃效果,希望对大家有所帮助