【每日学点HarmonyOS Next知识】网页获取高度、行为菜单底部布局、动画题目 ...

打印 上一主题 下一主题

主题 1871|帖子 1871|积分 5613

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
【每日学点HarmonyOS Next知识】网页获取高度、行为菜单底部布局、动画题目、Web跨域加载图片、不随体系深色变化

1、HarmonyOS Web控件getPageHeight()获取高度方法与网页实际高度不一样?

在Web的onPageEnd()方法中调用 getWebViewController().getPageHeight() 获取的高度与网页内容实际高度不符偏小,通过 setTimeout(()=>{ let webHeight1 = this.controller.getWebViewController().getPageHeight() Logger.getInstance().error(‘内部控件高度time’,’ webHeight:‘+webHeight1+’ webHeight:'+vp2px(webHeight1)) },200) 获取的高度与网页实际高度雷同,有没有不必要延时的获取网页实际高度的方法?
onPageEnd是页面加载完回调,不即是页面渲染完成,所从前后获取的高度不一样。可利用:onFirstMeaningfulPaint文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-web-V5#ZH-CN_TOPIC_0000001847049744__onfirstmeaningfulpaint12
2、HarmonyOS promptAction.showActionMenu 接口支持布局为底部布局吗?

promptAction.showActionMenu接口支持弹窗布局为底部布局吗?看了下文档里不能设置对其方式,并且示例里也是居中的方式。
参考demo:
  1. import promptAction from '@ohos.promptAction';
  2. import { BusinessError } from '@ohos.base';
  3. function showActionMenu() {
  4.   try {
  5.     promptAction.showActionMenu({
  6.       title: 'Title Info',
  7.       buttons: [
  8.         {
  9.           text: 'item1',
  10.           color: '#666666'
  11.         },
  12.         {
  13.           text: 'item2',
  14.           color: '#000000'
  15.         },
  16.       ]
  17.     }, (err, data) => {
  18.       if (err) {
  19.         console.info('showActionMenu err: ' + err);
  20.         return;
  21.       }
  22.       console.info('showActionMenu success callback, click button: ' + data.index);
  23.     })
  24.   } catch (error) {
  25.     let message = (error as BusinessError).message
  26.     let code = (error as BusinessError).code
  27.     console.error(`showActionMenu args error code is ${code}, message is ${message}`);
  28.   }
  29.   ;
  30. }
  31. @Entry
  32. @Component
  33. struct Index {
  34.   @State message: string = 'Hello World';
  35.   build() {
  36.     RelativeContainer() {
  37.       Text(this.message)
  38.         .id('HelloWorld')
  39.         .fontSize(50)
  40.         .fontWeight(FontWeight.Bold)
  41.         .alignRules({
  42.           center: { anchor: '__container__', align: VerticalAlign.Center },
  43.           middle: { anchor: '__container__', align: HorizontalAlign.Center }
  44.         })
  45.         .onClick(()=>{
  46.           showActionMenu()
  47.         })
  48.     }
  49.     .height('100%')
  50.     .width('100%')
  51.   }
  52. }
复制代码
3、HarmonyOS 动画必须搭配@State修饰的变量才能生效吗?

一般情况下动画是必要搭配@State修饰的变量才能生效,以下demo加上 Text().width(300).height(300).backgroundColor(Color.Black)就可以做动画了
  1. @Entry
  2. @Component
  3. export default struct ParticleStar {
  4.   aboutToAppear(): void {
  5.   }
  6.   build() {
  7.     Stack() {
  8.       Text()
  9.         .width(300).height(300).backgroundColor(Color.Black)
  10.       Particle({
  11.         particles: [
  12.           {
  13.             emitter: {
  14.               particle: {
  15.                 type: ParticleType.POINT, //粒子类型
  16.                 config: {
  17.                   radius: 10//圆点半径
  18.                 },
  19.                 count: 500, //粒子总数
  20.                 lifetime: 10000//粒子生命周期,单位ms
  21.               },
  22.               emitRate: 10, //每秒发射粒子数
  23.               position: [200, 0],
  24.               shape: ParticleEmitterShape.RECTANGLE//发射器形状
  25.             },
  26.             color: {
  27.               range: [Color.Red, Color.Yellow], //初始颜色范围
  28.               updater: {
  29.                 type: ParticleUpdater.CURVE, //变化方式为曲线变化
  30.                 config: [
  31.                   {
  32.                     from: Color.White, //变化起始值
  33.                     to: Color.Pink, //变化终点值
  34.                     startMillis: 0, //开始时间
  35.                     endMillis: 3000, //结束时间
  36.                     curve: Curve.EaseIn//变化曲线
  37.                   },
  38.                   {
  39.                     from: Color.Pink,
  40.                     to: Color.Orange,
  41.                     startMillis: 3000,
  42.                     endMillis: 5000,
  43.                     curve: Curve.EaseIn
  44.                   },
  45.                   {
  46.                     from: Color.Orange,
  47.                     to: Color.Pink,
  48.                     startMillis: 5000,
  49.                     endMillis: 8000,
  50.                     curve: Curve.EaseIn
  51.                   },
  52.                 ]
  53.               }
  54.             },
  55.             opacity: {
  56.               range: [0.0, 1.0], //粒子透明度的初始值从【0.0到1.0】随机产生
  57.               updater: {
  58.                 type: ParticleUpdater.CURVE, //透明度的变化方式是随机变化
  59.                 config: [
  60.                   {
  61.                     from: 0.0,
  62.                     to: 1.0,
  63.                     startMillis: 0,
  64.                     endMillis: 3000,
  65.                     curve: Curve.EaseIn
  66.                   },
  67.                   {
  68.                     from: 1.0,
  69.                     to: 0.0,
  70.                     startMillis: 5000,
  71.                     endMillis: 10000,
  72.                     curve: Curve.EaseIn
  73.                   }
  74.                 ]
  75.               }
  76.             },
  77.             scale: {
  78.               range: [0.0, 0.0],
  79.               updater: {
  80.                 type: ParticleUpdater.CURVE,
  81.                 config: [
  82.                   {
  83.                     from: 0.0,
  84.                     to: 0.5,
  85.                     startMillis: 0,
  86.                     endMillis: 3000,
  87.                     curve: Curve.EaseIn
  88.                   }
  89.                 ]
  90.               }
  91.             },
  92.             acceleration: {
  93.               //加速度的配置,从大小和方向两个维度变化,speed表示加速度大小,angle表示加速度方向
  94.               speed: {
  95.                 range: [3, 9],
  96.                 updater: {
  97.                   type: ParticleUpdater.RANDOM,
  98.                   config: [1, 20]
  99.                 }
  100.               },
  101.               angle: {
  102.                 range: [90, 90]
  103.               }
  104.             }
  105.           }
  106.         ]
  107.       }).width(300).height(300)
  108.     }.width("100%").height("100%").align(Alignment.Center)
  109.   }
  110. }
复制代码
4、HarmonyOS Web组件无法跨域加载图片资源?

App访问网页,网页里面有些图片必要跨域加载,但是由于跨域无法加载显示,叨教有什么方法可以解决吗?
跨域请参考以下链接:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/web-cross-origin-V5
为了提高安全性,ArkWeb内核不允许file协议或者resource协议访问URL上下文中来自跨域的请求。因此,在利用Web组件加载当地离线资源的时间,Web组件会拦截file协媾和resource协议的跨域访问。可以通过方法二设置一个路径列表,再利用file协议访问该路径列表中的资源,允许跨域访问当地文件。当Web组件无法访问当地跨域资源时,开发者可以在DevTools控制台中看到雷同以下报错信息:
  1. Access to script at 'xxx' from origin 'xxx' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, arkweb, data, chrome-extension, chrome, https, chrome-untrusted.
复制代码
方法一
为了使Web组件可以或许乐成访问跨域资源,开发者应采用http或https等协议,更换原先利用的file或resource协议进行加载。其中,更换的url域名为自界说构造的仅供个人或者组织利用的域名,以制止与互联网上实际存在的域名产生辩说。同时,开发者需利用Web组件的onInterceptRequest方法,对当地资源进行拦截和相应的更换。
以下联合示例说明如何解决当地资源跨域访问失败的题目。其中,index.html和js/script.js置于工程中的rawfile目录下。如果利用resource协议访问index.html,js/script.js将因跨域而被拦截,无法加载。在示例中,利用https://www.example.com/域名更换了本来的resource协议,同时利用onInterceptRequest接口更换资源,使得js/script.js可以乐成加载,从而解决了跨域拦截的题目。
方法二
通过setPathAllowingUniversalAccess设置一个路径列表。当利用file协议访问该列表中的资源时,允许进行跨域访问当地文件。别的,一旦设置了路径列表,file协议将仅限于访问列表内的资源(此时,fileAccess的行为将会被此接口行为覆盖)。路径列表中的路径必须符合以下任一路径格式:

  • 应用文件目录通过Context.filesDir获取,其子目录示例如下:

    • /data/storage/el2/base/files/example
    • /data/storage/el2/base/haps/entry/files/example

  • 应用资源目录通过Context.resourceDir获取,其子目录示例如下:

    • /data/storage/el1/bundle/entry/resource/resfile
    • /data/storage/el1/bundle/entry/resource/resfile/example
      当路径列表中的任一路径不满足上述条件时,体系将抛出异常码401,并判定路径列表设置失败。若设置的路径列表为空,file协议的可访问范围将遵循fileAccess的规则,详细示例如下。

  1. // main/ets/pages/Index.ets
  2. import { webview } from '@kit.ArkWeb';
  3. import { BusinessError } from '@kit.BasicServicesKit';
  4. @Entry
  5. @Component
  6. struct WebComponent {
  7.   controller: WebviewController = new webview.WebviewController();
  8.   build() {
  9.     Row() {
  10.       Web({ src: "", controller: this.controller })
  11.         .onControllerAttached(() => {
  12.           try {
  13.             // 设置允许可以跨域访问的路径列表
  14.             this.controller.setPathAllowingUniversalAccess([
  15.               getContext().resourceDir,
  16.               getContext().filesDir + "/example"
  17.             ])
  18.             this.controller.loadUrl("file://" + getContext().resourceDir + "/index.html")
  19.           } catch (error) {
  20.             console.error(`ErrorCode: ${(error as BusinessError).code},  Message: ${(error as   BusinessError).message}`);
  21.           }
  22.         })
  23.         .javaScriptAccess(true)
  24.         .fileAccess(true)
  25.         .domStorageAccess(true)
  26.     }
  27.   }
  28. }
复制代码
  1. <!-- main/resource/rawfile/index.html -->
  2. <!DOCTYPE html>
  3. <html lang="en">
  4. <head>
  5.     <meta charset="utf-8">
  6.     <title>Demo</title>
  7.     <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no,   viewport-fit=cover">
  8.     <script>
  9.         function getFile() {
  10.             var file = "file:///data/storage/el1/bundle/entry/resources/resfile/js/script.js";
  11.       // 使用file协议通过XMLHttpRequest跨域访问本地js文件。
  12.             var xmlHttpReq = new XMLHttpRequest();
  13.             xmlHttpReq.onreadystatechange = function(){
  14.                 console.log("readyState:" + xmlHttpReq.readyState);
  15.                 console.log("status:" + xmlHttpReq.status);
  16.                 if(xmlHttpReq.readyState == 4){
  17.                     if (xmlHttpReq.status == 200) {
  18.                 // 如果ets侧正确设置路径列表,则此处能正常获取资源
  19.                         const element = document.getElementById('text');
  20.                         element.textContent = "load " + file + " success";
  21.                     } else {
  22.                 // 如果ets侧不设置路径列表,则此处会触发CORS跨域检查错误
  23.                         const element = document.getElementById('text');
  24.                         element.textContent = "load " + file + " failed";
  25.                     }
  26.                 }
  27.             }
  28.             xmlHttpReq.open("GET", file);
  29.             xmlHttpReq.send(null);
  30.         }
  31.     </script>
  32. </head>
  33. <body>
  34. <div class="page">
  35.     <button id="example" onclick="getFile()">stealFile</button>
  36. </div>
  37. <div id="text"></div>
  38. </body>
  39. </html>
复制代码
  1. // main/resources/rawfile/js/script.js
  2. const body = document.body;
  3. const element = document.createElement('div');
  4. element.textContent = 'success';
  5. body.appendChild(element);
复制代码
5、HarmonyOS 有没有不随体系深色模式变化的操纵?

应用主动设置深浅色模式,可参考官方文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-light-dark-color-adaptation-V5

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

宝塔山

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表