3

我对 OnTouchEvent 感到沮丧。我只想检测 5 个手指。我怎样才能做到这一点?问题还在于它多次调用。这是我的代码:

@Override
public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

    int pointerCount = event.getPointerCount();

    System.out.println("My pointer....." + pointerCount);

    final int action = event.getAction();

       if(action == MotionEvent.ACTION_UP) {

           if(pointerCount >= 4){


           Log.d("MyActivity", "in onTouchEvent!");
          Toast.makeText(MyclassActivity.this, "Finger !!"+pointerCount,Toast.LENGTH_SHORT).show();

             Intent z = new Intent(MyclassActivity.this,
                        DashboardActivity.class);
                startActivity(z);
            finish();
           }      

    }

        return super.onTouchEvent(event);
    }

我对这件事不满意。让我在您的帮助下准确计算 5 个手指,并避免多次调用 onTouchevent。

谢谢,

4

2 回答 2

2

我相信MotionEvent.ACTION_POINTER_UP每次您从屏幕上移开一根手指时都会被调用。因此,如果您用 5 根手指触摸屏幕,它会变成 1 次以上。尝试MotionEvent.ACTION_UP在您的实现中使用。数数所有手指 -> 检查何时MotionEvent.ACTION_UP调用 -> 仅当最高手指数为 5 时才编写代码。

int maxPointercount; 
int previousPointercount; 

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

     int currentpointerCount = event.getPointerCount();

     Log.d("1", "My pointer = " + currentpointerCount); //what does it say here?

     final int action = event.getAction();
          switch (action & MotionEvent.ACTION_MASK) {
               case MotionEvent.ACTION_POINTER_DOWN:          
                 if(maxPointercount <= previousPointercount){
                 maxPointercount = currentpointerCount;
                }
                previousPointercount = currentpointerCount;
          }  

    if(action == MotionEvent.ACTION_UP) {
       Log.d("3", maxPointercount + " = maxPointercount");
       if(maxPointercount == 5){ //or whatever amount of fingers, try it out. 

          //your code that will run 1 time

       }
          maxPointercount = 0;
          previousPointercount = 0;      

     }
     return super.onTouchEvent(event);
}

编辑:再次修复它!现在它确实有效。

于 2013-02-24T10:12:35.370 回答
1

我很难知道它是如何工作的,所以这是我想出的基本内容

public boolean onTouchEvent(MotionEvent event){
    int eventaction = event.getAction();
    String str= "";
    //touch Events, i came up with the mask 5 by trial, hope it works for all devices
    //eventaction == 0 match the first touch event ever
    if( ( eventaction & 5 ) == 5  || eventaction == 0 ){
        str= "Touch Event";
    }
    //Release Event, i came up with the mask 6 by trial, hope it works for all devices 
    //eventaction == 1 match the last release event ever, this makes it hard to know wich finger was removed 
    if( ( eventaction & 6 ) == 6  || eventaction == 1 ){
        str= "Release Event:";
    }
    if( eventaction == 2 ){
        str= "Move Event:";
        return true;//it will make a mess in the logcat, if u want remove this line
    }
    str += " With Number Of fingers " + event.getPointerCount() ;
    str += ", the finger triggered the event is : finger ";
    //some stupid thing i have done, but it works 
    //these numbers was made based on the binary mask that i was able to figure out
    //but it still has an issue with the last finger removed as its eventaction  is always 0, but this can be pragmatically known by monitoring each finger touch and release 
    switch ( eventaction ){
    case 0:
    case 5:
    case 6:
        str += "1";
        break;
    case 261:
    case 262:
        str += "2";
        break;
    case 517:
    case 518:
        str += "3";
        break;
    case 773:
    case 774:
        str += "4";
        break;
    case 1029:
    case 1030:
        str += "5";
        break;
    case 1285:
    case 1286:
        str += "6";
        break;
    case 1541:
    case 1542:
        str += "7";
        break;
    case 1797:
    case 1798:
        str += "8";
        break;
    case 2053:
    case 2054:
        str += "9";
        break;
    case 2309:
    case 2310:
        str += "10";
        break;
    }
    Log.d("Test", str );
    return true;
}

希望这对任何人都有帮助,如果您仍然缺少信息,那么我很乐意提供帮助^_^。

于 2013-03-11T17:09:57.667 回答