2017-02-25 61 views
0

我有兩個ImageButton s,我想爲它們設置相同的OnTouchListener,但問題是 - 當我觸摸一個Image時,另一個相鄰的Image也在移動。 我也覺得,當我觸摸圖片2時,它正在移出屏幕並隱形。我希望它保持在屏幕上,並希望Touch事件應該爲他們單獨工作。On Touch Listener無法正常工作

這是我到現在爲止所做的。

//this is the snippet from main activity where the onTouch is implemented and layout is created  

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE); 
    imgMaster.setLayoutParams(params); 
    params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); 
    params.addRule(RelativeLayout.RIGHT_OF, imgMaster.getId()); 
    imgMaster.setOnTouchListener(new ChoiceTouchListener()); 
    imgMood.setOnTouchListener(new ChoiceTouchListener()); 
    imgMood.setLayoutParams(params); 
    imgMaster.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "Clicked Master", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
    imgMood.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast.makeText(getApplicationContext(), "Clicked Mood", Toast.LENGTH_SHORT).show(); 
     } 
    }); 
} 

private final class ChoiceTouchListener implements OnTouchListener { 
    public boolean onTouch(View view, MotionEvent event) { 
     //ImageButton views = (ImageButton) view; 
     final int X = (int) event.getRawX(); 
     final int Y = (int) event.getRawY(); 
     switch (view.getId()) { 
      case R.id.master: 
       switch (event.getAction() & MotionEvent.ACTION_MASK) { 
        case MotionEvent.ACTION_DOWN: 
         RelativeLayout.LayoutParams lparams = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
         _xDelta = X - lparams.leftMargin; 
         _yDelta = Y - lparams.topMargin; 
         break; 
        case MotionEvent.ACTION_UP: 
         break; 
        case MotionEvent.ACTION_POINTER_DOWN: 
         break; 
        case MotionEvent.ACTION_POINTER_UP: 
         break; 
        case MotionEvent.ACTION_MOVE: 
         RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
         layoutParams.leftMargin = X - _xDelta; 
         layoutParams.topMargin = Y - _yDelta; 
         layoutParams.rightMargin = -250; 
         layoutParams.bottomMargin = -250; 
         view.setLayoutParams(layoutParams); 
         break; 
       } 
      case R.id.mood: 
       switch (event.getAction() & MotionEvent.ACTION_MASK) { 
        case MotionEvent.ACTION_DOWN: 
         RelativeLayout.LayoutParams lparams = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
         _xDelta = X - lparams.leftMargin; 
         _yDelta = Y - lparams.topMargin; 
         break; 
        case MotionEvent.ACTION_UP: 
         break; 
        case MotionEvent.ACTION_POINTER_DOWN: 
         break; 
        case MotionEvent.ACTION_POINTER_UP: 
         break; 
        case MotionEvent.ACTION_MOVE: 
         RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams(); 
         layoutParams.leftMargin = X - _xDelta; 
         layoutParams.topMargin = Y - _yDelta; 
         layoutParams.rightMargin = -250; 
         layoutParams.bottomMargin = -250; 
         view.setLayoutParams(layoutParams); 
         break; 
       } 
       break; 
     } 


     return false; 

    } 

} 

//這是我提前佈局

<RelativeLayout 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:id="@+id/view_root" 
android:background="@drawable/transparent_background" 
xmlns:android="http://schemas.android.com/apk/res/android"> 
<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/mood" 
    android:src="@drawable/mood" 

    android:background="@null"/> 

<ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/master" 
    android:src="@drawable/master" 
    android:background="@null" /></RelativeLayout> 

感謝。

+0

這些圖像是否重疊在一起?我問這是因爲你使用了相對佈局,並沒有提到每個圖像相對於彼此的相對位置 – Dibzmania

+0

@Dibzmania但添加規則已經定義在代碼中的位置 –

+0

@Dibzmania可能是它的重疊...但如果我設置類似於在佈局android:layout_alignParentBottom =「true」 android:layout_alignParentRight =「true」 android:layout_alignParentEnd =「true」圖像它沒有做任何改變.... –

回答

0

正如你所要求的代碼,這裏是,但我仍然不知道你想從你的對象做什麼實際的運動。

我提供的代碼可讓您在屏幕上隨意拖動兩個對象,而無需移動其他對象。

希望這是你想要的(可能是至少近)

注:請刪除所有RelativeLayout.addRule()等,只需設置onTouch監聽到你的ImageButtons,下面是Ontouch

的定義
private final class ChoiceTouchListener implements View.OnTouchListener { 
     public boolean onTouch(View view, MotionEvent event) { 
      final int X = (int) event.getRawX(); 
      final int Y = (int) event.getRawY(); 

      switch (view.getId()) { 
       case R.id.master: 
        switch (event.getAction() & MotionEvent.ACTION_MASK) { 
         case MotionEvent.ACTION_DOWN: 
          break; 
         case MotionEvent.ACTION_UP: 
          break; 
         case MotionEvent.ACTION_POINTER_DOWN: 
          break; 
         case MotionEvent.ACTION_POINTER_UP: 
          break; 
         case MotionEvent.ACTION_MOVE: 
          view.setX(X-view.getWidth()/2); 
          view.setY(Y-view.getHeight()); 
          break; 
        } 
       case R.id.mood: 
        switch (event.getAction() & MotionEvent.ACTION_MASK) { 
         case MotionEvent.ACTION_DOWN: 
          break; 
         case MotionEvent.ACTION_UP: 
          break; 
         case MotionEvent.ACTION_POINTER_DOWN: 
          break; 
         case MotionEvent.ACTION_POINTER_UP: 
          break; 
         case MotionEvent.ACTION_MOVE: 
          view.setX(X-view.getWidth()/2); 
          view.setY(Y-view.getHeight()); 
          break; 
        } 
        break; 
      } 
       return false; 

     } 

這應該是在XML佈局

<RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:id="@+id/view_root" 
    android:background="@drawable/transparent_background" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/master" 
     android:src="@drawable/master" 
     android:background="@null" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentStart="true" /> 

    <ImageButton 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/mood" 
     android:src="@drawable/mood" 
     android:background="@null" 
     android:layout_alignParentTop="true" 
     android:layout_toRightOf="@+id/master" 
     android:layout_toEndOf="@+id/master" /> 
</RelativeLayout> 
+0

謝謝工作完美 –

+0

如果你覺得我真的付出了一些努力,你也可以贊同我的回答,謝謝:) – eRaisedToX