MotionEvent.getPointerCount()始终为1

时间:2012-05-07 14:51:12

标签: android multi-touch android-gesture

我在尝试在我的应用中实现多点触控时,会以某种方式获得意外结果。我永远不会获得多个指针的数据。 我的手机上的多点触控确实有效,因为我可以使用GestureDetector捏缩放浏览器并检测捏手势,但无论我用多少手指触摸屏幕,下面的示例都会打印action=0 pointers=1

我需要配置/ AndroidManifest或Activity创建

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    findViewById(R.id.ll1).setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            Log.d("TAG","onTouch action="+event.getAction()+" pointers="+event.getPointerCount());
            return false;
        }
    });
}

布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ll1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
</LinearLayout>

2 个答案:

答案 0 :(得分:23)

问题是我在false返回onTouch,因此尚未生成新的触控事件。

答案 1 :(得分:0)

就我而言,我意识到getPointerCount()并没有改变,因为它在MotionEvent.ACTION_DOWN动作中被调用,这意味着它始终是一个。

解决方案是将其移动,如果需要在移动时需要指针计数,则将其移动到MotionEvent.ACTION_MOVE上,或者在动作之外将其移动到onTouch实现,如果一次触摸需要指针计数。我希望这可以对可能不了解我情况的人有所帮助。

相关问题