安卓多点触控?

发布于 2024-10-12 05:13:47 字数 488 浏览 4 评论 0原文

作为一名开发人员,我倾向于先编程,然后再研究。我试图实现一个可以处理多个用户输入的屏幕。基本上映射的不仅仅是一根手指。我尝试了两件事......

我有一个实现 OnTouchListener 的 Activity 类。这里我有两个单独的子视图,它们将 Activity 添加为它们的 ontouch 监听器。然后我有一个打印行,打印出 event.getPointerCount() 并打印出每个指针的位置。

将其下载到手机并进行测试。我注意到指针永远不会超过两个。我什至注意到,如果一根手指放在一个子视图上,另一根手指放在另一个子视图上,则事件只会到达第一个子组件。接下来,我尝试在每个子组件上放置单独的侦听器,看看这是否有效,但其行为相同。

因此,在处理用户触摸屏幕的输入时,Android 似乎只支持总共两个用户输入。

这是真的吗?从测试和研究来看,情况确实如此。

有没有人遇到过这个限制。有什么巧妙的解决方法吗?

感谢您的时间和回复。

dk

As a developer, I tend to program first then research later. I was trying to implement a screen that would handle multiple user inputs. Basically map more than just one finger. I tried two things..

I had an Activity class that implemented OnTouchListener. Here I had two separate child views that added the Activity as their ontouch listeners. And then I had a print line that printed out the event.getPointerCount() and printed out the location of each of the pointers..

Downloaded it to the phone and tested it. I noticed that the pointers never grew greater than two. I even noticed if one finger was on one child and another finger on the other child view that the events only made it to the first child component. Next I tried to put separate listeners on each of the child components to see if that would work but it acted the same.

So, it seems that the android only supports a total of two user inputs when dealing with inputs from the user touching the screen.

Is this true? It seems from testing and research that this is the case.

Has anyone ran into this limitation. Any clever work arounds?

Thanks for you time and responses.

dk

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

痴骨ら 2024-10-19 05:13:47

支持的触摸点数量取决于设备。在所有当前可用的 Android 版本中,MotionEvent 都会发送到单个目标视图并描述所有当前活动触摸点的状态。目标视图由第一个 MotionEvent.ACTION_DOWN 事件发生的位置确定。

请参阅帖子 了解多点触控 Android 开发人员博客,快速概述如何在 Android 上处理多点触控事件。

The number of touch points supported is dependent on the device. In all currently available versions of Android MotionEvents are sent to a single target view and describe the state of all currently active touch points. The target view is determined by where the first MotionEvent.ACTION_DOWN event occurs.

See the post Making Sense of Multitouch on the Android Developers blog for a quick overview of working with multitouch events on Android.

我一向站在原地 2024-10-19 05:13:47

我编写了一个非常简单的应用程序来测试我的 HTC Incredible 手机上的多点触摸。我的行为与我的想法非常相似,并且与 adamp 给出的 URL 非常相似。我的结果是手机只能处理两个手指输入。我有一个盒子,每个盒子都有自己的 OnTouchListener。当我单击每个框时,我会打印每个框的文本。单击每个获取触摸事件,但用食指单击屏幕上的任意位置,我得到两个指针。来自 MotionEvent event.getPointerCount 方法。第二根手指落在哪里并不重要。可能位于接收第一个手指按下事件的原始视图之外。无论如何,如果您想自己尝试一下,这里是代码。

我有整个项目的 7z,但我认为我无法附加它。

所以,这里是主要的 Activity 和布局的 xml。

