HarmonyOS BreakPointType怎么导入

HarmonyOS
2024-12-26 14:40:23
浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
shlp

BreakPointType 这个是需要自行开发的一个类。这边提供了一个已经开发好的示例。可以参考。

响应式布局中的媒体查询

对通过媒体查询监听断点的功能做简单的封装,方便后续使用

参考链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/responsive-layout-V5

参考demo:

// common/breakpointsystem.ets
import mediaQuery from '@ohos.mediaquery'

declare interface BreakPointTypeOption<T> {
  xs?: T
  sm?: T
  md?: T
  lg?: T
  xl?: T
  xxl?: T
}

export class BreakPointType<T> {
  options: BreakPointTypeOption<T>

  constructor(option: BreakPointTypeOption<T>) {
    this.options = option
  }

  getValue(currentBreakPoint: string) {
    if (currentBreakPoint === 'xs') {
      return this.options.xs
    } else if (currentBreakPoint === 'sm') {
      return this.options.sm
    } else if (currentBreakPoint === 'md') {
      return this.options.md
    } else if (currentBreakPoint === 'lg') {
      return this.options.lg
    } else if (currentBreakPoint === 'xl') {
      return this.options.xl
    } else if (currentBreakPoint === 'xxl') {
      return this.options.xxl
    } else {
      return undefined
    }
  }
}

interface Breakpoint {
  name: string
  size: number
  mediaQueryListener?: mediaQuery.MediaQueryListener
}

export class BreakpointSystem {
  private currentBreakpoint: string = 'md'
  private breakpoints: Breakpoint[] = [
    { name: 'xs', size: 0 }, { name: 'sm', size: 320 },
    { name: 'md', size: 600 }, { name: 'lg', size: 840 }
  ]

  private updateCurrentBreakpoint(breakpoint: string) {
    if (this.currentBreakpoint !== breakpoint) {
      this.currentBreakpoint = breakpoint
      AppStorage.Set<string>('currentBreakpoint', this.currentBreakpoint)
      console.log('on current breakpoint: ' + this.currentBreakpoint)
    }
  }

  public register() {
    this.breakpoints.forEach((breakpoint: Breakpoint, index) => {
      let condition:string
      if (index === this.breakpoints.length - 1) {
        condition = '(' + breakpoint.size + 'vp<=width' + ')'
      } else {
        condition = '(' + breakpoint.size + 'vp<=width<' + this.breakpoints[index + 1].size + 'vp)'
      }
      console.log(condition)
      breakpoint.mediaQueryListener = mediaQuery.matchMediaSync(condition)
      breakpoint.mediaQueryListener.on('change', (mediaQueryResult) => {
        if (mediaQueryResult.matches) {
          this.updateCurrentBreakpoint(breakpoint.name)
        }
      })
    })
  }

  public unregister() {
    this.breakpoints.forEach((breakpoint: Breakpoint) => {
      if(breakpoint.mediaQueryListener){
        breakpoint.mediaQueryListener.off('change')
      }
    })
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
分享
微博
QQ
微信
回复
2024-12-26 17:55:53
相关问题
HarmonyOS动态导入是否会重复导入?
1000浏览 • 1回复 待解决
本地模拟器怎么导入媒体数据?
5521浏览 • 1回复 待解决
HarmonyOS 导入ComponentContent报错
1171浏览 • 1回复 待解决
HarmonyOS mindspore库导入问题
1130浏览 • 1回复 待解决
HarmonyOS import动态导入库失败
635浏览 • 1回复 待解决
HarmonyOS 导入不同的包的区别
836浏览 • 1回复 待解决
HarmonyOS 使用agconnect需要导入什么包
713浏览 • 1回复 待解决
HarmonyOS 能否支持3D模型导入
1490浏览 • 0回复 待解决
HarmonyOS import动态导入绝对路径
1289浏览 • 1回复 待解决
HarmonyOS 导入本地动态库报错9568305
1448浏览 • 1回复 待解决
DevEco Studio不能导入Module吗
8078浏览 • 2回复 待解决
Huks如何导入AES的密钥?
1449浏览 • 1回复 待解决
Mysql中的数据如何导入Greenplum?
4297浏览 • 1回复 待解决
ArkTS import导入napi模块结果错误
3528浏览 • 0回复 待解决
RSA导入外部密钥实现加解密
1577浏览 • 1回复 待解决