2013-03-01 57 views
4

我試圖檢測兩個觸摸我的兩個imagebuttons是單一的相對佈局。 問題是它在兩個按鈕上顯示action_down值,但沒有動作指針向下值。android多點觸摸,動作指針向下不能正常工作

我的代碼如下:

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" /> 

+0

添加**返回true;賦予true值後,**你OnTouch方法.. – Numair 2013-03-01 11:51:12

+1

,問題依舊不變。 – 2013-03-01 12:18:39

+0

然後發佈你的代碼,你的onTouch方法沒有問題。 – Numair 2013-03-01 12:32:22

回答

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

謝謝你的回覆,但是這段代碼在單點觸摸和多點觸摸上都顯示了動作指針的下降值,我希望能夠幫助我在action_down和action_pointer_down上賦予不同的值。而且你的代碼也只能工作一次..它的顯示力量在第二次觸摸時就結束了 – 2013-03-01 11:10:39