package com.multitouch;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MultiTouch extends Activity {
    protected LinearLayout sq1;
    protected LinearLayout sq2;
    protected LinearLayout sq3;
    protected LinearLayout sq4;

    protected String sqText = new String();
    protected String sq1Text = new String();
    protected String sq2Text = new String();
    protected String sq3Text = new String();

    private Handler handler = new Handler();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setViews();
        handler.postDelayed(mUpdateUITimerTask, 5000);
    }

    private final Runnable mUpdateUITimerTask = new Runnable() {
        public void run() {
            // do whatever you want to change here, like:
            updateTextField();
        }
    };

    private void updateTextField() {
        TextView view1 = (TextView) this.findViewById(R.id.logView);
        TextView view2 = (TextView) this.findViewById(R.id.logView1);
        TextView view3 = (TextView) this.findViewById(R.id.logView2);
        TextView view4 = (TextView) this.findViewById(R.id.logView3);


        //view1.append(sqText + "\n");
        view1.setText(sqText);
        view2.setText(sq1Text);
        view3.setText(sq2Text);
        view4.setText(sq3Text);

        handler.post(mUpdateUITimerTask);
    }

    private void setViews() {
         sq1 = (LinearLayout) this.findViewById(R.id.square1);
         sq2 = (LinearLayout) this.findViewById(R.id.square2);
         sq3 = (LinearLayout) this.findViewById(R.id.square3);
         sq4 = (LinearLayout) this.findViewById(R.id.square4);

         sq1.setOnTouchListener(new TouchListener());
         sq2.setOnTouchListener(new TouchListener());
         sq3.setOnTouchListener(new TouchListener());
         sq4.setOnTouchListener(new TouchListener());        
    }

    private class TouchListener implements OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

             if(event.getPointerCount()>2) {
                sq1Text = "GOT THREE POINTERS!!";
                sq2Text = "GOT THREE POINTERS!!";
                sq3Text = "GOT THREE POINTERS!!";
                sqText = "GOT THREE POINTERS!!";
             }

             if(event.getAction() == MotionEvent.ACTION_DOWN){
                switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                   case R.id.square2:
                       sq1Text = "Square 2 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                   case R.id.square3:
                       sq2Text = "Square 3 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                }
             }  else if(event.getAction() == MotionEvent.ACTION_POINTER_DOWN ||
                        event.getAction() == MotionEvent.ACTION_POINTER_UP) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_POINTER_1_DOWN) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text = "Square 4 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_POINTER_2_DOWN) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_POINTER_3_DOWN) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText =  "Square 1 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text = "Square 4 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_UP) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_MOVE) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             }


            // TODO Auto-generated method stub
            return true;
        }

    }
}

布局:

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

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal" android:padding="10dip">


        <TableRow android:gravity="center_horizontal" android:padding="10dip">
            <LinearLayout android:background="#FFFF00" android:id="@+id/square1" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
            <LinearLayout android:background="#FF00FF" android:id="@+id/square2" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
        </TableRow>

        <TableRow android:gravity="center_horizontal" android:padding="10dip">
            <LinearLayout android:background="#00FFFF" android:id="@+id/square3" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
            <LinearLayout android:background="#eeeeee" android:id="@+id/square4" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
        </TableRow>     


          <TextView android:id="@+id/logView" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
          <TextView android:id="@+id/logView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
          <TextView android:id="@+id/logView2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
          <TextView android:id="@+id/logView3" android:layout_width="wrap_content" android:layout_height="wrap_content"/>



    </TableLayout>

</LinearLayout>

祝你好运,如果你尝试一下,请告诉我你是否得到了三个指针!打印报表。我想知道什么手机支持它。

谢谢,
dk

I wrote a very simple application to test multiple touch on my HTC Incredible phone. I behave pretty much like I thought and pretty much like the URL given by adamp. My results is that the phone could only handle two finger inputs. I have for box that each has its own OnTouchListener. When I click each box I print out a text for each. Clicking on each get touch events but with my index finger I clicked anywhere on the screen I got two pointers. From the MotionEvent event.getPointerCount method. Did not matter where the second finger landed. Could have been outside of the original view that received the first finger down event. Anyways here is the code if you like to try it yourself.

I have a 7z of the entire project but I do not think I can attach it.

So, here is the main activity and the xml for the layout.

