if (null != selectPoint && MotionEvent.ACTION_UP != mCurrentAction) {
Path scopeArea = new Path();
int w = (int) MeasurelUtils.convertDpToPixel(100, getContext());
int h = (int) MeasurelUtils.convertDpToPixel(100, getContext());
//手指碰到绘制区域要避开
RectF rect = new RectF(0, 0, w, h);
if (rect.contains(selectPoint.x, selectPoint.y)) {
rect = new RectF(mWidth - w, 0, mWidth, h);
}
scopeArea.addRoundRect(rect, 10f, 10f,
Path.Direction.CCW);
canvas.save();
canvas.clipPath(scopeArea);
float offsetX = ((mWidth - mBitmap.getWidth() * mScale) / 2f); //去除计算中纳入的控件黑边,以防图像在控件缩放后两边有黑边时,导致裁剪时把偏移量多算了控件上的黑边范围导致严重误差
Point point = new Point((int) ((selectPoint.x - offsetX) * (1f / mScale)),
(int) ((selectPoint.y - (mHeight / 2f - mBitmap.getHeight() / 2f * mScale)) * (1f / mScale)));
Matrix scopeMatrix = new Matrix();
scopeMatrix.postScale(mScale * 2f, mScale * 2f, (point.x + w / 2), (point.y + h / 2));
scopeMatrix.setTranslate((-point.x + w / 2), (-point.y + h / 2));
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(mBitmap, scopeMatrix, null);
mSelectorRectPointPaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(scopeArea, mSelectorRectPointPaint);
canvas.drawLine(rect.centerX() - w / 10, rect.centerY(), rect.centerX() + w / 10, rect.centerY(), mSelectorRectPointPaint);
canvas.drawLine(rect.centerX(), rect.centerY() - w / 10, rect.centerX(), rect.centerY() + w / 10, mSelectorRectPointPaint);
canvas.restore();
}
要点在于图像定位上,先把之前触摸点在控件上的位置拿到,然后排除上下左右的黑边,最后通过缩放系数放大触摸点到图片对应的位置,缩放中心定于放大中心,然后“图片动放大镜不动”——反向移动图片到缩放中心,即可获取原比例的局部图像了,此时你可以添加代码,按照你需要的放大系数放大。