鸿蒙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%')  }}
相关推荐
tedcloud1233 小时前
UI-TARS-desktop部署教程:构建AI桌面自动化系统
服务器·前端·人工智能·ui·自动化·github
jerryinwuhan5 小时前
基于各城市站点流量的复合功能比较
开发语言·php
UXbot6 小时前
AI原型设计工具如何支持团队协作与快速迭代
前端·交互·个人开发·ai编程·原型模式
迈巴赫车主6 小时前
Java基础:list、set、map一遍过
java·开发语言
ZC跨境爬虫7 小时前
跟着MDN学HTML_day_48:(Node接口)
前端·javascript·ui·html·音视频
南 阳7 小时前
Python从入门到精通day66
开发语言·python
PieroPc8 小时前
CAMWATCH — 局域网摄像头监控系统 Fastapi + html
前端·python·html·fastapi·监控
十八旬8 小时前
快速安装ClaudeCode完整指南
开发语言·windows·python·claude
前进的李工9 小时前
EXPLAIN输出格式全解析:JSON、TREE与可视化
开发语言·数据库·mysql·性能优化·explain
Byron Loong9 小时前
【c++】为什么有了dll和.h,还需要包含lib
java·开发语言·c++