package com.multitouch;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MultiTouch extends Activity {
    protected LinearLayout sq1;
    protected LinearLayout sq2;
    protected LinearLayout sq3;
    protected LinearLayout sq4;

    protected String sqText = new String();
    protected String sq1Text = new String();
    protected String sq2Text = new String();
    protected String sq3Text = new String();

    private Handler handler = new Handler();

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setViews();
        handler.postDelayed(mUpdateUITimerTask, 5000);
    }

    private final Runnable mUpdateUITimerTask = new Runnable() {
        public void run() {
            // do whatever you want to change here, like:
            updateTextField();
        }
    };

    private void updateTextField() {
        TextView view1 = (TextView) this.findViewById(R.id.logView);
        TextView view2 = (TextView) this.findViewById(R.id.logView1);
        TextView view3 = (TextView) this.findViewById(R.id.logView2);
        TextView view4 = (TextView) this.findViewById(R.id.logView3);


        //view1.append(sqText + "\n");
        view1.setText(sqText);
        view2.setText(sq1Text);
        view3.setText(sq2Text);
        view4.setText(sq3Text);

        handler.post(mUpdateUITimerTask);
    }

    private void setViews() {
         sq1 = (LinearLayout) this.findViewById(R.id.square1);
         sq2 = (LinearLayout) this.findViewById(R.id.square2);
         sq3 = (LinearLayout) this.findViewById(R.id.square3);
         sq4 = (LinearLayout) this.findViewById(R.id.square4);

         sq1.setOnTouchListener(new TouchListener());
         sq2.setOnTouchListener(new TouchListener());
         sq3.setOnTouchListener(new TouchListener());
         sq4.setOnTouchListener(new TouchListener());        
    }

    private class TouchListener implements OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

             if(event.getPointerCount()>2) {
                sq1Text = "GOT THREE POINTERS!!";
                sq2Text = "GOT THREE POINTERS!!";
                sq3Text = "GOT THREE POINTERS!!";
                sqText = "GOT THREE POINTERS!!";
             }

             if(event.getAction() == MotionEvent.ACTION_DOWN){
                switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                   case R.id.square2:
                       sq1Text = "Square 2 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                   case R.id.square3:
                       sq2Text = "Square 3 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 Pressed::Pointer count = " + String.valueOf(event.getPointerCount()); 
                   break;
                }
             }  else if(event.getAction() == MotionEvent.ACTION_POINTER_DOWN ||
                        event.getAction() == MotionEvent.ACTION_POINTER_UP) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 Pointer Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_POINTER_1_DOWN) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text = "Square 4 Pointer1Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_POINTER_2_DOWN) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 Pointer2Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_POINTER_3_DOWN) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText =  "Square 1 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text = "Square 4 Pointer3Down::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_UP) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 UP::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             } else if(event.getAction() == MotionEvent.ACTION_MOVE) {
                 switch(v.getId()) {
                   case R.id.square1:
                       sqText = "Square 1 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square2:
                       sq1Text =  "Square 2 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square3:
                       sq2Text =  "Square 3 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                   case R.id.square4:
                       sq3Text =  "Square 4 MOVE::Pointer count = " + String.valueOf(event.getPointerCount()) + "\n"; 
                   break;
                }
             }


            // TODO Auto-generated method stub
            return true;
        }

    }
}

Layout:

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

    <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"  
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:gravity="center_horizontal" android:padding="10dip">


        <TableRow android:gravity="center_horizontal" android:padding="10dip">
            <LinearLayout android:background="#FFFF00" android:id="@+id/square1" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
            <LinearLayout android:background="#FF00FF" android:id="@+id/square2" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
        </TableRow>

        <TableRow android:gravity="center_horizontal" android:padding="10dip">
            <LinearLayout android:background="#00FFFF" android:id="@+id/square3" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
            <LinearLayout android:background="#eeeeee" android:id="@+id/square4" android:padding="10px" android:layout_width="125px" android:layout_height="125px"/>
        </TableRow>     


          <TextView android:id="@+id/logView" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
          <TextView android:id="@+id/logView1" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
          <TextView android:id="@+id/logView2" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
          <TextView android:id="@+id/logView3" android:layout_width="wrap_content" android:layout_height="wrap_content"/>



    </TableLayout>

</LinearLayout>

Good luck, and if you try it let me know if you get the GOT THREE POINTERS!! print statements. I wonder what phones supports it.

thanks,
dk

半枫 2024-10-19 05:13:47

