鸿蒙UI开发——组件滤镜效果

1、概 述

ArkUI为组件提供了滤镜效果设置,背景滤镜、前景滤镜、合成滤镜。我们可以通过以下方式为组件设置对应的滤镜效果。

复制代码
Text('前景滤镜')// ..其他属性.foregroundFilter(filterTest1) // 通过 foregroundFilter 设置模糊效果Text('背景滤镜')// ...其他属性.backgroundFilter(filterTest2) // 通过 backgroundFilter 设置模糊效果Text('合成滤镜')// ...其他属性.compositingFilter(filterTest3) // 通过 compositingFilter 设置模糊效果

具体用法,下面展开介绍。

2、设置滤镜效果

foregroundFilter、backgroundFilter、compositingFilter三个属性的入参是Filter对象。Filter对象的创建方式如下:​​​​​​​

复制代码
import { uiEffect } from "@kit.ArkGraphics2D";// ....let filter : uiEffect.Filter = uiEffect.createFilter()

目前Filter对象暂时只能设置一种效果,blur模糊效果,接口如下:

复制代码
blur(blurRadius: number): Filter

我们创建好的Filter则可以传入到组件对应的属性中。

一个案例如下(有一个文本组件,有一张背景图片,分别设置三种滤镜效果):

    • 前景滤镜将文字进行模糊处理;

    • 背景对背景图片进行了处理;

    • 合成滤镜是对两者同时进行处理;

【注意:目前版本的IDE预览器中是看不到效果的,需要在真机或者模拟器中运行】

代码如下:​​​​​​​

复制代码
// xxx.etsimport { uiEffect } from '@kit.ArkGraphics2D';@Entry@Componentstruct FilterEffectExample {  @State filterTest1: uiEffect.Filter = uiEffect.createFilter().blur(10)  @State filterTest2: uiEffect.Filter = uiEffect.createFilter().blur(10)  @State filterTest3: uiEffect.Filter = uiEffect.createFilter().blur(10)  build() {    Column({ space: 15 }) {      Text('originalImage').fontSize(20).width('75%').fontColor('#DCDCDC')      Text('没有滤镜')        .width(100)        .height(100)        .backgroundColor('#ADD8E6')        .backgroundImage($r("app.media.test"))        .backgroundImageSize({ width: 100, height: 100 })      Text('foregroundFilter').fontSize(20).width('75%').fontColor('#DCDCDC')      Text('前景滤镜')        .width(100)        .height(100)        .backgroundColor('#ADD8E6')        .backgroundImage($r("app.media.test"))        .backgroundImageSize({ width: 100, height: 100 })        .foregroundFilter(this.filterTest1) // 通过 foregroundFilter 设置模糊效果      Text('backgroundFilter').fontSize(20).width('75%').fontColor('#DCDCDC')      Text('背景滤镜')        .width(100)        .height(100)        .backgroundColor('#ADD8E6')        .backgroundImage($r("app.media.test"))        .backgroundImageSize({ width: 100, height: 100 })        .backgroundFilter(this.filterTest2) // 通过 backgroundFilter 设置模糊效果      Text('compositingFilter').fontSize(20).width('75%').fontColor('#DCDCDC')      Text('合成滤镜')        .width(100)        .height(100)        .backgroundColor('#ADD8E6')        .backgroundImage($r("app.media.test"))        .backgroundImageSize({ width: 100, height: 100 })        .compositingFilter(this.filterTest3) // 通过 compositingFilter 设置模糊效果    }    .height('100%')    .width('100%')  }}
相关推荐
江城开朗的豌豆3 分钟前
小程序登录不迷路:一篇文章搞定用户身份验证
前端·javascript·微信小程序
aesthetician7 分钟前
React 19.2.0: 新特性与优化深度解析
前端·javascript·react.js
第二只羽毛8 分钟前
重载和继承的实践
java·开发语言
Django强哥11 分钟前
JSON Schema Draft-07 详细解析
javascript·算法·代码规范
FIN666822 分钟前
射频技术领域的领航者,昂瑞微IPO即将上会审议
前端·人工智能·前端框架·信息与通信
U.2 SSD32 分钟前
ECharts漏斗图示例
前端·javascript·echarts
江城开朗的豌豆32 分钟前
我的小程序登录优化记:从短信验证到“一键获取”手机号
前端·javascript·微信小程序
excel35 分钟前
Vue Mixin 全解析:概念、使用与源码
前端·javascript·vue.js
IT_陈寒42 分钟前
Java性能优化:这5个Spring Boot隐藏技巧让你的应用提速40%
前端·人工智能·后端
光军oi43 分钟前
全栈开发杂谈————JAVA微服务全套技术栈详解
java·开发语言·微服务