HarmonyOS 蒙层或护眼模式方案咨询

1、app弹出有遮罩蒙层,颜色是透明灰度的。可以看到被遮住原布局内容

2、蒙层不拦截点击响应事件,响应的事件还是原有布局。

3、减少改动原有页面布局代码

HarmonyOS
2024-12-25 07:34:49
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

示例参考

// 1.EntryAbility.ets
import UIAbility from '@ohos.app.ability.UIAbility';
import window from '@ohos.window';
import { BusinessError } from '@ohos.base';

let sub_windowClass:window.Window |null = null;

export default class EntryAbility extends UIAbility {

  destroySubWindow() {
    // 4.销毁子窗口。当不再需要子窗口时,可根据具体实现逻辑,使用destroy对其进行销毁。
    sub_windowClass?.destroyWindow((err: BusinessError) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err));
        return;
      }
      console.info('Succeeded in destroying the window.');
    });
  }

  onWindowStageCreate(windowStage: window.WindowStage) {
    AppStorage.SetOrCreate('window', windowStage);

    windowStage.loadContent('pages/Index', (err, data) => {
      if (err.code) {
        console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err));
        return;
      }
      console.error('Succeeded in loading the main page content.');
    });
  }

  onWindowStageDestroy() {
    // this.destroySubWindow();
  }
};

// 2.Index.ets
// Index.ets
import window from '@ohos.window';
import router from '@ohos.router';
import { BusinessError } from '@ohos.base';

let windowStage_: window.WindowStage | null = null;

@Entry
@Component
struct Index {
  @State message: string = 'DEMO for EyeCare';
  @StorageLink('window') storWindow: window.WindowStage|null = null;
  private windowStage = this.storWindow;
  private sub_windowClass: window.Window|null = null;

  showSubWindow() {
    // 0.获取当前系统的window显示设置
    // let windowLimits = windowClass.getWindowLimits();
    // let maxWinWidth = windowLimits?.maxWidth;
    // let maxHeight = windowLimits?.maxHeight;
    // console.info(`Get the window limits: maxWinWidth:${maxWinWidth}, maxHeight:${maxHeight}`);

    // 1.创建应用子窗口。
    this.windowStage?.createSubWindow("maskWindow", (err: BusinessError, data) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('Failed to create the subwindow. Cause: ' + JSON.stringify(err));
        return;
      }
      this.sub_windowClass = data;
      console.info('Succeeded in creating the subwindow. Data: ' + JSON.stringify(data));
      // 2.子窗口创建成功后,设置子窗口的位置、大小及相关属性等。
      this.sub_windowClass?.moveWindowTo(0, 0, (err: BusinessError) => {
        let errCode: number = err.code;
        if (errCode) {
          console.error('Failed to move the window. Cause:' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in moving the window.');
      });
      this.sub_windowClass?.resize(1260, 2720, (err: BusinessError) => {
        let errCode: number = err.code;
        if (errCode) {
          console.error('Failed to change the window size. Cause:' + JSON.stringify(err));
          return;
        }
        console.info('Succeeded in changing the window size.');
      });
      // 3.为子窗口加载对应的目标页面。
      this.sub_windowClass?.setUIContent("pages/subWindow", (err: BusinessError) => {
        let errCode: number = err.code;
        if (errCode) {
          console.error('Failed to load the content. Cause:' + JSON.stringify(err));
          return;
        }

        (this.sub_windowClass as window.Window).setWindowBackgroundColor('#33e3ffcd');//33e3edcd
        (this.sub_windowClass as window.Window).setWindowFocusable(false);
        (this.sub_windowClass as window.Window).setWindowTouchable(false, (err: BusinessError) => {
          const errCode: number = err.code;
          if (errCode) {
            console.error(`Failed to set the window to be touchable. Cause code: ${err.code}, message: ${err.message}`);
            return;
          }
          console.info('Succeeded in setting the window to be touchable.');
        });

        console.info('Succeeded in loading the content.');
        // 3.显示子窗口。
        (this.sub_windowClass as window.Window).showWindow((err: BusinessError) => {
          let errCode: number = err.code;
          if (errCode) {
            console.error('Failed to show the window. Cause: ' + JSON.stringify(err));
            return;
          }
          console.info('Succeeded in showing the window.');
        });
      });
    })
  }

