HarmonyOS 获取控件高度和加载本地图片?

1、如何在控件的ontouch方法里获取控件的高度

2、image控件怎么通过文件路径加载手机本地目录,应用文件下的图片文件

阅读 415
1 个回答

1、onTouch方法会获得TouchEvent对象,其中包含EventTarget:触发事件的元素对象显示区域 Area,参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-universal-events-touch-V5\#ontouch

.onTouch((event?: TouchEvent) => {
  if(event?.target){
    let height = event.target.area.height;
  }
})

2、image控件加载图片资源可以参考:https://developer.huawei.com/consumer/cn/doc/harmonyos-guides-V5/arkts-graphics-display-V5\#加载图片资源

Image组件不能直接传入应用沙箱路径,需要传入应用沙箱uri。Image的src支持file://路径前缀的字符串,应用沙箱URI:file://<bundleName\>/<sandboxPath\>。用于读取本应用安装目录下files文件夹下的图片资源。需要保证目录包路径下的文件有可读权限。参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/ts-basic-components-image-V5\#image-1

通过调用@ohos.file.fileuri模块的fileuri.getUriFromPath将沙箱路径转化为沙箱uri,参考文档:https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/js-apis-file-fileuri-V5\#fileurigeturifrompath

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进