OnTouchEvent问题Android

时间:2013-02-24 09:51:58

标签: android ontouchevent

我对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。

谢谢,

2 个答案:

答案 0 :(得分: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);
}

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

答案 1 :(得分: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;
}

希望这可以帮助任何人,如果你仍然缺少信息,那么我将很乐意帮助^ _ ^。