创造一款安卓自定义控件_任意4顶点裁剪框2_为裁剪框添加放大镜功能

                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();
                }

要点在于图像定位上,先把之前触摸点在控件上的位置拿到,然后排除上下左右的黑边,最后通过缩放系数放大触摸点到图片对应的位置,缩放中心定于放大中心,然后“图片动放大镜不动”——反向移动图片到缩放中心,即可获取原比例的局部图像了,此时你可以添加代码,按照你需要的放大系数放大。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值