中国优质的IT技术网站
专业IT技术创作平台
IT职业在线教育平台
实现一个背景透明,中间矩形镂空的界面
微信扫码分享
import display from '@ohos.display'; import { Logger } from '../common/Logger'; const log: Logger = new Logger('CustomPage'); @Entry @Component struct CustomPage { //用来配置CanvasRenderingContext2D对象的参数,包括是否开启抗锯齿,true表明开启抗锯齿。 private settings: RenderingContextSettings = new RenderingContextSettings(true) //用来创建CanvasRenderingContext2D对象,通过在canvas中调用CanvasRenderingContext2D对象来绘制。 private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) private pointX: number = 30; private pointY: number = 150; private mScanWidth: number = 300; private mScreenWidth: number = 1080; private mScreenHeight: number = 1920; @State mIsInit: boolean = false; onPageShow(): void { log.info('onPageShow'); // 获取屏幕参数 let defaultDisplay = display.getDefaultDisplaySync(); this.mScreenWidth = px2vp(defaultDisplay.width); this.mScreenHeight = px2vp(defaultDisplay.height); // 计算扫描框边长 this.mScanWidth = this.mScreenWidth * 0.8; // 计算扫描框左上角位置 this.pointX = (this.mScreenWidth - this.mScanWidth) / 2; this.pointY = (this.mScreenHeight - this.mScanWidth) / 2; log.info(`init, pointX: ${this.pointX}, pointY: ${this.pointY}, mScanWidth: ${this.mScanWidth}`); this.mIsInit = true; } build() { Stack() { Text('这里是底色').fontSize(180).width('100%').height('100%') if (this.mIsInit) { // 数据初始化完后才需要显示 Canvas(this.context) .width('100%') .height('100%') .onReady(() => { this.context.fillStyle = '#33000000'; this.context.fillRect(0, 0, this.mScreenWidth, this.mScreenHeight); this.context.clearRect(this.pointX, this.pointY, this.mScanWidth, this.mScanWidth); }) } }.width('100%') .height('100%') } }