画布绘图和居中缩放

发布于 2025-01-01 21:45:19 字数 677 浏览 5 评论 0原文

我正在使用 Google 教程为我的画布实现缩放功能。我正在画布上绘制多个项目,并且可以通过 Canvas.translate(dx,dy) 将其与项目一起移动。为了让我确定画布平移后是否触摸了这些项目,我采用了用于 Canvas.translate(dx,dy) 的画布偏移量并减去触摸位置。一旦我在图片中引入缩放,它就会变得有点棘手。如果我使用 Canvas.scale(sx,sy) 并执行 (touchX-offsetX)/scaleFactor ,则缩放后触摸位置的转换效果完美。但是,如果包含枢轴位置 Canvas.scale(sx,sy,px,py) 上面的代码将不起作用。作为触摸翻译的一部分,我该如何考虑枢轴位置?我通过执行以下操作来设置我的枢轴位置,

    public boolean onScaleBegin(ScaleGestureDetector detector) {
    mScaleX = detector.getFocusX();
    mScaleY = detector.getFocusY();
    return true;
}

任何帮助将不胜感激。

I'm playing with a Google tutorial to implement zooming capabilities to my canvas. I'm drawing several items onthe canvas and I'm able to move it via Canvas.translate(dx,dy) along with the items. In order for me to determine if these items are touched after the canvas translation, I take the canvas offset used for Canvas.translate(dx,dy) and subtract the touch position. Once I introduce scaling into the picture, it get's a little tricky. If I use Canvas.scale(sx,sy) and do (touchX-offsetX)/scaleFactor the translation of the touch position after the scale works perfect. However, if include a pivot position Canvas.scale(sx,sy,px,py) the code above those not work. How do I go about factoring in the pivot position as a part of my touch translation? I set my pivot position by doing the following,

    public boolean onScaleBegin(ScaleGestureDetector detector) {
    mScaleX = detector.getFocusX();
    mScaleY = detector.getFocusY();
    return true;
}

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

寒江雪… 2025-01-08 21:45:19

简单的解决方案

 public class ScalableImageView extends View {
    Matrix drawMatrix = new Matrix();
    ScaleGestureDetector detector;
    public ScalableImageView(Context context, AttributeSet attrs) { 
         @Override
        public boolean onScale(ScaleGestureDetector detector) {

            drawMatrix.postScale(detector.getScaleFactor(), detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY());
            invalidate();
            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            return drawMatrix != null;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {

        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        canvas.concat(drawMatrix);
        // ... draw here
        // for example drawable.draw()
        canvas.restore();
    }

}

}

Simple solution

 public class ScalableImageView extends View {
    Matrix drawMatrix = new Matrix();
    ScaleGestureDetector detector;
    public ScalableImageView(Context context, AttributeSet attrs) { 
         @Override
        public boolean onScale(ScaleGestureDetector detector) {

            drawMatrix.postScale(detector.getScaleFactor(), detector.getScaleFactor(), detector.getFocusX(), detector.getFocusY());
            invalidate();
            return true;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            return drawMatrix != null;
        }

        @Override
        public void onScaleEnd(ScaleGestureDetector detector) {

        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        canvas.save();
        canvas.concat(drawMatrix);
        // ... draw here
        // for example drawable.draw()
        canvas.restore();
    }

}

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文