使用预览器实现多端预览

通过对代码进行编译混淆,生成闭源HAP。在不共享源码的情况下,通过闭源HAR对外提供组件、资源等,可以实现多个模块或者多个工程共享组件、资源等。

HarmonyOS
2024-05-28 21:54:32
1254浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
一杯生椰

使用的核心API       

1. 参考文档:查看多端设备预览效果

核心代码解释

1.代码文件:

import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; 
import { hilog } from '@kit.PerformanceAnalysisKit'; 
import { window } from '@kit.ArkUI'; 
import display from '@ohos.display' 
 
export default class EntryAbility extends UIAbility { 
 
  private windowObj?: window.Window 
  private curBp: string = '' 
  //... 
  // 根据当前窗口尺寸更新断点 
  private updateBreakpoint(windowWidth: number) :void{ 
    // 将长度的单位由px换算为vp 
    let windowWidthVp = windowWidth / display.getDefaultDisplaySync().densityPixels 
    console.log('windowWidthVp = '+windowWidthVp) 
    let newBp: string = '' 
    if (windowWidthVp < 320) { 
      newBp = 'xs' 
    } else if (windowWidthVp < 600) { 
      newBp = 'sm' 
    } else if (windowWidthVp < 840) { 
      newBp = 'md' 
    } else { 
      newBp = 'lg' 
    } 
    if (this.curBp !== newBp) { 
      this.curBp = newBp 
      // 使用状态变量记录当前断点值 
      AppStorage.setOrCreate('currentBreakpoint', this.curBp) 
    } 
  } 
 
  onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate'); 
  } 
 
  onDestroy(): void { 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy'); 
  } 
 
  onWindowStageCreate(windowStage: window.WindowStage): void { 
    // Main window is created, set main page for this ability 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); 
 
    windowStage.loadContent('pages/MediaQuerySample', (err, data) => { 
      if (err.code) { 
        hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? ''); 
        return; 
      } 
      hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? ''); 
    }); 
 
    windowStage.getMainWindow().then((windowObj) => { 
      this.windowObj = windowObj 
      // 获取应用启动时的窗口尺寸 
      this.updateBreakpoint(windowObj.getWindowProperties().windowRect.width) 
      // 注册回调函数,监听窗口尺寸变化 
      windowObj.on('windowSizeChange', (windowSize)=>{ 
        this.updateBreakpoint(windowSize.width) 
      }) 
    }); 
  } 
 
  onWindowStageDestroy(): void { 
    // Main window is destroyed, release UI related resources 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); 
 
    try { 
      if (this.windowObj) { 
        this.windowObj.off('windowSizeChange') 
      } 
    } catch (err) { 
      console.log('show windowObj errMsg' + JSON.stringify(err)) 
    } 
  } 
 
  onForeground(): void { 
    // Ability has brought to foreground 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground'); 
  } 
 
  onBackground(): void { 
    // Ability has back to background 
    hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground'); 
  } 
} 
// 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') 
      } 
    }) 
  } 
} 
// MediaQuerySample.ets 
import { BreakpointSystem, BreakPointType } from './breakpointsystem' 
import { display } from '@kit.ArkUI'; 
 
@Entry 
@Component 
struct MediaQuerySample { 
  @StorageLink('currentBreakpoint') private currentBreakpoint: string = "md"; 
  @State private icon: Resource = $r('app.media.images2') 
  private breakpointSystem: BreakpointSystem = new BreakpointSystem() 
 
  aboutToAppear() { 
    this.breakpointSystem.register() 
  } 
 
