一个支持放大缩小的imageview链接
/**
* 手势体验极棒但使用简单的ImageView控件,实现了手势放大缩小
* https://github.com/boycy815/PinchImageView
* */
关键代码
// 获取触摸点的坐标 x, y
float x = e.getX();
float y = e.getY();
// 目标点的坐标
float dst[] = new float[2];
// 获取到ImageView的matrix
Matrix imageMatrix = getImageMatrix();
// 创建一个逆矩阵
Matrix inverseMatrix = new Matrix();
// 求逆,逆矩阵被赋值
imageMatrix.invert(inverseMatrix);
// 通过逆矩阵映射得到目标点 dst 的值
inverseMatrix.mapPoints(dst, new float[]{x, y});
float dstX = dst[0];
float dstY = dst[1];
// 获取图片的大小
float drawWidth = getDrawable().getBounds().width();
float drawHeight = getDrawable().getBounds().height();
float deviceX = dstX/drawWidth;
float deviceY = dstY/drawHeight;
// 判断dstX, dstY在Bitmap上的位置即可
if (!(deviceX<0 || deviceX>1 || deviceY<0 || deviceY>1)){
imageOnClickListener.onClick(deviceX,deviceY);
}
网友评论