MotionEvent.getPointerCount()始终为1

14

我正在尝试在我的应用程序中实现多点触控,但却得到了意外的结果。我从未收到超过一个指针的数据。 我的手机肯定支持多点触控,因为我可以在浏览器中使用捏缩手势并使用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个回答

31
问题在于我在 onTouch 中返回了false,因此没有生成新的触摸事件。

3
谢谢,谢谢,谢谢!但愿文档中有相关解释。 - user1676075

2
在我的情况下,我意识到getPointerCount()没有改变,因为它被调用在MotionEvent.ACTION_DOWN动作中,这意味着它总是一个。解决方法是将其移动,如果需要在移动时指针计数,则将其移动到MotionEvent.ACTION_MOVE中,或者将其移动到onTouch实现之外,如果指针计数仅在触摸时需要。希望这能帮助那些可能不理解此问题的人,就像我一样。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接