如何实现图片预览,有人知道吗?

需要一个图片点击放大,包含手势缩放的组件

HarmonyOS
2024-07-22 12:02:57
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
Heiang

通过stack做图片点击后展示的蒙层,点击蒙层后可退出预览,预览图中可对图片进行放大缩小等基础操作。

参考代码:

@Entry 
@Component 
struct Index { 
  @State visible: Visibility = Visibility.None 
  @State scaleValue: number = 1 
  @State pinchValue: number = 1 
  @State pinchX: number = 0 
  @State pinchY: number = 0 
  @State count: number = 0 
  @State offsetX: number = 0 
  @State offsetY: number = 0 
  @State positionX: number = 0 
  @State positionY: number = 0 
 
  build() { 
    Stack() { 
      Row() { 
        Column() { 
          Image($r('app.media.icon')) 
            .width(100) 
            .height(100) 
            .onClick(() => { 
              console.log("hit me!") 
              if (this.visible == Visibility.Visible) { 
                this.visible = Visibility.None 
              } else { 
                this.visible = Visibility.Visible 
              } 
            }) 
        } 
        .width('100%') 
        .justifyContent(FlexAlign.Center) 
        .alignItems(HorizontalAlign.Center) 
      } 
      .height('100%') 
 
 
      Text('') 
        .onClick(() => { 
          if (this.visible == Visibility.Visible) { 
            this.visible = Visibility.None 
          } else { 
            this.visible = Visibility.Visible 
          } 
        }) 
        .width('100%') 
        .height('100%')// 透明度可以自己调节一下 
        .opacity(0.16) 
        .backgroundColor(0x000000) 
        .visibility(this.visible) 
 
      Column() { 
        Image($r('app.media.icon')) 
          .width(300) 
          .height(300) 
          .draggable(false) 
          .visibility(this.visible) 
          .scale({ x: this.scaleValue, y: this.scaleValue, z: 1 }) 
          .translate({ x: this.offsetX, y: this.offsetY, z: 0 }) 
          .gesture( 
            GestureGroup(GestureMode.Parallel, 
              PinchGesture({ fingers: 2 }) 
                .onActionStart((event?: GestureEvent) => { 
                  console.info('Pinch start') 
                }) 
                .onActionUpdate((event?: GestureEvent) => { 
                  if (event) { 
                    this.scaleValue = this.pinchValue * event.scale 
                    this.pinchX = event.pinchCenterX 
                    this.pinchY = event.pinchCenterY 
                  } 
                }) 
                .onActionEnd(() => { 
                  this.pinchValue = this.scaleValue 
                  console.info('Pinch end') 
                }), 
 
              PanGesture() 
                .onActionUpdate((event?: GestureEvent) => { 
                  if (event) { 
                    this.offsetX = this.positionX + event.offsetX 
                    this.offsetY = this.positionY + event.offsetY 
                  } 
                  console.info('pan update') 
                }) 
                .onActionEnd(() => { 
                  this.positionX = this.offsetX 
                  this.positionY = this.offsetY 
                  console.info('pan end') 
                }) 
            ) 
          ) 
      } 
    } 
  } 
}
分享
微博
QQ
微信
回复
2024-07-22 19:48:42
相关问题
如何实现翻页功能,有人知道吗
1575浏览 • 1回复 待解决
如何实现振动,有人知道吗
897浏览 • 2回复 待解决
如何实现http长连接,有人知道吗
1361浏览 • 1回复 待解决
如何实现镂空效果,有人知道吗?
146浏览 • 1回复 待解决
热重载该如何实现有人知道吗
507浏览 • 1回复 待解决
$$语法如何使用?有人知道吗
191浏览 • 1回复 待解决
如何保存faultLogger ,有人知道吗
284浏览 • 1回复 待解决
如何跳出ForEach,有人知道吗
1613浏览 • 1回复 待解决
如何发送短信,有人知道吗?
1557浏览 • 1回复 待解决
图片压缩并保存方法,有人知道吗
510浏览 • 0回复 待解决
如何实现防截屏功能,有人知道吗
1607浏览 • 1回复 待解决
如何获取wifi列表,有人知道吗
188浏览 • 1回复 待解决
如何定义dialog动画,有人知道吗?
1536浏览 • 1回复 待解决
如何使用快速修复,有人知道吗
381浏览 • 1回复 待解决
如何引用HSP库,有人知道吗?
1355浏览 • 1回复 待解决
导航栏如何适配,有人知道吗?
1498浏览 • 0回复 待解决
如何获取windowStage,有人知道吗
66浏览 • 1回复 待解决
webview组件demo ,有人知道吗
521浏览 • 1回复 待解决
taskpool 使用问题,有人知道吗
569浏览 • 1回复 待解决
如何开启AOT编译模式,有人知道吗
1531浏览 • 1回复 待解决
IP地址如何转化,有人知道吗
368浏览 • 1回复 待解决
如何查询设备类型?有人知道吗
99浏览 • 1回复 待解决
如何获取组件高度,有人知道吗
1740浏览 • 1回复 待解决
ArkTS要如何使用this,有人知道吗
145浏览 • 1回复 待解决