Android多点触控问题

时间:2013-11-16 14:45:01

标签: java android multi-touch

所以我正在制作一个游戏,两个玩家在一个屏幕上同时玩。为简单起见,假设两个玩家在屏幕一侧有一个按钮,按钮B [0]和B [1]。我想实现多点触控,这样两个按钮都可以按下而没有反应,但只要其中一个按钮按下ACTION_DOWN,另一个按钮就会无法响应。我阅读了所有重复的问题并理解第二次触摸将被引用为ACTION_POINTER_DOWN所以我写了这段代码:

@Override
    public boolean onTouch(View buttonPressed, MotionEvent action) 
    {
        if(buttonPressed.getId()==B[0].getId())
        {
             if(action.getAction() == MotionEvent.ACTION_DOWN || action.getAction() == MotionEvent.ACTION_POINTER_DOWN)
                B[0].setImageDrawable(BPressed[0]);
             if(action.getAction() == MotionEvent.ACTION_UP || action.getAction() == MotionEvent.ACTION_POINTER_UP)
                B[0].setImageDrawable(BOk[0]);    
        }
        if(buttonPressed.getId()==B[1].getId())
        {
             if(action.getAction() == MotionEvent.ACTION_DOWN || action.getAction() == MotionEvent.ACTION_POINTER_DOWN)
                B[1].setImageDrawable(BPressed[1]);
             if(action.getAction() == MotionEvent.ACTION_UP || action.getAction() == MotionEvent.ACTION_POINTER_UP)
                B[1].setImageDrawable(BOk[1]);    
        }
        return true;
    }

所以到目前为止,我可以判断我是否将1个手指放在B [0]上并将其留在那里(没有ACTION_UP)然后将另一个放在B [1]上,听众应该触发onTouch(B [1],ACTION_POINTER_DOWN)但是我认为它不会,因为当我这样做时,B [0]具有BPressed drawable而B [1]具有BOk drawable。我哪里做错了?对不起,如果我复制了,但我读了一切,似乎无法找到一个简单的解决方案。我找到的关于这个问题的每个解决方案都比我需要的更多。

这两个按钮都与监听器连接如下:

B[0].setOnTouchListener(this);
B[1].setOnTouchListener(this);

我读过这个:

ACTION_POINTER_DOWN and ACTION_POINTER_UP are fired whenever a secondary pointer goes down or up.

但这似乎不是真的。

编辑:

新代码,结果仍然相同:

        @Override
        public boolean onTouch(View buttonPressed, MotionEvent action) 
        {
if( buttonPressed.getId()==B[1].getId() )
             Toast.makeText(context, "One", 500).show();
    int act = action.getAction() & MotionEvent.ACTION_MASK;

            if(buttonPressed.getId()==B[0].getId())
            {
                 if(act == MotionEvent.ACTION_DOWN || act == MotionEvent.ACTION_POINTER_DOWN)
                    B[0].setImageDrawable(BPressed[0]);
                 if(act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_POINTER_UP)
                    B[0].setImageDrawable(BOk[0]);    
            }
            if(buttonPressed.getId()==B[1].getId())
            {
                 if(act == MotionEvent.ACTION_DOWN || act == MotionEvent.ACTION_POINTER_DOWN)
                    B[1].setImageDrawable(BPressed[1]);
                 if(act == MotionEvent.ACTION_UP || act == MotionEvent.ACTION_POINTER_UP)
                    B[1].setImageDrawable(BOk[1]);    
            }
            return true;
        }   

此外,祝酒词:

if( buttonPressed.getId()==B[1].getId() )
         Toast.makeText(context, "One", 500).show();
如果我把手指放在B [0]上,

永远不会被看到。

屏蔽后:

有些事情发生了变化。假设我把手指放在B [0]上。在屏蔽之前,当我按下B [1]时没有任何反应。屏蔽后,如果我将手指放在B [0]上,然后我在屏幕上随意点击我的手指,它会循环B [0]。所以,如果我去:

finger1_DOWN on B[0] it changes to Pressed drawable
while(1)
{
finger2_DOWN anywhere nothing changes
finger2_UP anywhere B[0] changes to Ok drawable
}

我在OnCreate中创建了我的图像按钮:

B[0] = (ImageButton) findViewById(R.id.ImageButton05);
B[0].setOnTouchListener(this);
B[1] = (ImageButton) findViewById(R.id.ImageButton15);
B[1].setOnTouchListener(this);

我尝试了jboi的方法。我不在乎按钮是先按下还是按下按钮,所以我只是这样做了:

