如何捕获的Andr​​oid手机的手指移动的方向吗? [英] How to capture finger movement direction in android phone?

查看:169
本文介绍了如何捕获的Andr​​oid手机的手指移动的方向吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想捕捉的Andr​​oid触控手机的手指移动的方向。如果用户滑动手指在上/下/左/右方向,我想抓住这个方向发展。我如何才能找到呢?谢谢你。

I want to capture the finger movement direction on Android touch phone. If a user slides his finger in up/down/left/right direction, I want to capture this direction. How can I find this? Thanks.

推荐答案

实施的onTouchEvent(),并在用户presses下来,抬起计算dx和dy。您可以使用这些值来计算出的移动方向。

Implement onTouchEvent(), and calculate dx and dy by where the user presses down and lifts up. You can use these values to figure out the direction of the move.

float x1, x2, y1, y2, dx, dy;
String direction;
switch(event.getAction()) {
    case(MotionEvent.ACTION_DOWN):
        x1 = event.getX();
        y1 = event.getY();
        break;
    case(MotionEvent.ACTION_UP) {
        x2 = event.getX();
        y2 = event.getY();
        dx = x2-x1;
            dy = y2-y1;

            // Use dx and dy to determine the direction
        if(Math.abs(dx) > Math.abs(dy)) {
            if(dx>0) directiion = "right";
            else direction = "left";
        } else {
            if(dy>0) direction = "down";
            else direction = "up";
        }
    }
}

这篇关于如何捕获的Andr​​oid手机的手指移动的方向吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