如何启用捏在图像缩放切换?缩放、图像

2023-09-07 23:48:10 作者:若你离去,谁许我余生幸福

应用程序的接口包括以下项目。

The application's interface consists of the following items.

填满整个屏幕的图像切换。 A 接下来按钮。 A previous 按钮。 An image switcher that fills the entire screen. A next button. A previous button.

如何使用捏缩放图像切换

How can I use the pinch zooming in the image Switcher?

推荐答案

实施捏缩放手势

夹紧变焦手势是类似于拖动手势,除了启动时的第二手指是pssed到屏幕$ P $( ACTION_POINTER_DOWN )。

The pinch zoom gesture is similar to the drag gesture, except it starts when the second finger is pressed to the screen (ACTION_POINTER_DOWN).


case MotionEvent.ACTION_POINTER_DOWN:
oldDist = spacing(event);
Log.d(TAG, "oldDist=" + oldDist);
if (oldDist > 10f) {
savedMatrix.set(matrix);
midPoint(mid, event);
mode = ZOOM;
Log.d(TAG, "mode=ZOOM" );
}
break;


case MotionEvent.ACTION_MOVE:
if (mode == DRAG) {
// ...
}
else if (mode == ZOOM) {
float newDist = spacing(event);
Log.d(TAG, "newDist=" + newDist);
if (newDist > 10f) {
matrix.set(savedMatrix);
float scale = newDist / oldDist;
matrix.postScale(scale, scale, mid.x, mid.y);
}
}
break;

当我们得到用于第二手指向下事件,计算并记住两根手指之间的距离。在我的测试,机器人有时会告诉我(错误地),有两个手指分别在$几乎一模一样的位置pssed按住p $。所以我增加了一个检查来忽略该事件,如果距离大于像素的一些任意数量较小。如果比这更大的,我们记住了当前转换矩阵,计算出两个手指的中点,并启动变焦。

When we get the down event for the second finger, we calculate and remember the distance between the two fingers. In my testing, Android would sometimes tell me (incorrectly) that there were two fingers pressed down in almost exactly the same position. So I added an check to ignore the event if the distance is smaller than some arbitrary number of pixels. If it’s bigger than that, we remember the current transformation matrix, calculate the midpoint of the two fingers, and start the zoom.

当一个移动事件到达而我们在缩放模式下,我们重新计算手指之间的距离。如果它太小,则事件被忽略,否则我们恢复的变换矩阵和缩放围绕中点图像

When a move event arrives while we’re in zoom mode, we calculate the distance between the fingers again. If it’s too small, the event is ignored, otherwise we restore the transformation matrix and scale the image around the midpoint.

比例是简单地在新的距离由旧距离除以的比率。如果新的距离越大(即,手指已经得到进一步分开),则规模将大于1,使图像大。如果是较小的(指靠得更近),则规模将小于1,使得图像变小。当然,如果一切是相同的,规模是等于1和图像不被改变。

The scale is simply the ratio of the new distance divided by the old distance. If the new distance is bigger (that is, the fingers have gotten further apart), then the scale will be greater than 1, making the image bigger. If it’s smaller (fingers closer together), then the scale will be less than one, making the image smaller. And of course if everything is the same, the scale is equal to 1 and the image is not changed.

现在让我们定义的间距()和中点()方法。

Now let’s define the spacing() and midPoint() methods.

两点之间的距离

要找出两个手指分开多远,我们首先构造一个向量(X,Y),这是在两个点之间的差。

To find out how far apart two fingers are, we first construct a vector (x, y) which is the difference between the two points.

然后我们使用了欧氏距离公式计算间距:

Then we use the formula for Euclidean distance to calculate the spacing:


private float spacing(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return FloatMath.sqrt(x * x + y * y);
}

该点的顺序并不重要,因为当我们正视他们的任何负面迹象都将丢失。请注意,所有的数学是使用Java的浮点类型进行。虽然有些Android设备可能没有浮点硬件,我们不这样做往往不够担心其性能。

The order of the points doesn’t matter because any negative signs will be lost when we square them. Note that all math is done using Java’s float type. While some Android devices may not have floating point hardware, we’re not doing this often enough to worry about its performance.

二点中点

计算中的两个点的中间点更容易:

Calculating a point in the middle of two points is even easier:


private void midPoint(PointF point, MotionEvent event) {
float x = event.getX(0) + event.getX(1);
float y = event.getY(0) + event.getY(1);
point.set(x / 2, y / 2);
}

我们所要做的就是把他们的X和Y坐标的平均值。以避免垃圾回收,可以在应用程序中导致明显的停顿,我们重新使用现有的对象,以将结果存储,而不是分配和每次返回一个新的。试试你的手机上,现在运行的程序。图像用一个手指拖动,并通过或缩小捏两个手指放大它。为了获得最佳效果,不要让你的手指比一英寸左右的间隔更近。否则,你就会开始碰到一些我刚才提到的API中的错误。

All we do is take the average of their X and Y coordinates. To avoid garbage collections that can cause noticeable pauses in the application, we reuse an existing object to store the result rather than allocating and returning a new one each time. Try running the program now on your phone. Drag the image with one finger, and zoom it by pinching two fingers in or out. For best results, don’t let your fingers get closer than an inch or so apart. Otherwise you’ll start to run into some of those bugs in the API I mentioned earlier.

这是摘录从您好,Android的第三版,由务实书架公布。欲了解更多信息或购买一本平装或PDF副本,请访问 http://www.pragprog.com/titles/eband3