0

我有一個ImageViewButtonLinearLayout。我想拖動屏幕上的ImageView,但仍然保留Button。但由於某些原因,當我拖動我的ImageViewButton一起拖動它。圖像拖動導致按鈕拖動

有人可以幫忙嗎?

代碼:

activity_main.xml中

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:gravity="center" 
    android:weightSum=10 
    android:id="@+id/main"> 

     <ImageView 
      android:id="@+id/image" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/my_pic" 
      android:layout_weight="2"/> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_weight="2.5"/> 

     <ImageView 
      android:id="@+id/click" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:src="@drawable/btn_click" 
      android:layout_weight="2"/> 

    </LinearLayout> 

MainActivity.java

public class MainActivity extends ActionBarActivity implements View.OnTouchListener 
{ 
    ImageView img; 
    private ViewGroup mRrootLayout; 
    private int _xDelta; 
    private int _yDelta; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mRrootLayout = (ViewGroup) findViewById(R.id.main); 
     img = (ImageView) mRrootLayout.findViewById(R.id.image); 
     LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(300, 300); 

     img.setLayoutParams(layoutParams); 
     img.setOnTouchListener(this); 
    } 

編輯

public boolean onTouch(View view, MotionEvent event) { 
     switch (view.getId()) { 
      case R.id.logo: 
       final int X = (int) event.getRawX(); 
       final int Y = (int) event.getRawY(); 
       switch (event.getAction() & MotionEvent.ACTION_MASK) { 
        case MotionEvent.ACTION_DOWN: 
         LinearLayout.LayoutParams lParams = (LinearLayout.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: 
         LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view 
           .getLayoutParams(); 
         layoutParams.leftMargin = X - _xDelta; 
         layoutParams.topMargin = Y - _yDelta; 
         layoutParams.rightMargin = -250; 
         layoutParams.bottomMargin = -250; 
         view.setLayoutParams(layoutParams); 
         break; 
       } 
       mRrootLayout.invalidate(); 
       break; 

      case R.id.fade: 
       return false; 
     } 

     return true; 
    } 
+0

哈是的。我改變了這一點。但是,你有沒有解決辦法? – user2456977 2015-02-11 13:38:30

回答

0

在上觸摸你應該檢查看看哪個視圖被觸摸,所以你只知道移動該視圖。例如:

public boolean onTouch(View view, MotionEvent event) { 
    switch (view.getId()) { 
     case R.id.image: 

      //Your code goes here 

      break; 
    } 

    return true; 
} 

這樣,只有您引用的視圖纔會移動。

+0

感謝您的建議。我編輯了上面的onTouch方法,但它給了我同樣的錯誤。我猜這個問題是ACTION_MOVE。也許我每次移動圖像時都會重畫整個屏幕,以便它也移動按鈕。你能告訴我如何改變它,所以只有圖像移動,而不是按鈕?我一直在堅持這一段時間... – user2456977 2015-02-11 16:10:39