onTouch问题 - x<你不反应

时间:2011-08-23 20:08:59

标签: java android ontouchevent

当我使用onTouch方法时,我使用methon event.getY()为变量y2赋予手指精度。但是当我使用if语句时,有一个问题:

    public boolean onTouch(View v, MotionEvent event){
         if(y2 <= knifeY){
           //y2 = where the finger is, knifeY = where on the height a knife will be on.             
           knifeDown = false; 
           // The knife stops
           point--; 
           // score - 1;
           knife = BitmapFactory.decodeResource(getResources(),R.drawable.bloddyknife);
           // change picture.
           return true;
         }
    } 

但问题是,当你把手指放在屏幕上时,你必须移动手指,如果你按住手指,刀就会滑过。 :(

有人帮忙吗?

1 个答案:

答案 0 :(得分:1)

您是否指定了int y2=event.getY()?因为我没有在任何地方看到它,所以必须在onTouch()

内完成

我不确定这是否是好方法

public boolean onTouch(View v, MotionEvent event)
{
    while(event.getAction()!=MotionEvent.ACTION_CANCEL) // or here measure the pressure applied on the screen surface
    { 
       if(event.getY() <= knifeY)
       {
        knifeDOwn=false; // keep the knife down 
        point--; 
        knife = BitmapFactory.decodeResource(getResources(),R.drawable.bloddyknife); // 
         //maybe here you might stop the pointers collecting with using event.setAction(MotionEvent.ACTION_CANCEL) (or ACTION_UP) to exit the while loop, i dont know your idea for it, at this point i'm just guessing, but you still need to change the action of the event at anypoint at this code to exit the while loop.
       }
  }
  return true;
}

我建议您使用GestureListener并使用onFling()来实现此目的。

在此处阅读有关MotionEvent类常量的更多信息: http://developer.android.com/reference/android/view/MotionEvent.html#ACTION_DOWN