private class MPListener implements OnTouchListener
    {
        @Override
        public boolean onTouch(View buttonPressed, MotionEvent event)
        {
            if(event.getAction() == MotionEvent.ACTION_DOWN)
            {
                if(buttonPressed.getId()==B[0].getId())
                   B[0].setImageDrawable(BPressed[0]);

                if(buttonPressed.getId()==B[1].getId())
                  B[1].setImageDrawable(BPressed[1]);
            }
            if(event.getAction() == MotionEvent.ACTION_UP)
            {
                if(buttonPressed.getId()==B[0].getId())
                   B[0].setImageDrawable(BOk[0]);

                if(buttonPressed.getId()==B[1].getId())
                  B[1].setImageDrawable(BOk[1]);
            }
        return true;
        }
    }

我从我的活动中删除了实现onTouchListener并添加了

B[0].setOnTouchListener(new MPListener());
B[1].setOnTouchListener(new MPListener());

没有任何改变。我仍然可以单独按下两个按钮,但不能同时按下。

修改 所以我做了一个新的活动,以防其他问题出现。这是我的新活动的完整代码:

package com.example.projectname;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.ImageView;

public class About extends Activity
{

    private class MPListener implements OnTouchListener 
    {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP) 
            {
                if(v.getId()==B[0].getId())
                    B[0].setImageDrawable(Bok[0]);
                if(v.getId()==B[1].getId())
                    B[1].setImageDrawable(Bok[1]);
            }
            else if(event.getAction() == MotionEvent.ACTION_DOWN) 
            {
                if(v.getId()==B[0].getId())
                    B[0].setImageDrawable(Bpressed[0]);
                if(v.getId()==B[1].getId())
                    B[1].setImageDrawable(Bpressed[1]);
            }
            return true;
        }
    }

    ImageView[] B;
    Drawable[] Bok,Bpressed;
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        //standard oncreate stuff
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_about);

        Context context=getApplicationContext();

        B=new ImageView[2];
        Bok=new Drawable[2];
        Bpressed=new Drawable[2];

        Bok[0] = context.getResources().getDrawable(R.drawable.bblue);
        Bok[1] = context.getResources().getDrawable(R.drawable.bpurp);
        Bpressed[0] = context.getResources().getDrawable(R.drawable.bpressedblue);
        Bpressed[1] = context.getResources().getDrawable(R.drawable.bpressedpurp);

        B[0] = (ImageView) findViewById(R.id.imageView1);
        B[1] = (ImageView) findViewById(R.id.imageView2);
        B[0].setImageDrawable(Bok[0]);
        B[1].setImageDrawable(Bok[1]);

        B[0].setOnTouchListener(new MPListener());
        B[1].setOnTouchListener(new MPListener());
    }
}

一切都和往常一样。只要我按下一个按钮,另一个按钮就会没有响应。

1 个答案:

答案 0 :(得分:1)

Android可以为您排序,按什么顺序按下什么,是否仍然被触摸。只需给出两个按钮不同的侦听器。类似这样的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.d("TEST", "Begin onCreate");
    super.onCreate(savedInstanceState);
    Log.d("TEST", "End   onCreate");

    setContentView(R.layout.test);
    findViewById(R.id.upperTouchable).setOnTouchListener(new MyTouchListener());
    findViewById(R.id.lowerTouchable).setOnTouchListener(new MyTouchListener());
}

private boolean lowerIsTouched = false;
private boolean upperIsTouched = false;

private void setInfo() {
    if(lowerIsTouched && upperIsTouched)
        ((TextView) findViewById(R.id.info)).setText("both touched");
    else if(lowerIsTouched)
        ((TextView) findViewById(R.id.info)).setText("only lower is touched");
    else if(upperIsTouched)
        ((TextView) findViewById(R.id.info)).setText("only upper is touched");
    else
        ((TextView) findViewById(R.id.info)).setText("non is touched");

    ((TextView) findViewById(R.id.lowerTouchable)).setText(lowerIsTouched? "touched":"not touched");
    ((TextView) findViewById(R.id.upperTouchable)).setText(upperIsTouched? "touched":"not touched");
}

private class MyTouchListener implements OnTouchListener {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if(event.getAction() == MotionEvent.ACTION_DOWN) {
            if(v.getId() == R.id.lowerTouchable)
                lowerIsTouched = true;
            else if(v.getId() == R.id.upperTouchable)
                upperIsTouched = true;
            setInfo();
        }
        else if(event.getAction() == MotionEvent.ACTION_UP) {
            if(v.getId() == R.id.lowerTouchable)
                lowerIsTouched = false;
            else if(v.getId() == R.id.upperTouchable)
                upperIsTouched = false;
            setInfo();
        }
        return true;
    }
}

和相应的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/info"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:background="#FFFFFFFF"
android:text="Non touched"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/upperTouchable"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center"
    android:text="Not touched"
    android:background="#FFF0F0F0"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/lowerTouchable"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:gravity="center"
    android:text="Not touched"
    android:background="#FFFFFFFF"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>