我编辑了@dakira 答案。这是我的示例代码:

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


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="Place Your Finger on Squire"
            android:textColor="#000"
            android:textSize="20sp" />

        <LinearLayout
            android:id="@+id/llStatusHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvHeader"
            android:orientation="vertical"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="8dp"
                android:weightSum="2">

                <TextView
                    android:id="@+id/tvBox1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#FFFF00"
                    android:gravity="center"
                    android:padding="4dp"
                    android:text="Finger : 0"
                    android:textColor="#000"
                    android:textSize="28sp" />

            </LinearLayout>

        </LinearLayout>


        <LinearLayout
            android:id="@+id/square1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp"
            android:layout_below="@+id/llStatusHolder"
            android:layout_weight="1"
            android:background="#FFFF00"
            android:orientation="horizontal"
            android:padding="10dp">

        </LinearLayout>

        </RelativeLayout>
    </LinearLayout>

这是 JAVA 代码:

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MultiTouchTestActivity extends Activity {
    protected LinearLayout sq1;

    TextView tvBox1;
    int counter1 = 0;

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

    private void updateTextField() {

        tvBox1.setText("Finger : " + counter1);

    }

    private void setViews() {
        tvBox1 = (TextView) findViewById(R.id.tvBox1);
        sq1 = (LinearLayout) this.findViewById(R.id.square1);

        sq1.setOnTouchListener(new TouchListener());

    }

    private class TouchListener implements OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == 1) {
                setCounterValue(1, 0);
            }else if (event.getAction() == MotionEvent.ACTION_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_DOWN ||
                    event.getAction() == MotionEvent.ACTION_POINTER_UP) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_1_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_2_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_3_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                setCounterValue(1, event.getPointerCount());
            }else if (event.getAction() == MotionEvent.ACTION_BUTTON_RELEASE) {
                setCounterValue(1, 0);
            }
            updateTextField();
            return true;
        }
    }

    void setCounterValue(int hasvalue, int value) {
        switch (hasvalue) {
            case 1:
                counter1 = value;
                break;
        }
    }
}

希望这能解决很多人的问题:)

在此处输入图像描述

I edited @dakira answer. Here is the my sample code:

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


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/tvHeader"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:padding="4dp"
            android:text="Place Your Finger on Squire"
            android:textColor="#000"
            android:textSize="20sp" />

        <LinearLayout
            android:id="@+id/llStatusHolder"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/tvHeader"
            android:orientation="vertical"
            android:weightSum="2">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal"
                android:padding="8dp"
                android:weightSum="2">

                <TextView
                    android:id="@+id/tvBox1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:background="#FFFF00"
                    android:gravity="center"
                    android:padding="4dp"
                    android:text="Finger : 0"
                    android:textColor="#000"
                    android:textSize="28sp" />

            </LinearLayout>

        </LinearLayout>


        <LinearLayout
            android:id="@+id/square1"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_margin="4dp"
            android:layout_below="@+id/llStatusHolder"
            android:layout_weight="1"
            android:background="#FFFF00"
            android:orientation="horizontal"
            android:padding="10dp">

        </LinearLayout>

        </RelativeLayout>
    </LinearLayout>

Here is JAVA code:

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MultiTouchTestActivity extends Activity {
    protected LinearLayout sq1;

    TextView tvBox1;
    int counter1 = 0;

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

    private void updateTextField() {

        tvBox1.setText("Finger : " + counter1);

    }

    private void setViews() {
        tvBox1 = (TextView) findViewById(R.id.tvBox1);
        sq1 = (LinearLayout) this.findViewById(R.id.square1);

        sq1.setOnTouchListener(new TouchListener());

    }

    private class TouchListener implements OnTouchListener {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            if (event.getAction() == 1) {
                setCounterValue(1, 0);
            }else if (event.getAction() == MotionEvent.ACTION_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_DOWN ||
                    event.getAction() == MotionEvent.ACTION_POINTER_UP) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_1_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_2_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_POINTER_3_DOWN) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                setCounterValue(1, event.getPointerCount());
            } else if (event.getAction() == MotionEvent.ACTION_MOVE) {
                setCounterValue(1, event.getPointerCount());
            }else if (event.getAction() == MotionEvent.ACTION_BUTTON_RELEASE) {
                setCounterValue(1, 0);
            }
            updateTextField();
            return true;
        }
    }

    void setCounterValue(int hasvalue, int value) {
        switch (hasvalue) {
            case 1:
                counter1 = value;
                break;
        }
    }
}

Hope this will solve many persons problem :)

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文