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

画面から指を 1 本離すMotionEvent.ACTION_POINTER_UPたびに が呼び出されていると思います。したがって、5 本の指で画面に触れると、1 回以上 true になります。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 に答える