android多点触控,动作指针向下不能正常工作

时间:2023-01-16 14:23:49

i have tried to detect two touches on my two imagebuttons which is single Relative layout . The problem is its showing action_down values on both buttons but no action pointer down value.

我试图在我的两个图像按钮上检测到两个触摸,即单个相对布局。问题是它在两个按钮上显示action_down值但没有动作指针向下值。

My code is as follows:

我的代码如下:

private int getIndex(MotionEvent event) {
    int idx = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >>     MotionEvent.ACTION_POINTER_INDEX_SHIFT;
    return idx;
}

@Override
public boolean onTouch(View paramView, MotionEvent paramMotionEvent) {
    // TODO Auto-generated method stub
//  Boolean state=false;
//   int pointerCount = paramMotionEvent.getPointerCount();
    boolean val = false;
     int pointerIndex = paramMotionEvent.getAction() & MotionEvent.ACTION_MASK;

     switch(pointerIndex)
     {
     case MotionEvent.ACTION_DOWN :{

            Log.i("fil", "action dowm");    
         break;
     }
     case MotionEvent.ACTION_POINTER_DOWN :{
         int id = paramMotionEvent.getPointerId(getIndex(paramMotionEvent));
            Log.d("fil", "Other point down ["+id+"]");
    Log.i("fil", "action pointer down");    
    break;
     }
     case MotionEvent.ACTION_POINTER_UP :{
         int id = paramMotionEvent.getPointerId(getIndex(paramMotionEvent));
            Log.d("fil", "Other point down ["+id+"]");
            Log.i("fil", "action pointer up");  
            break;
     }
     case MotionEvent.ACTION_UP :{

            Log.i("fil", "action up");  
         break;
     }

     }


'my xml'





<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" 
android:gravity="center">

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center" >

</RelativeLayout>

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/ic_launcher" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="100dp"
    android:background="@drawable/ic_launcher" />

1 个解决方案

#1


0  

 StringBuilder sb = new StringBuilder();
 TextView tv;
 int upPI = 0;
 int downPI = 0;
 boolean inTouch = false;
 String result = "";

@Override
public boolean onTouch(View view, MotionEvent event) {

int actionMask = event.getActionMasked();
int pointerIndex = event.getActionIndex();
int pointerCount = event.getPointerCount();

switch (actionMask) {
case MotionEvent.ACTION_DOWN:
  inTouch = true;
case MotionEvent.ACTION_POINTER_DOWN: 
  downPI = pointerIndex;
  break;

case MotionEvent.ACTION_UP: 
  inTouch = false;
  sb.setLength(0);
case MotionEvent.ACTION_POINTER_UP: 
  upPI = pointerIndex;
  break;

case MotionEvent.ACTION_MOVE:
  sb.setLength(0);

  for (int i = 0; i < 10; i++) {
    sb.append("Index = " + i);
    if (i < pointerCount) {
      sb.append(", ID = " + event.getPointerId(i));
      sb.append(", X = " + event.getX(i));
      sb.append(", Y = " + event.getY(i));
    } else {
      sb.append(", ID = ");
      sb.append(", X = ");
      sb.append(", Y = ");
    }
    sb.append("\r\n");
  }
  break;
}
result = "down: " + downPI + "\n" + "up: " + upPI + "\n";

if (inTouch) {
  result += "pointerCount = " + pointerCount + "\n" + sb.toString();
}
tv.setText(result);
return true;
}

#1


0  

 StringBuilder sb = new StringBuilder();
 TextView tv;
 int upPI = 0;
 int downPI = 0;
 boolean inTouch = false;
 String result = "";

@Override
public boolean onTouch(View view, MotionEvent event) {

int actionMask = event.getActionMasked();
int pointerIndex = event.getActionIndex();
int pointerCount = event.getPointerCount();

switch (actionMask) {
case MotionEvent.ACTION_DOWN:
  inTouch = true;
case MotionEvent.ACTION_POINTER_DOWN: 
  downPI = pointerIndex;
  break;

case MotionEvent.ACTION_UP: 
  inTouch = false;
  sb.setLength(0);
case MotionEvent.ACTION_POINTER_UP: 
  upPI = pointerIndex;
  break;

case MotionEvent.ACTION_MOVE:
  sb.setLength(0);

  for (int i = 0; i < 10; i++) {
    sb.append("Index = " + i);
    if (i < pointerCount) {
      sb.append(", ID = " + event.getPointerId(i));
      sb.append(", X = " + event.getX(i));
      sb.append(", Y = " + event.getY(i));
    } else {
      sb.append(", ID = ");
      sb.append(", X = ");
      sb.append(", Y = ");
    }
    sb.append("\r\n");
  }
  break;
}
result = "down: " + downPI + "\n" + "up: " + upPI + "\n";

if (inTouch) {
  result += "pointerCount = " + pointerCount + "\n" + sb.toString();
}
tv.setText(result);
return true;
}