2012-07-05 68 views
3

問題查找哪一個運動事件上​​

我想找來創建,其中一個按鈕的初始按下設置的其他3個按鈕從去可見的可見性的效果結束的圖(按鈕)。在向最近可見的按鈕之一滑動並結束後,我想啓動其他活動。

我的問題是,不管方法我用得到的意見(使用矩形對象,findNearestTouchable)和位置觸摸事件(使用ACTION_UP onTouch /手勢)的位置,他們似乎從來沒有匹配,我永遠不能返回我想要的視圖;

期望的功能

,其從按鈕的按壓開始,並且在另一個按鈕結束時,返回按鈕的ID /視圖對象則結束對一個運動事件。

我main_menu.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="fill_parent" 
    android:fillViewport="true" 
    android:orientation="vertical" > 


    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="Button"   
     android:tag="1" 

     /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/button1" 
     android:layout_marginBottom="36dp" 
     android:layout_marginRight="19dp" 
     android:layout_toLeftOf="@+id/button1" 
     android:text="Button" 
     android:visibility="gone" 
     android:tag="3" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button3" 
     android:layout_alignBottom="@+id/button3" 
     android:layout_toRightOf="@+id/button3" 
     android:text="Button" 
     android:visibility="gone" 
     android:tag="2"/> 

    <Button 
     android:id="@+id/button4" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/button3" 
     android:layout_alignBottom="@+id/button3" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="20dp" 
     android:text="Button" 
     android:visibility="gone" 
     android:tag="4" /> 

</RelativeLayout> 

在活動現狀的企圖,不設置foundit(我很開放的,完全不同的實現)

public class Menu extends Activity { 
    private GestureDetector gestureDetector; 
    View.OnTouchListener gestureListener; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main_menu); 

     gestureDetector = new GestureDetector(new MyGestureDetector()); 
     gestureListener = new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 

       View findViewById2 = findViewById(R.id.button2); 
       View findViewById3 = findViewById(R.id.button3); 
       View findViewById4 = findViewById(R.id.button4); 
       findViewById2.setVisibility(View.VISIBLE); 
       findViewById3.setVisibility(View.VISIBLE); 
       findViewById4.setVisibility(View.VISIBLE); 
       return gestureDetector.onTouchEvent(event); 
      } 
     }; 
     View button = findViewById(R.id.button1); 
     button.setOnTouchListener(gestureListener); 

    } 

    class MyGestureDetector extends SimpleOnGestureListener { 
     @Override 
     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, 
       float velocityY) { 

      View foundit = null; 
      ViewGroup menu = (ViewGroup) findViewById(R.id.button1).getParent(); 
      for (int numChildren = menu.getChildCount(); numChildren >= 0; --numChildren) { 
       View child = menu.getChildAt(numChildren - 1); 
       if (child instanceof Button) { 
        Rect bounds = new Rect(); 
        child.getHitRect(bounds); 
        if (bounds.contains((int) e2.getX(), (int) e2.getY())) { 
         foundit = child; 
        } 
       } 
      } 
      if (foundit != null) { 
       Log.i("found", "FOUND IT"); 
      } 
      return false; 
     } 

     @Override 
     public boolean onDown(MotionEvent e) { 
      return true; 
     } 
    } 

} 

有沒有人有任何見解?

編輯

測試如下:

gestureListener = new View.OnTouchListener() { 
     @Override 
     public boolean onTouch(View v, MotionEvent event) { 
      View findViewById2 = findViewById(R.id.button2); 
      View findViewById3 = findViewById(R.id.button3); 
      View findViewById4 = findViewById(R.id.button4); 
      findViewById2.setVisibility(View.VISIBLE); 
      findViewById3.setVisibility(View.VISIBLE); 
      findViewById4.setVisibility(View.VISIBLE); 
      if (event.getAction() == MotionEvent.ACTION_UP) { 
       Log.i("TOUCH X IS", Float.toString(event.getRawX())); 
       Log.i("TOUCH Y IS", Float.toString(event.getRawY())); 
      } 
      for (int numChildren = ((ViewGroup) v.getParent()) 
        .getChildCount(); numChildren >= 0; --numChildren) { 
       View child = ((ViewGroup) v.getParent()) 
         .getChildAt(numChildren - 1); 
       if (child instanceof Button) { 
        Rect bounds = new Rect(); 
        child.getHitRect(bounds); 
        Log.i(child.getTag().toString() + " CENTER X IS", 
          Float.toString(bounds.exactCenterX())); 
        Log.i(child.getTag().toString() + " CENTER Y IS", 
          Float.toString(bounds.exactCenterY())); 
       } 
      } 
      return false; 
     } 
    }; 

