文档管理中心
我的
您当前正在浏览新版开发者文档中心,目录分类和层级有所调整。点击左侧当前文档分类名称前的“☰”图标,可切换文档分类。 了解新版目录
指南与API参考指南应用框架ArkUI(方舟UI框架)窗口管理其他开发场景控制窗口外观 (ArkTS)

控制窗口外观 (ArkTS)

场景介绍

窗口外观用于描述窗口在屏幕上的显示形态和视觉效果,目前支持通过设置异形窗口、窗口阴影、窗口圆角以及窗口背景色实现窗口外观设置。

开发者可以根据界面设计和交互需求,对窗口外观进行定制。例如:

  • 通过设置窗口掩码,将子窗或全局悬浮窗显示为异形窗口。

  • 通过设置窗口边缘阴影的模糊半径,调整子窗或全局悬浮窗的阴影效果。

  • 通过设置窗口圆角,调整子窗或全局悬浮窗的边缘显示效果。

  • 通过设置窗口背景色,使窗口背景与应用页面或主题样式保持一致。

异形窗口

异形窗口为非常规形状的窗口,掩码用于描述异形窗口的形状。仅应用子窗和全局悬浮窗可设置为异形窗口。

可通过setWindowMask()或setWindowMaskWithAlpha()接口设置异形窗口的掩码,以定义窗口的可见区域。

设置掩码后,窗口将按照掩码形状显示,窗口阴影将被禁用,窗口的圆角半径变为0。

  • 使用setWindowMask()接口设置异形窗口的掩码。

    掩码仅支持取值为整数0和整数1的二维数组输入,数组行数对应窗口高度,列数对应窗口宽度。整数0代表对应像素透明且不可交互,整数1代表对应像素不透明且可交互。

  • 从API版本26.0.0开始,支持使用setWindowMaskWithAlpha()接口设置异形窗口的掩码。

    掩码支持取值在[0, 255]范围的数组输入,数组长度等于窗口宽度乘以窗口高度。整数0代表对应像素透明且不可交互,整数255代表对应像素不透明且可交互,0~255之间代表对应像素部分透明且可交互。此接口性能优于setWindowMask(),推荐使用。

此处以设置子窗的异形窗口为例。此例主要实现以下效果:

  1. 点击“Create Sub Window”按钮可以创建子窗。

  2. 创建子窗后,点击“setWindowMask for Sub Window”按钮,可通过setWindowMaskWithAlpha()接口设置子窗掩码。子窗发生以下变化:

    • 子窗变为三角形。
    • 子窗的阴影和圆角消失。
    • 子窗矩形区域的左上部分变为透明不可交互,通过点击“Create Test Window”按钮,事件透传到该按钮,创建出绿色的测试窗口。
收起
自动换行
深色代码主题
复制
  1. import { window } from '@kit.ArkUI';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. @Entry
  4. @Component
  5. struct Index {
  6. // ...
  7. private windowMaskSub: window.Window | undefined = undefined;
  8. // ...
  9. setWindowMask(window: window.Window) {
  10. let windowMask: Uint8Array = new Uint8Array(this.winWidth * this.winHeight);
  11. for (let i = 0; i < this.winHeight; i++) {
  12. for (let k = 0; k < this.winWidth; k++) {
  13. if ((i + k) < (this.winHeight + this.winWidth) / 2) {
  14. windowMask[i * this.winWidth + k] = 0;
  15. } else {
  16. windowMask[i * this.winWidth + k] = 255;
  17. }
  18. }
  19. }
  20. window.setWindowMaskWithAlpha(windowMask, this.winWidth, this.winHeight);
  21. }
  22. build() {
  23. Row() {
  24. Scroll(){
  25. Column() {
  26. // ...
  27. Row() {
  28. Button('setWindowMask for Sub Window')
  29. .width('90%')
  30. .type(ButtonType.Capsule)
  31. .margin({
  32. top: 10
  33. }).fontSize(18)
  34. .onClick(() => {
  35. if(this.windowMaskSub) {
  36. this.setWindowMask(this.windowMaskSub);
  37. }
  38. })
  39. }
  40. }
  41. .width('100%')
  42. }
  43. }
  44. .height('100%')
  45. }
  46. }

窗口阴影

窗口阴影是显示在窗口边缘的投影效果,可以增强窗口与背景之间的层次感,使窗口呈现悬浮于背景之上的视觉效果。

  • 可通过setWindowShadowRadius()接口设置窗口边缘阴影的模糊半径,仅支持子窗和全局悬浮窗使用。

    此处以全局悬浮窗为例,设置其窗口边缘阴影的模糊半径。

    收起
    自动换行
    深色代码主题
    复制
    1. // pages/page1.ets
    2. import { window } from '@kit.ArkUI';
    3. @Entry
    4. @Component
    5. struct SliderDemo {
    6. // ...
    7. // 设置窗口边缘阴影的模糊半径
    8. setShadowRadius(val: number) {
    9. const floatWindowObj = AppStorage.get<window.Window>('floatWindow');
    10. floatWindowObj?.setWindowShadowRadius(val);
    11. }
    12. build() {
    13. // ...
    14. }
    15. }