  destroySubWindow() {
    // 4.销毁子窗口。当不再需要子窗口时,可根据具体实现逻辑,使用destroy对其进行销毁。
    (this.sub_windowClass as window.Window).destroyWindow((err: BusinessError) => {
      let errCode: number = err.code;
      if (errCode) {
        console.error('Failed to destroy the window. Cause: ' + JSON.stringify(err));
        return;
      }
      console.info('Succeeded in destroying the window.');
    });
  }

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(24)
          .fontWeight(FontWeight.Bold)

        Button() {
          Text('Next Page')
            .fontSize(20)
            .fontWeight(FontWeight.Bold)
        }
        .type(ButtonType.Capsule)
        .margin({
          top: 20
        })
        .backgroundColor('#0D9FFB')
        .width('40%')
        .height('5%')
        // 跳转按钮绑定onClick事件,点击时跳转到第二页
        .onClick(() => {
          console.info(`Succeeded in clicking the 'Next' button.`)
          // 跳转到第二页
          router.pushUrl({ url: 'pages/Second' }).then(() => {
            console.info('Succeeded in jumping to the second page.')
          }).catch((err: BusinessError) => {
            console.error(`Failed to jump to the second page.Code is ${err.code}, message is ${err.message}`)
          })
        })
        Button(){
          Text('CreateSubWindow')
            .fontSize(24)
            .fontWeight(FontWeight.Normal)
        }.width(220).height(68)
        .margin({left:10, top:60})
        .onClick(() => {
          // this.CreateSubWindow()
          this.showSubWindow();
        })
        Button(){
          Text('destroySubWindow')
            .fontSize(24)
            .fontWeight(FontWeight.Normal)
        }.width(220).height(68)
        .margin({left:10, top:60})
        .onClick(() => {
          this.destroySubWindow()
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}

// 3 Second.ets
// Second.ets
import { router } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';

@Entry
@Component
struct Second {
  @State message: string = 'this is second page';

  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button() {
          Text('Back')
            .fontSize(25)
            .fontWeight(FontWeight.Bold)
        }
        .type(ButtonType.Capsule)
        .margin({
          top: 20
        })
        .backgroundColor('#0D9FFB')
        .width('40%')
        .height('5%')
        // 返回按钮绑定onClick事件,点击按钮时返回到第一页
        .onClick(() => {
          console.info(`Succeeded in clicking the 'Back' button.`)
          try {
            // 返回第一页
            router.back()
            console.info('Succeeded in returning to the first page.')
          } catch (err) {
            let code = (err as BusinessError).code;
            let message = (err as BusinessError).message;
            console.error(`Failed to return to the first page.Code is ${code}, message is ${message}`)
          }
        })
      }
      .width('100%')
    }
    .height('100%')
  }
}


// 4.SubWindow.ets
// subWindow.ets
@Entry
@Component
struct SubWindow {
  @State message: string = 'subWindow: untouchable & unfocusable';
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(12)
          .fontWeight(FontWeight.Bold)
      }
      .width('100%')
    }
    .backgroundColor('#33e3ffcd')
    .height('100%')
  }
}
分享
微博
QQ
微信
回复
2024-12-25 10:26:48
相关问题
arkts 护眼模式功能实现
501浏览 • 0回复 待解决
HarmonyOS bindsheet去除
247浏览 • 1回复 待解决
HarmonyOS 气泡点击问题
585浏览 • 1回复 待解决
如何去除Tabs组件两侧的
2099浏览 • 1回复 待解决
Web组件下网页中图片长按出现
1079浏览 • 1回复 待解决
HarmonyOS 热修复技术方案咨询
391浏览 • 1回复 待解决
HarmonyOS 订阅续订订单同步方案咨询
226浏览 • 1回复 待解决
HarmonyOS 自定义相册方案咨询
231浏览 • 1回复 待解决
HarmonyOS 关于Swiper+LazyForEach方案咨询
193浏览 • 1回复 待解决
适老化模式字体放大咨询
661浏览 • 1回复 待解决
HarmonyOS装饰器注解实现方式咨询
635浏览 • 1回复 待解决