android 中毛玻璃效果(背景虚化)的实现

本文介绍了在Android中实现背景虚化(毛玻璃效果)的三种方法:RenderScript、FastBlur和AdvancedFastBlur。重点对比了它们的性能和适用场景,其中AdvancedFastBlur在保持效果的同时,显著提高了处理速度。对于API版本在16以上的设备,建议使用RenderScript,而FastBlur则适合低版本设备,但需注意内存管理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

搜集很多关于背景虚化的帖子, @IcyFox 的见解 : 模糊实现方案探究

1. RenderScript

谈到高斯模糊,第一个想到的就是RenderScript。RenderScript是由Android3.0引入,用来在Android上编写高性能代码的一种语言(使用C99标准)。 引用官方文档的描述:

RenderScript runtime will parallelize work across all processors available on a device, such as multi-core CPUs, GPUs, or DSPs, allowing you to focus on expressing algorithms rather than scheduling work or load balancing.

为了在Android中使用RenderScript,我们需要(直接贴官方文档,比直译更通俗易懂):

  • High-performance compute kernels are written in a C99-derived language.
  • A Java API is used for managing the lifetime of RenderScript resources and controlling kernel execution.

学习文档:http://developer.android.com/guide/topics/renderscript/compute.html

上面两点总结成一句话为:我们需要一组compute kernels(.rs文件中编写),及一组用于控制renderScript相关的java api(.rs文件自动生成为java类)。 由于compute kernels的编写需要一定的学习成本,从JELLY_BEAN_MR1开始,Androied内置了一些compute kernels用于常用的操作,其中就包括了Gaussian blur

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

布局:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3. android:layout_width="match_parent"  
  4. android:layout_height="match_parent" >  
  5.   
  6.     <ImageView   
  7.         android:id="@+id/picture"   
  8.         android:layout_width="match_parent"   
  9.         android:layout_height="match_parent"   
  10.         android:src="@drawable/splash"   
  11.         android:scaleType="centerCrop" />  
  12.   
  13.     <TextView   
  14.         android:id="@+id/text"  
  15.         android:gravity="center_horizontal"   
  16.         android:layout_width="match_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:text="Gaussian Blur"  
  19.         android:textColor="@android:color/black"  
  20.         android:layout_gravity="center_vertical"  
  21.         android:textStyle="bold"  
  22.         android:textSize="48sp" />  
  23.   
  24.     <LinearLayout   
  25.         android:id="@+id/controls"   
  26.         android:layout_width="match_parent"
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值