HarmonyOS鸿蒙Next中怎样获取FrameNode含expandSafeArea的位置大小
HarmonyOS鸿蒙Next中怎样获取FrameNode含expandSafeArea的位置大小 使用getMeasuredSize和getPositionToWindow可以获取到FrameNode的大小和位置,但是不包含expandSafeArea扩展出来的区域,怎样才能获取到expandSafeArea扩展后的背景大小?就是包含了扩展安全区的组件的大小,能不能主动获取,而不是监听变化?
可以通过获取扩展的安全区大小后自行计算
获取规避的区域高度的demo
// xxx.ets
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
@Entry
@Component
struct SafeAreaExample1 {
@State text: string = ''
controller: TextInputController = new TextInputController()
build() {
Row() {
Column()
.height('100%').width('100%')
.backgroundImage($r('app.media.background')).backgroundImageSize(ImageSize.Cover)
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP])
.onAreaChange((old:Area,newArea:Area)=>{
console.info("新的Area:", JSON.stringify(newArea))
})
.onClick(()=>{
console.info("进入点击事件")
window.getLastWindow(getContext(this), (err: BusinessError, data) =>{
let windowClass: window.Window = data;
let topAvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
let bottomAvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
let bottomRectHeight = px2vp(bottomAvoidArea.bottomRect.height)
let topRectHeight = px2vp(topAvoidArea.topRect.height)
console.info("topAvoidArea:",bottomRectHeight)
console.info("bottomAvoidArea:",topRectHeight)
})
});
}.height('100%')
}
}
通过getRectangleById获取组件大小
参考demo:
// xxx.ets
import { window } from '@kit.ArkUI';
import { BusinessError } from '@kit.BasicServicesKit';
import { ComponentUtils, Font, PromptAction, Router, UIInspector, MediaQuery } from '@ohos.arkui.UIContext';
@Entry
@Component
struct SafeAreaExample1 {
@State text: string = ''
controller: TextInputController = new TextInputController()
getSafeArea(){
console.info("进入点击事件")
window.getLastWindow(getContext(this), (err: BusinessError, data) =>{
let windowClass: window.Window = data;
let topAvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_SYSTEM)
let bottomAvoidArea = windowClass.getWindowAvoidArea(window.AvoidAreaType.TYPE_NAVIGATION_INDICATOR)
let bottomRectHeight = px2vp(bottomAvoidArea.bottomRect.height)
let topRectHeight = px2vp(topAvoidArea.topRect.height)
console.info("topAvoidArea:",bottomRectHeight)
console.info("bottomAvoidArea:",topRectHeight)
})
}
getAreaById(){
let context = this.getUIContext()
let componentUtils:ComponentUtils = context.getComponentUtils();
let modePosition = componentUtils.getRectangleById("out");
let localOffsetWidth = modePosition.size.width;
let localOffsetHeight = modePosition.size.height;
console.info("localOffsetWidth:", px2vp(localOffsetWidth))
console.info("localOffsetHeight:",px2vp(localOffsetHeight))
}
build() {
Row() {
Column()
.id('out')
.height('100%').width('100%')
.backgroundImage($r('app.media.background')).backgroundImageSize(ImageSize.Cover)
.expandSafeArea([SafeAreaType.SYSTEM], [SafeAreaEdge.TOP,SafeAreaEdge.BOTTOM])
.onAreaChange((old:Area,newArea:Area)=>{
console.info("新的Area:", JSON.stringify(newArea))
})
.onClick(()=>{
this.getAreaById()
});
}.height('100%')
}
}
更多关于HarmonyOS鸿蒙Next中怎样获取FrameNode含expandSafeArea的位置大小的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
在HarmonyOS鸿蒙Next中,获取FrameNode
包含expandSafeArea
的位置和大小可以通过FrameNode
的getBounds
方法实现。getBounds
方法返回一个Rect
对象,该对象包含了FrameNode
的边界信息,包括其位置和大小。
具体步骤如下:
-
获取
FrameNode
对象:首先,你需要获取到目标FrameNode
的实例。FrameNode
通常是通过Component
的getFrameNode
方法获取的。 -
调用
getBounds
方法:使用FrameNode
对象的getBounds
方法,该方法会返回一个Rect
对象,包含了FrameNode
的边界信息。 -
处理
expandSafeArea
:expandSafeArea
是鸿蒙系统中用于扩展安全区域的概念。getBounds
方法返回的Rect
已经包含了expandSafeArea
的影响,因此无需额外处理。
示例代码如下:
const frameNode = component.getFrameNode();
const bounds = frameNode.getBounds();
const left = bounds.left;
const top = bounds.top;
const right = bounds.right;
const bottom = bounds.bottom;
const width = bounds.width;
const height = bounds.height;
在上述代码中,bounds
对象包含了FrameNode
的位置和大小信息,left
、top
、right
、bottom
分别表示FrameNode
的边界位置,width
和height
表示其宽度和高度。
通过这种方式,你可以获取到FrameNode
包含expandSafeArea
的位置和大小信息。
在HarmonyOS鸿蒙Next中,获取FrameNode包含expandSafeArea的位置大小,可以通过FrameNode
的getBounds
方法结合expandSafeArea
属性来实现。首先,使用getBounds
获取FrameNode的原始边界矩形,然后通过expandSafeArea
属性获取安全区域的扩展值,最后将两者结合计算出包含安全区域的实际位置和大小。具体代码如下:
Rect bounds = frameNode.getBounds();
Rect expandSafeArea = frameNode.getExpandSafeArea();
Rect finalBounds = new Rect(
bounds.left - expandSafeArea.left,
bounds.top - expandSafeArea.top,
bounds.right + expandSafeArea.right,
bounds.bottom + expandSafeArea.bottom
);
这样,finalBounds
即为包含expandSafeArea的FrameNode的实际位置和大小。