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

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

HarmonyOS
2024-07-22 12:02:57
709浏览
收藏 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') 
                }) 
            ) 
          ) 
      } 
    } 
  } 
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
  • 66.
  • 67.
  • 68.
  • 69.
  • 70.
  • 71.
  • 72.
  • 73.
  • 74.
  • 75.
  • 76.
  • 77.
  • 78.
  • 79.
  • 80.
  • 81.
  • 82.
  • 83.
  • 84.
  • 85.
  • 86.
  • 87.
  • 88.
  • 89.
  • 90.
  • 91.
  • 92.
  • 93.
  • 94.
  • 95.
  • 96.
分享
微博
QQ
微信
回复
2024-07-22 19:48:42
相关问题
如何实现振动,有人知道吗
1978浏览 • 2回复 待解决
如何实现镂空效果,有人知道吗?
1044浏览 • 1回复 待解决
如何实现翻页功能,有人知道吗
2773浏览 • 1回复 待解决
如何对网络图片处理,有人知道吗
880浏览 • 1回复 待解决
热重载该如何实现有人知道吗
1327浏览 • 1回复 待解决
图片压缩并保存方法,有人知道吗
1411浏览 • 0回复 待解决
如何获取windowStage,有人知道吗
1555浏览 • 1回复 待解决
如何实现http长连接,有人知道吗
2391浏览 • 1回复 待解决
如何实现防截屏功能,有人知道吗
2693浏览 • 1回复 待解决
如何查询设备类型?有人知道吗
1114浏览 • 1回复 待解决
ArkTS要如何使用this,有人知道吗
1368浏览 • 1回复 待解决
如何获取系统电量,有人知道吗
2875浏览 • 1回复 待解决
如何获取组件高度,有人知道吗
2997浏览 • 1回复 待解决
IP地址如何转化,有人知道吗
1287浏览 • 1回复 待解决
如何发送短信,有人知道吗?
2854浏览 • 1回复 待解决
如何跳出ForEach,有人知道吗
2979浏览 • 1回复 待解决
如何保存faultLogger ,有人知道吗
1488浏览 • 1回复 待解决
$$语法如何使用?有人知道吗
1526浏览 • 1回复 待解决
clientid相关问题,有人知道吗
2752浏览 • 1回复 待解决
如何调整内存大小,有人知道吗
869浏览 • 1回复 待解决
如何拉起相机界面,有人知道吗
2527浏览 • 1回复 待解决
如何压缩字符串,有人知道吗
1448浏览 • 1回复 待解决
如何引用HSP库,有人知道吗?
2444浏览 • 1回复 待解决
如何获取wifi列表,有人知道吗
1645浏览 • 1回复 待解决
如何使用快速修复,有人知道吗
1431浏览 • 1回复 待解决