產生以下logcat的輸出:

07-06 01:12:58.245: TOUCH X IS(20965): 394.0

07-06 01:12:58.245: TOUCH Y IS(20965): 297.0

07-06 01:12:58.245: 4 CENTER X IS(20965): 392.0

07-06 01:12:58.245: 4 CENTER Y IS(20965): 219.0

07-06 01:12:58.245: 2 CENTER X IS(20965): 240.0

07-06 01:12:58.250: 2 CENTER Y IS(20965): 219.0

07-06 01:12:58.250: 3 CENTER X IS(20965): 95.0

07-06 01:12:58.250: 3 CENTER Y IS(20965): 219.0

07-06 01:12:58.250: 1 CENTER X IS(20965): 240.0

07-06 01:12:58.250: 1 CENTER Y IS(20965): 345.0

回答

1

獲取子視圖,如果它在線性佈局或GridLayout中的可以通過下面的方法實現。

| * |如果視圖垂直放置在LinearLayout中,我已經向你展示瞭如何獲得SubView。

NamVyuVar.setOnTouchListener(new View.OnTouchListener() 
{ 
    @Override 
    public boolean onTouch(View view, MotionEvent MsnEvtPsgVar) 
    { 
     switch (MsnEvtPsgVar.getActionMasked()) 
     { 
      case MotionEvent.ACTION_DOWN: 
       Log.d("TAG", "onTouch: ACTION_DOWN"); 
       break; 

      case MotionEvent.ACTION_MOVE: 
       LinearLayout NamLloVav = (LinearLayout)view; 
       float SubVyuHytVal = NamLloVav.getChildAt(0).getHeight(); 
       float TchPntYcoVal = MsnEvtPsgVar.getY(); 
       int NamIdxVal = (int) (TchPntYcoVal/SubVyuHytVal); 

       View NamIdxVyuVav = NamLloVav.getChildAt(NamIdxVal) 

      // CodTodo With the Indexed View NamIdxVyuVav : 

       break; 

      case MotionEvent.ACTION_UP: 
       Log.d("TAG", "onTouch: ACTION_UP"); 
       break; 
     } 
     return true; 
    } 
}); 

| * |同樣如果你想要得到SubView如果視圖水平放置在LinearLayout內改變3行以下。

float SubVyuWdtVal = NamLloVav.getChildAt(0).getWidth(); 
float TchPntXcoVal = MsnEvtPsgVar.getX(); 
int NamIdxVal = (int) (TchPntXcoVal/SubVyuWdtVal); 

| * |同樣的技術也可以應用於網格佈局。

+0

感謝您回答一個非常古老的問題,從我完成任何android開發已經很長時間了,但這使我看看這是否有效。 – Dnlhoust 2017-05-21 14:46:30

0

在你的手勢監聽,檢查時的MotionEvent是MotionEvent。 ACTION_UP並使用coords event.getX()和event.getY(),然後確定這些協調中的視圖ES。

public boolean onTouch(View view, MotionEvent event) { 
    if (event == MotionEvent.ACTION_UP) { 
    int x = event.getX(); 
    int y = event.getY(); 
    //find what view is at x, y 
    } 
} 

當第一按鈕被按下(或者與的onClick或當運動事件是ACTION_DOWN)你會引發一些標誌。然後,當他們舉起手指時,上面就會被解僱。

編輯:一定要使用event.getY()而不是event.getRawY()。計算是不同的,並且按鈕的邊界以與getY()相同的方式進行計算:

+0

我已經試過了,不幸的是它具有相同的問題;事件的x和y似乎並不匹配視圖的共同點,即使ACTION_UP在它們之上被觸發。 – Dnlhoust 2012-07-05 23:56:44

+0

您是否有過一個示例,說明觸摸獲得哪些值以及視圖的實際邊界是什麼? – CSAntol 2012-07-05 23:59:44

+0

請參閱編輯:) – Dnlhoust 2012-07-06 00:20:13