为Android开发的多个手指输入 [英] Multiple finger input for android development

查看:236
本文介绍了为Android开发的多个手指输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

获取计算器应用程序工作,我决定尝试创建傍后。有在中心框和在两个端部的两个极板。这款手机是水平的。我有盒子反弹的墙壁和桨与我感动我的手指向下移动。我的问题是,我想让它两个玩家的,我想对游戏的多个手指输入。我想一个手指移动桨1和其他移动桨2.到目前为止,这是我的输入code

After getting the calculator application to work I decided to try to create pong. There is a box in the center and two paddles on both ends. The phone is horizontal. I have the box bouncing off the walls and the paddle moves with me moving my finger down. My problem is i want to make it two player and i want to have multiple finger input for the game. I want one finger to move paddle 1 and the other to move paddle 2. So far this is my input code

@Override
 public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        switch (action) {

        case MotionEvent.ACTION_MOVE: {
            // Find the index of the active pointer and fetch its position



           float p1y = ev.getY();

            if(ev.getX()<300)
            {
              player1y = p1y;
            }
            if(ev.getX()>300)
            {
              player2y = p1y;
            }

           //player1y = p1y;

            invalidate();
            break;
        }

        }
        return true;


    }

它驻留在我的surfaceview类。我怎样才能修改输入法或完全摆脱它,改变它实现我的目标?也对不起我的变量。 Eclipse的崩溃上了我很多,我的笔记本电脑触摸屏倾向于移动我的光标这么短的变量似乎是可行的。 P1Y是触摸的y。和player1y和player2y是PLAYER1和player2桨的y位置。

it resides in my surfaceview class. How can i modify the input method or completely get rid of it and change it to accomplish my goal? Also sorry about my variables. Eclipse crashes a lot on me and my laptops touch panel tends to move my cursor so shorter variables seemed viable. p1y is the y of the touch. and player1y and player2y is the y positions of the player1 and player2 paddle.

推荐答案

一个MotionEvent可以拥有多个指针。使用 getPointerCount()来看看有多少指针在当前的情况下触摸屏幕。有替换版本的getX 的getY 即采取指数从0 - getPointerCount() - 1

A MotionEvent can hold multiple pointers. Use getPointerCount() to see how many pointers are touching the screen in the current event. There are alternate versions of getX and getY that take an index from 0-getPointerCount() - 1.

在更复杂的应用程序,你会希望跟踪由指针ID手指,但东西这个简单,你正在使用屏幕上的分界点,你可以做这样的事情在你的 ACTION_MOVE 情况:

In a more complex app you would want to track fingers by pointer ID, but for something this simple where you are using a cutoff point on the screen you could do something like this in your ACTION_MOVE case:

int pointerCount = ev.getPointerCount();
for (int i = 0; i < pointerCount; i++) {
    float x = ev.getX(i);
    float y = ev.getY(i);

    if (x < 300) {
        player1y = y;
    } else if (x > 300) {
        player2y = y;
    }
}

该职位从Android开发者博客可能会帮助,如果你想了解更多信息的http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html

This post from the Android Developers Blog might help if you'd like more information: http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html

这篇关于为Android开发的多个手指输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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