如何在android中识别三指点击

发布于 2024-12-10 19:05:01 字数 78 浏览 0 评论 0原文

我想在android屏幕上检测三指点击。我最多能够检测两根手指。如何检测三根手指呢?我听说有些地方说android能够检测两根手指。是这样吗?

I want to detect three finger tap in android screen.I am able to detect up to two fingers.How to detect three fingers it?I heard some where that android is capable of detecting 2 fingers.Is it so?

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

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

发布评论

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

评论(2

握住我的手 2024-12-17 19:05:02

这段代码可以帮助你:

public boolean onTouchEvent(MotionEvent event)
{
    int action = event.getAction();
    switch(action & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_POINTER_DOWN:
            // multitouch!! - touch down
            int count = event.getPointerCount(); // Number of 'fingers' in this time
            break;
    }
}

This code will can help you:

public boolean onTouchEvent(MotionEvent event)
{
    int action = event.getAction();
    switch(action & MotionEvent.ACTION_MASK)
    {
        case MotionEvent.ACTION_POINTER_DOWN:
            // multitouch!! - touch down
            int count = event.getPointerCount(); // Number of 'fingers' in this time
            break;
    }
}
梦幻的味道 2024-12-17 19:05:02

我被要求只处理三个手指的点击。
使用上述解决方案,如果您点击 4 & 5 个手指 以及更多,您将在 onTouchEvent 内触发 3 个手指,因此我必须实现自己的解决方案,因为它很好,所以我可以分享它。

因此,我们的想法是创建一个 CountDownTimer,每当用户完成点击屏幕时就会触发一个事件。

在此示例中,我将 numberOfFingers 定义为 3,因此当且仅当三个手指在屏幕上点击时,我才会显示 Toast。

您可以更改它并获得您想要跟踪的任意数量的手指

首先,创建一个新的 java 类并将其命名为 FingerTapHandler

import android.os.CountDownTimer;

public class FingerTapHandler {

    private final int numberOfTrackingFingers;
    private int receivedNumOfFingers;
    private final OnTouchTimerListener touchTimerListener;

    public FingerTapHandler(int numberOfTrackingFingers, OnTouchTimerListener listener) {
        this.numberOfTrackingFingers = numberOfTrackingFingers;
        this.touchTimerListener = listener;
    }

    public void updateFingersCount(int fingers) {
        reset();
        this.receivedNumOfFingers = fingers;
    }

    private void reset() {
        onTouchTimer.cancel();
        onTouchTimer.start();
    }

    CountDownTimer onTouchTimer = new CountDownTimer(100, 100) {
        @Override
        public void onTick(long l) {
        }

        @Override
        public void onFinish() {
            if (receivedNumOfFingers == numberOfTrackingFingers) {
                touchTimerListener.handleOnFinish();
            }
        }
    };
}

并且我们还需要一个接口来处理点击事件:OnTouchTimerListener 现在,您可以从上面

public interface OnTouchTimerListener {
    void handleOnFinish();
}

的类创建一个新实例,并让您的 Activity 跟踪所需的点击次数。

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements OnTouchTimerListener {

    private FingerTapHandler fingerTapHandler;
    private static final int numberOfFingers = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fingerTapHandler = new FingerTapHandler(numberOfFingers, this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if ((action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN) {
            int fingers = event.getPointerCount();
            fingerTapHandler.updateFingersCount(fingers);
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void handleOnFinish() {
        Toast.makeText(this, "Tapped on screen with " + numberOfFingers + " fingers", Toast.LENGTH_SHORT).show();
    }
}

I was asked to handle only three fingers tap.
With the above solution if you tap 4 & 5 fingers and more you will get 3 fingers triggering inside onTouchEvent so I had to implement my own solution and since it is good I can share it.

so the idea is to create a CountDownTimer that will fire back an event whenever users finish tapping on the screen.

In this example, I defined numberOfFingers to 3 so I show a Toast if and only if three fingers were tapped on the screen.

You can change it and get whatever number of fingers you wish to track

First, create a new java class and name it FingerTapHandler

import android.os.CountDownTimer;

public class FingerTapHandler {

    private final int numberOfTrackingFingers;
    private int receivedNumOfFingers;
    private final OnTouchTimerListener touchTimerListener;

    public FingerTapHandler(int numberOfTrackingFingers, OnTouchTimerListener listener) {
        this.numberOfTrackingFingers = numberOfTrackingFingers;
        this.touchTimerListener = listener;
    }

    public void updateFingersCount(int fingers) {
        reset();
        this.receivedNumOfFingers = fingers;
    }

    private void reset() {
        onTouchTimer.cancel();
        onTouchTimer.start();
    }

    CountDownTimer onTouchTimer = new CountDownTimer(100, 100) {
        @Override
        public void onTick(long l) {
        }

        @Override
        public void onFinish() {
            if (receivedNumOfFingers == numberOfTrackingFingers) {
                touchTimerListener.handleOnFinish();
            }
        }
    };
}

And also we need an interface to handle the tap event: OnTouchTimerListener

public interface OnTouchTimerListener {
    void handleOnFinish();
}

Now, you can create a new instance from the above class and let your Activity tracks the wanted numbers of taps.

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements OnTouchTimerListener {

    private FingerTapHandler fingerTapHandler;
    private static final int numberOfFingers = 3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fingerTapHandler = new FingerTapHandler(numberOfFingers, this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int action = event.getAction();
        if ((action & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_POINTER_DOWN) {
            int fingers = event.getPointerCount();
            fingerTapHandler.updateFingersCount(fingers);
        }
        return super.onTouchEvent(event);
    }

    @Override
    public void handleOnFinish() {
        Toast.makeText(this, "Tapped on screen with " + numberOfFingers + " fingers", Toast.LENGTH_SHORT).show();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文