设置窗口圆角

  • 可通过setWindowCornerRadius()接口设置窗口的圆角半径,仅支持子窗和全局悬浮窗使用。

    此处以全局悬浮窗为例,设置其窗口圆角。

    收起
    自动换行
    深色代码主题
    复制
    1. // pages/page1.ets
    2. import { window } from '@kit.ArkUI';
    3. @Entry
    4. @Component
    5. struct SliderDemo {
    6. // ...
    7. // 设置圆角
    8. setCornerRadius(val: number) {
    9. const floatWindowObj = AppStorage.get<window.Window>('floatWindow');
    10. floatWindowObj?.setWindowCornerRadius(val);
    11. }
    12. build() {
    13. // ...
    14. }
    15. }

窗口背景色

窗口背景色用于控制窗口内容区域或窗口容器区域的背景显示效果。

开发者可根据业务场景,选择设置应用内容区域背景色,或设置包含标题栏在内的窗口容器背景色,以实现页面背景透明、窗口整体配色统一等效果。

  • 可使用setWindowBackgroundColor()接口调整窗口内容区域的背景色,主要影响窗口内承载UI内容的部分。可传入不区分大小写的十六进制RGB或ARGB颜色,调整背景颜色。从API version 18开始,支持传入ColorMetrics类型。

  • 可使用setWindowContainerColor()接口设置PC/2in1或Tablet设备上的主窗口容器区域的背景色,窗口容器背景色会覆盖整个窗口区域,包括标题栏和内容区域。如果处于非自由窗口状态下,效果等同于setWindowBackgroundColor()。该接口不支持将非焦点态下的主窗口背景设置为透明。

  • 当同时使用setWindowContainerColor()和setWindowBackgroundColor()时,内容区域显示setWindowBackgroundColor()设置的颜色,而标题栏则显示setWindowContainerColor()设置的颜色。

  • 从API版本26.0.0开始,支持使用setWindowContainerModalColor()接口设置PC/2in1设备上的主窗口容器区域的背景色,以适配不同UI设计需求。通过该接口设置的背景色会作用于整个窗口容器区域,包括标题栏和内容区域。该接口支持将非焦点态下的主窗口背景设置为透明。

说明

示例代码如下:

收起
自动换行
深色代码主题
复制
  1. import { ColorMetrics, window } from '@kit.ArkUI';
  2. import { hilog } from '@kit.PerformanceAnalysisKit';
  3. const DOMAIN = 0x0000;
  4. @Entry
  5. @Component
  6. struct Index {
  7. @StorageLink('mainWindow') mainWindow: window.Window | undefined = undefined;
  8. @State alpha: number = 0;
  9. @State red: number = 0;
  10. @State green: number = 0;
  11. @State blue: number = 0;
  12. @State statusText: string = 'Move the sliders to change the current window background color.';
  13. @State applyModeText: string = 'Current mode: string (#AARRGGBB)';
  14. // 将0~255的通道值转换成两位十六进制字符串,用于拼接 #AARRGGBB。
  15. private toHex(value: number): string {
  16. return Math.round(value).toString(16).padStart(2, '0').toUpperCase();
  17. }
  18. // 按ARGB顺序生成当前窗口背景色的十六进制字符串。
  19. private getColorValue(): string {
  20. return `#${this.toHex(this.alpha)}${this.toHex(this.red)}${this.toHex(this.green)}${this.toHex(this.blue)}`;
  21. }
  22. // ...
  23. // 直接用ColorMetrics.rgba(...)设置窗口背景色。
  24. private applyByColorMetrics(): void {
  25. if (!this.mainWindow) {
  26. this.statusText = 'Current window is unavailable.';
  27. return;
  28. }
  29. try {
  30. const alpha = Math.round(this.alpha) / 255;
  31. const colorMetrics = ColorMetrics.rgba(Math.round(this.red), Math.round(this.green),
  32. Math.round(this.blue), alpha);
  33. this.mainWindow.setWindowBackgroundColor(colorMetrics);
  34. this.applyModeText = 'Current mode: ColorMetrics.rgba(...)';
  35. this.statusText = `setWindowBackgroundColor(ColorMetrics.rgba(${Math.round(this.red)}, ${Math.round(this.green)}, ${Math.round(this.blue)}, ${alpha.toFixed(2)})) success`;
  36. hilog.info(DOMAIN, 'backgroundColor', this.statusText);
  37. } catch (err) {
  38. this.statusText = `setWindowBackgroundColor by ColorMetrics failed: ${JSON.stringify(err)}`;
  39. hilog.error(DOMAIN, 'backgroundColor', this.statusText);
  40. }
  41. }
  42. // ...
  43. }