  aboutToDisappear() { 
    this.breakpointSystem.unregister() 
  } 
  onPageShow(): void { 
    console.log('width='+JSON.stringify(display.getDefaultDisplaySync().width)) 
    console.log('height='+JSON.stringify(display.getDefaultDisplaySync().height)) 
    console.log('densityDPI='+JSON.stringify(display.getDefaultDisplaySync().densityDPI)) 
  } 
  build() { 
    Flex({ direction: FlexDirection.Column, alignItems: ItemAlign.Center, justifyContent: FlexAlign.Center }) { 
      Image(new BreakPointType({sm:$r('app.media.images2'), md:$r('app.media.images3'), lg:$r('app.media.images4')}).getValue(this.currentBreakpoint)!) 
        .height(100) 
        .width(100) 
        .objectFit(ImageFit.Contain) 
 
      Text(this.currentBreakpoint) 
        .fontSize(24) 
        .margin(10) 
    } 
    .width('100%') 
    .height('100%') 
  } 
}
  • 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.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
  • 97.
  • 98.
  • 99.
  • 100.
  • 101.
  • 102.
  • 103.
  • 104.
  • 105.
  • 106.
  • 107.
  • 108.
  • 109.
  • 110.
  • 111.
  • 112.
  • 113.
  • 114.
  • 115.
  • 116.
  • 117.
  • 118.
  • 119.
  • 120.
  • 121.
  • 122.
  • 123.
  • 124.
  • 125.
  • 126.
  • 127.
  • 128.
  • 129.
  • 130.
  • 131.
  • 132.
  • 133.
  • 134.
  • 135.
  • 136.
  • 137.
  • 138.
  • 139.
  • 140.
  • 141.
  • 142.
  • 143.
  • 144.
  • 145.
  • 146.
  • 147.
  • 148.
  • 149.
  • 150.
  • 151.
  • 152.
  • 153.
  • 154.
  • 155.
  • 156.
  • 157.
  • 158.
  • 159.
  • 160.
  • 161.
  • 162.
  • 163.
  • 164.
  • 165.
  • 166.
  • 167.
  • 168.
  • 169.
  • 170.
  • 171.
  • 172.
  • 173.
  • 174.
  • 175.
  • 176.
  • 177.
  • 178.
  • 179.
  • 180.
  • 181.
  • 182.
  • 183.
  • 184.
  • 185.
  • 186.
  • 187.
  • 188.
  • 189.
  • 190.
  • 191.
  • 192.
  • 193.
  • 194.
  • 195.
  • 196.
  • 197.
  • 198.
  • 199.
  • 200.
  • 201.
  • 202.
  • 203.
  • 204.
  • 205.
  • 206.
  • 207.
  • 208.
  • 209.

2.打开预览器,在Previewer窗口中,打开Profile Manager中的Multi-profile preview开关,同时查看多设备上的应用/服务运行效果。

实现效果

适配的版本信息

IDE:DevEco Studio 4.1.3.500

SDK:HarmoneyOS NEXT Developer Preview1

分享
微博
QQ
微信
回复
2024-05-29 23:11:42
相关问题
卡片能否通过预览进行预览
1326浏览 • 1回复 待解决
无法使用DevEco Studio的预览
7320浏览 • 1回复 待解决
如何使用预览查看服务效果
2970浏览 • 1回复 待解决
DevEco Studio预览支持实时预览吗?
7698浏览 • 1回复 待解决
如何使用预览接口进行文件预览
1195浏览 • 1回复 待解决
使用DevEco Studio时Java预览提示错误
11057浏览 • 1回复 待解决
使用预览报这个错的原因是?
5373浏览 • 1回复 待解决
使用web组件实现预览沙箱中pdf
2957浏览 • 1回复 待解决
HarmonyOS 如何实现图片预览
842浏览 • 1回复 待解决
HarmonyOS filePreview预览txt提示预览失败
1757浏览 • 1回复 待解决
如何实现pdf文件的预览
1313浏览 • 1回复 待解决
应用开发中的预览换成手表模式
11032浏览 • 2回复 待解决
DevEco Device无法使用Previewer预览
41079浏览 • 3回复 待解决
能否提供图片预览的官方实现
1577浏览 • 1回复 待解决
如何实现双路预览+录制功能
2015浏览 • 1回复 待解决
预览上WEB组件无法显示HTML内容
3573浏览 • 1回复 待解决
如何实现拍照预览onPreviewFrame回调
1284浏览 • 1回复 待解决
camera_lite预览功能如何实现
3198浏览 • 0回复 待解决
HarmonyOS 使用PDF kit预览PDF文件
835浏览 • 1回复 待解决