【问题标题】:OnTouchEvent Issue AndroidOnTouchEvent 问题 Android
【发布时间】:2013-02-24 09:51:57
【问题描述】:

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

谢谢,

【问题讨论】:

  • 我想你不明白什么是触摸事件。这不是点击屏幕。如果您移动任何手指或改变压力onTouchEvent 将被调用。
  • onTouch 方法被设计为在屏幕上有动作时调用,因此无法避免多次调用。为什么你在这里不满意?
  • @PlumillonForge :我不满意,因为如果我可以得到指针计数 4 或更多,我想在里面执行我的东西。否则会调用多次。
  • @abc667 :我知道兄弟。这是我的问题,如果我想触摸并且只想检测 5 个手指,我该如何避免多次打电话!!

标签: android touch-event


【解决方案1】:

我相信每次您从屏幕上移开一根手指时都会调用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);
}

编辑:再次修复它!现在它真的有效了。

【讨论】:

  • 我用过!!但是它无法获得指针计数!如果我也只触摸一根手指就可以了!!
  • 正如你所说,我在这里更新了我的代码!!它不工作!看上面的代码,让我帮你!
  • 你将不得不做更多的事情,然后只是替换那一行代码兄弟! ACTION_UP 在最后一根手指离开屏幕时发送,此时您需要知道在您触摸屏幕并松开所有手指期间手指的最高数量。如果是 5,运行你的代码。
  • ACTION_POINTER_DOWN 适用于在 FIRST 之后进入屏幕的额外手指。所以我猜应该是 4。
  • 我已经放了 4,3,2... 但它不满足那个条件!!
【解决方案2】:

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

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

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

【讨论】:

    猜你喜欢
    • 2011-06-15
    • 1970-01-01
    • 2011-06-19
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多