如何实现镂空效果,有人知道吗?

如何实现镂空效果

HarmonyOS
2024-07-23 10:33:31
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
pfuchenlu

利用canvas绘制镂空圆形然后使用Stack组件叠加在需要透明展示的区域上,参考代码如下:

@Entry 
@Component 
struct Page { 
  @State message: string = 'Hello World'; 
  private settings: RenderingContextSettings = new RenderingContextSettings(true) 
  private context: CanvasRenderingContext2D = new CanvasRenderingContext2D(this.settings) 
  @State circleCenterX: number = 0 
  @State circleCenterY: number = 0 
  @State circleRadius: number = 100 
 
  build() { 
    Row() { 
      Column() { 
        Stack() { 
          Image($r('app.media.startIcon')).height(300) 
          // 使用Canvas绘制遮罩覆盖在图片、相机等上面 
          Canvas(this.context) 
            .width('100%') 
            .height('100%') 
            .backgroundColor('#00000000') 
            .onReady(() => { 
              this.circleCenterX = this.context.width / 2 
              this.circleCenterY = this.context.height / 2 
              this.context.fillStyle = '#aa000000' 
              // 绘制原型路径进行半透明填充 
              this.context.beginPath() 
              this.context.moveTo(0, 0) 
              this.context.lineTo(0, this.context.height) 
              this.context.lineTo(this.context.width, this.context.height) 
              this.context.lineTo(this.context.width, 0) 
              this.context.lineTo(0, 0) 
              this.context.arc(this.circleCenterX, this.circleCenterY, this.circleRadius, 0, Math.PI * 2) 
              this.context.fill() 
              this.context.closePath() 
            }) 
        }.width('1456px') 
        .height('1456px') 
      } 
      .width('100%') 
    } 
    .height('100%') 
  } 
}
分享
微博
QQ
微信
回复
2024-07-23 18:29:46
相关问题
如何实现振动,有人知道吗
895浏览 • 2回复 待解决
如何实现图片预览,有人知道吗
145浏览 • 1回复 待解决
如何实现翻页功能,有人知道吗
1554浏览 • 1回复 待解决
如何实现http长连接,有人知道吗
1354浏览 • 1回复 待解决
热重载该如何实现有人知道吗
499浏览 • 1回复 待解决
如何实现防截屏功能,有人知道吗
1586浏览 • 1回复 待解决
如何跳出ForEach,有人知道吗
1598浏览 • 1回复 待解决
如何发送短信,有人知道吗?
1548浏览 • 1回复 待解决
$$语法如何使用?有人知道吗
191浏览 • 1回复 待解决
如何保存faultLogger ,有人知道吗
284浏览 • 1回复 待解决
如何获取windowStage,有人知道吗
61浏览 • 1回复 待解决
如何使用快速修复,有人知道吗
377浏览 • 1回复 待解决
如何定义dialog动画,有人知道吗?
1526浏览 • 1回复 待解决
如何引用HSP库,有人知道吗?
1346浏览 • 1回复 待解决
导航栏如何适配,有人知道吗?
1494浏览 • 0回复 待解决
如何获取wifi列表,有人知道吗
188浏览 • 1回复 待解决
ArkTS要如何使用this,有人知道吗
139浏览 • 1回复 待解决
IP地址如何转化,有人知道吗
368浏览 • 1回复 待解决
如何获取系统电量,有人知道吗
1700浏览 • 1回复 待解决
如何查询设备类型?有人知道吗
94浏览 • 1回复 待解决
如何获取组件高度,有人知道吗
1725浏览 • 1回复 待解决
taskpool 使用问题,有人知道吗
568浏览 • 1回复 待解决
webview组件demo ,有人知道吗
521浏览 • 1回复 待解决
如何开启AOT编译模式,有人知道吗
1516浏览 • 1回复 待解决