gpt4 book ai didi

java - Android 多点触控问题

转载 作者:行者123 更新时间:2023-12-01 13:50:14 26 4
gpt4 key购买 nike

所以我正在开发一款游戏,让两个玩家在一个屏幕上同时玩。为了简单起见,我们假设两个玩家在屏幕的一侧都有一个按钮,按钮 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;
}

据我所知,如果我将一根手指放在 B[0] 上并将其留在那里(无 ACTION_UP),然后将另一个手指放在 B[1] 上,监听器应该触发 onTouch(B[1],ACTION_POINTER_DOWN) 但是我认为它不会,因为当我这样做时,B[0]具有BPressed可绘制而B[1]具有BOk可绘制。我哪里做错了?抱歉,如果我复制了,但我阅读了所有内容,似乎找不到简单的解决方案。我发现的关于这个问题的每个解决方案都超出了我的需要。

两个按钮都与监听器连接,如下所示:

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;
}

还有, toast :

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;
}
}

我从我的 Activity 中删除了 onTouchListener 并添加了

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

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

编辑:因此,我做了一项全新的 Activity ,以防出现其他问题。这是我的新 Activity 的完整代码:

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());
}
}

一切都和往常一样。一旦我按下一个按钮,另一个按钮就会失去 react 。

最佳答案

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>

关于java - Android 多点触控问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20019721/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com