可双指缩放的图片控件_PhoteView

导包

在project的gradle中
allprojects {
    repositories {
        jcenter()
        maven { url "https://jitpack.io" }
    }
}

在module的gradle中
    //photoView
    compile 'com.github.chrisbanes:PhotoView:2.1.3'
    //图片加载框架
    compile 'com.squareup.picasso:picasso:2.5.2'

布局

//注意不能设置src,否则报错
    <com.github.chrisbanes.photoview.PhotoView
        android:id="@+id/iv_show_photo"
        android:scaleType="fitCenter"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在小程序的 canvas 中实现双指缩放图片的功能,你可以按照以下步骤进行操作: 1. 创建一个 canvas 组件,并设置好宽度和高度。 ``` <canvas id="myCanvas" style="width: 300px; height: 300px;"></canvas> ``` 2. 在小程序的 js 文件中获取到 canvas 组件的上下文对象。 ``` const ctx = wx.createCanvasContext('myCanvas'); ``` 3. 加载并绘制图片到 canvas 上。 ``` const imgPath = '图片路径'; // 替换为你的图片路径 wx.getImageInfo({ src: imgPath, success(res) { const imgWidth = res.width; // 图片原始宽度 const imgHeight = res.height; // 图片原始高度 // 计算图片缩放后的初始宽度和高度 const initialWidth = 300; const initialHeight = (imgHeight / imgWidth) * initialWidth; ctx.drawImage(imgPath, 0, 0, initialWidth, initialHeight); ctx.draw(); } }); ``` 4. 监听 canvas 组件的触摸事件,实现双指缩放功能。 ``` let touchStartDistance = 0; // 记录触摸开始时两指之间的距离 let scale = 1; // 记录当前缩放比例 wx.canvasTouchStart((e) => { if (e.touches.length >= 2) { const x1 = e.touches[0].x; const y1 = e.touches[0].y; const x2 = e.touches[1].x; const y2 = e.touches[1].y; // 计算两指之间的距离 touchStartDistance = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2); } }); wx.canvasTouchMove((e) => { if (e.touches.length >= 2) { const x1 = e.touches[0].x; const y1 = e.touches[0].y; const x2 = e.touches[1].x; const y2 = e.touches[1].y; // 计算两指之间的距离 const touchMoveDistance = Math.sqrt((x2 - x1) ** 2 + (y2 - y1) ** 2); // 计算缩放比例 const newScale = touchMoveDistance / touchStartDistance; // 更新缩放比例 scale = newScale; // 清空 canvas ctx.clearRect(0, 0, 300, 300); // 根据缩放比例绘制图片 const newWidth = initialWidth * scale; const newHeight = initialHeight * scale; ctx.drawImage(imgPath, 0, 0, newWidth, newHeight); ctx.draw(); } }); ``` 请根据你的实际需求,将上述代码片段进行适当修改和组合。希望对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值