onScale和Canvas - 如何缩放图像后调整原点?缩放、原点、图像、onScale

2023-09-05 08:37:20 作者:洪荒少女~

我有一个自定义组件 MyView.java - 它会显示一个可滚动的,可扩展的图像(重presenting一板一拼字游戏般的文字游戏):

I have a very simple test app with a custom component MyView.java - which displays a scrollable and scalable image (representing a board in a Scrabble-like word game):

1)在我的听众规模我调整比例系数:

1) In my scale listener I adjust the scale factor:

public boolean onScale(ScaleGestureDetector detector) {
    mScale *= detector.getScaleFactor();

    // XXX how to adjust mOffsetX and mOffsetY ? XXX

    constrainZoom();
    constrainOffsets();
    invalidate();
    return true;
}

2),然后在我的自定义组件的绘制方法,我申请的平移和缩放系数:

2) And then in the drawing method of my custom component I apply the translation and scale factor:

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

    canvas.translate(mOffsetX, mOffsetY);
    canvas.scale(mScale, mScale);
    gameBoard.setBounds(
        0, 
        0, 
        gameBoard.getIntrinsicWidth(),
        gameBoard.getIntrinsicHeight()
    );
    gameBoard.draw(canvas);  
}

不幸的是,有捏的手势缩放的时候似乎是一个小错误 -

Unfortunately, there seems to be a small bug when scaling with pinch gesture -

我可以看到,规模和边界是正确的尺寸放大后,但图像的偏移不是。

I can see, that the scale and boundaries are of correct size after zooming, but the offset of the image is not.

这个问题变得更糟,当捏手势的焦点是远远0,0点的画面。

The problem get worse, when the focus point of the pinch gesture is far from 0, 0 point of the screen.

这是一个有点难以用言语来形容的问题,但是当你检查出的从GitHub上我的测试项目你会马上看到它(你可以随时双击复位偏移和标度)。

It is a bit difficult to describe the problem in words, but when you check out my test project from GitHub you will see it immediately (and you can always double tap to reset the offset and the scale).

这可能是一个常见的​​问题来解决它的标准方式,但我一直没能找到它。

This is probably a common problem with a standard way to solve it, but I haven't been able to find it yet.

董事会图像 CC BY-SA由Denelson83 和code基于href="http://www.tiemenschut.com/support-fling-gesture-bounce-effect/" rel="nofollow">如何-支持一扔手势反弹效应