如何code多点触控多点、触控、code

2023-09-04 10:28:53 作者:谁能与我同醉

所以,我开发必须处理多点触控的应用程序。基本上我想要的旋转单点触摸(这是没有问题的)。而多点触控滚动。

So I'm developing an application that must handle multitouch. Basically I want single touch for rotating ( this is no problem ). And multitouch for scrolling.

我有基本的code的,但我有问题时,从单一转向多点触控,副verca,会发生。基本上移动会摇晃,因为多点(两个手指)与单个手指的绝对位置的中间位置是在距离。所以,如果我有两个手指放在屏幕上,他们提出了一个中间位置,然后抬起一个手指,它会像从中间位置到绝对一根手指位置的快速移动。这将是我不希望的运动。

I have the basic code in, but I'm having problems when the shift from single to multitouch, and vice verca, occur. Basically the movement will jolt because the median position of the multitouch ( two fingers ) and the absolute position of the single finger are at a distance. So if I have two fingers on the screen, they make up a median position, and then lift one finger, it would be like a quick movement from that median position to the absolute single finger position. This will be the movement that I don't want.

这是我的code:

@Override
public boolean onTouchEvent( MotionEvent event ) {
    float xEvent[] = new float[ 2 ];
    float yEvent[] = new float[ 2 ];
    switch( event.getPointerCount() ) {
        case 1:
            xEvent[ 0 ] = event.getX( 0 );
            yEvent[ 0 ] = event.getY( 0 );
            switch( event.getAction() ) {
                case MotionEvent.ACTION_DOWN:
                    camera.onTouchDown( xEvent[ 0 ], yEvent[ 0 ] );
                    return true;
                case MotionEvent.ACTION_MOVE:
                    camera.onTouchRotate( xEvent[ 0 ], yEvent[ 0 ] );
                    return true;
                default: return super.onTouchEvent( event );
            }
        case 2:
            xEvent[ 0 ] = event.getX( 0 );
            yEvent[ 0 ] = event.getY( 0 );
            xEvent[ 1 ] = event.getX( 1 );
            yEvent[ 1 ] = event.getY( 1 );
            switch( event.getAction() ) {
                case MotionEvent.ACTION_DOWN:
                    camera.onTouchDown( ( ( xEvent[ 0 ] + xEvent[ 1 ] ) / 2 ), ( ( yEvent[ 0 ] + yEvent[ 1 ] ) / 2 ) );
                    return true;
                case MotionEvent.ACTION_MOVE:
                    camera.onTouchSlide( ( ( xEvent[ 0 ] + xEvent[ 1 ] ) / 2 ), ( ( yEvent[ 0 ] + yEvent[ 1 ] ) / 2 ) );
                    return true;
                case MotionEvent.ACTION_POINTER_1_UP:
                    camera.onTouchDown( xEvent[ 1 ], yEvent[ 1 ] );
                    return true;
                case MotionEvent.ACTION_POINTER_2_UP:
                    camera.onTouchDown( xEvent[ 0 ], yEvent[ 0 ] );
                    return true;
                default: return super.onTouchEvent( event );
            }
        default: return false;
    }
}

相机的onTouchDown功能只是设置的触摸移动的第一个值。这就是为什么我还用它在向上的运动设置从多点触控到来时,一个新的开始值的单点触摸动作。

The onTouchDown function of the camera just sets the first value of the touch move. That's why I also use it in the up movement to set a new begin value for the single touch movement when coming from a multitouch.

我希望有人知道我的问题是,可以帮我。

I hope someone knows what my problem is and can help me further.

推荐答案

在这里开始,如果你还没有看过它了,它会在一些事情处理的多点触控Android中:http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html

Start here if you haven't read it already, it goes over a number of things dealing with multitouch in Android: http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html

你的贴code有几件事情:

A few things about your posted code:

您永远不会看到 ACTION_DOWN 与2 ACTION_DOWN 指针数只发送了第一台指针式该下降。所有的手指后的第一个触摸屏幕将发送 ACTION_POINTER_DOWN 。 请不要认为只有达到2手指触摸,可以很容易地多。 这是一个更容易与屏蔽操作(使用 MotionEvent#getActionMasked()),而不是code为每个单独的指针指数工作。 在年底,指数只是无所谓拉数据从一个 MotionEvent 的。如果你正在跟踪指针的移动随着时间的推移,使用指针ID。 指针ID都只是数字。不要假设,以什么样的价值观,他们会不会有其他比他们将是整数0以上。 You'll never see ACTION_DOWN with a pointer count of 2. ACTION_DOWN is only sent for the first pointer that goes down. All fingers that touch the screen after the first will send ACTION_POINTER_DOWN. Don't assume only up to 2 finger touch, there can easily be more. It's a lot easier to work with the masked action (use MotionEvent#getActionMasked()) than to code for each pointer index individually. In the end, indices only matter for pulling data out of a MotionEvent. If you're tracking pointer movement over time, use the pointer ID. Pointer IDs are just numbers. Don't make assumptions as to what values they will have other than that they will be integers from 0 and up.