如何获得度的旋转从双指多触摸事件? [英] How to get degrees of rotation from a two-finger multi-touch event?

查看:125
本文介绍了如何获得度的旋转从双指多触摸事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,你可以用做一个手指平移或两个手指规模的ImageView的。它工作正常。我已经扩展它来处理旋转,其行为是造成一些混乱。

I have an ImageView that you can use to do a one-finger pan, or a two finger scale. It works fine. I've extended it to handle rotation, and its behaviour is causing some confusion.

当有一个多点触摸事件的这个方法被调用,它应该返回一个旋转角度以度。它没有做什么,我所期望的。

When there's a multi-touch event this method gets called, which should return an angle of rotation in degrees. It is not doing what I expect.

private int fingersAngle(MotionEvent event)
{
    float x = event.getX(0) - event.getX(1);
    float y = event.getY(0) - event.getY(1);
    int degrees = (int)(Math.toDegrees(Math.atan2(y, x)));
    return degrees;
}

由于我旋转两个手指,我期望像输出...

As I rotate the two fingers I'd expect outputs like...

158 166 168 169 174 176 179 181等

158 166 168 169 174 176 179 181 etc

但我实际上得到的是更多像这样的:

But what I actually get is more like this:

158 166 -179 179 179 -179 -179

158 166 -179 179 -179 179 -179

这个问题似乎与体征。请问这个方法知道它是否是180或-180,或90或-270?图像经常转动走错了路,然后突然跳跃,并开始旋转相反的方向。更糟糕的是,它最初的旋转方向是随机的。

The problem seems to be with signs. How does this method know whether it's 180 or -180, or 90 or -270? The image often rotates the wrong way, and then suddenly jumps and starts rotating the opposite way. Even worse, the direction it initially rotates is effectively random.

我测试使用的是Nexus One的应用程序,同时也看到了临维加平板电脑同样的问题。这两款器件工作确定与谷歌地图的3D旋转屏幕(如果有点神经质有时),这样的证据无法表明硬件的限制。

I'm testing the app using a Nexus One, but also see the same problem on an Advent Vega tablet. Both devices work ok with Google Maps in 3D to rotate the screen (if a bit jumpy sometimes) so the evidence doesn't suggest a hardware limitation.

一个次要问题是,当两根手指近似垂直或水平对齐的角度根本不改变,所以旋转棒约10-20度顶部/底部/左/右

A secondary problem is that when the two fingers are approximately vertically or horizontally aligned the angle simply doesn't change, so the rotation "sticks" for about 10-20 degrees top/bottom/left/right.

目前我正在做一个检查,看看是否角度突然改变了数额巨大的,如果是的话,从360丑得要命减去它,但它可以帮助一点点。

Currently I'm doing a check to see if the angle has suddenly changed a huge amount, and if so, to subtract it from 360. Ugly as hell, but it helps a little.

希望你们中的一个之前已经看到了这一点,并且知道如何从一个多点触摸手势提取旋转角度。

Hopefully one of you has seen this before and knows how to extract the angular rotation from a multi-touch gesture.

有些东西在Android中是那么容易这是惊人的,但这样的东西,这是严重的痛苦。

Some things in Android are so easy it's amazing, but stuff like this is seriously painful.

推荐答案

为使这项工作想用户所期望的那样,你必须保持一定的状态。

To make this work like users expect, you must keep some state.

这是反正切只会告诉你两点&mdash之间的角度;该系统的瞬时快照。但是,你正在执行一个动画—发生的事情随着时间的推移

An arc-tangent will only tell you the angle between two points—an instantaneous snapshot of the system. But you are performing an animation—something that happens over time.

所以,我认为你是在正确的轨道你的丑的解决方案,尝试检测大的变化的角度。你真正应该被跟踪随着时间的推移是每个手指的位置。

So, I think that you are on the right track with your "ugly" solution that tries to detect large changes in the angle. What you really should be tracking over time is the position of each finger.

假设一个用户正在使用他们的拇指和食指的手势。你需要确保他们的数字是一致的重新presented,例如:拇指总是重新被psented $ P $(X <子> 0 ,Y <子> 0 ),和食指由(x <子> 1 ,Y <子> 1 )。

Suppose a user is using their thumb and index finger to gesture. You need to make sure that their digits are consistently represented, e.g. the thumb is always represented by (x0, y0), and the index finger by (x1, y1).

这可能是因为该设备有一定的规则,如何协调各报告。例如,也许它会总是报告在该事件的0的时隙左上角坐标,以及右下的1的时隙坐标。但是,如果用户从右下移动其拇指左上角,你必须确保你还是把它当作在(X <分> 1 ,Y <子> 1 )你角公式,即使该设备是目前报告其为(x <子> 0 ,Y <子> 0 )事件。否则,图像会出现快速翻转180度;,来回。

It's likely that the device has some rule how each coordinate is reported. For example, maybe it will always report the upper-left coordinate in the "0" slot of the event, and the lower-right coordinate in the "1" slot. But if the user moves their thumb from lower-right to upper-left, you must make sure that you are still treating it as (x1, y1) in your angle formula, even though the device is now reporting it as (x0, y0) in the event. Otherwise, the image will appear to quickly flip 180°, back and forth.

如果设备不适合你跟踪手指,你就必须拿出一个启发式的猜测。一个简单的的previous负责协调更接近当前坐标将是一个良好的开端,但我可以看到需要的东西票友,如果事件的分辨率较差。

If the device doesn't track fingers for you, you'll have to come up with a heuristic for guessing. A simple, "which of the previous coordinates is closer to the current coordinate" would be a good place to start, but I could see the need for something fancier if the resolution of events is poor.

这篇关于如何获得度的旋转从双指多触摸事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