2015-10-18 70 views
1

我有一個簡單的佈局變得可見一旦拖動事件開始:的Android上的dragEvent火災在屏幕的任何位置拖動時

<RelativeLayout 
    android:id="@+id/onDragMenu" 
    android:visibility="gone" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 
    <TextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="blablabla" 
     android:textAppearance="?android:attr/textAppearanceLarge" 
     android:layout_marginTop="75dp" 
     android:textAlignment="center" 
     /> 
    <ImageView 
     android:id="@+id/smsDragButton" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginLeft="25dp" 
     android:layout_marginBottom="25dp" 
     android:src="@mipmap/sms_icon" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentBottom="true" 
     android:background="@drawable/rounded_button" 
     android:padding="5dp" 
     /> 
    <ImageView 
     android:id="@+id/callDragButton" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:layout_marginRight="25dp" 
     android:layout_marginBottom="25dp" 
     android:src="@mipmap/phone_icon" 
     android:layout_alignParentRight="true" 
     android:layout_alignParentBottom="true" 
     android:background="@drawable/rounded_button" 
     android:padding="5dp" 
     /> 
</RelativeLayout> 

和一些代碼來處理時,東西拖動到圖像中的一個發生了什麼意見:

View callButton = onDragMenu.findViewById(R.id.callDragButton); 
callButton.setOnDragListener(new View.OnDragListener() { 
@Override 
public boolean onDrag(View v, DragEvent event) { 
    switch (event.getAction()){ 
     case DragEvent.ACTION_DRAG_ENTERED: 
      // blablabla 
      break; 
     case DragEvent.ACTION_DRAG_ENDED: 
      // blablabla 
      break; 
    } 
    return true; 
} 

});

但是,onDrag被第二次觸發,我用ACTION_DRAG_STARTED開始拖動(在按鈕之外),並且ACTION_DRAG_ENTERED從未被擊中。 最後,當我放開時,ACTION_DRAG_ENDED被擊中,座標爲(0,0)。

我對Android很陌生,可能缺少一些基本的東西。 請幫忙。 謝謝

回答

0

而不是使用拖動事件,使用onTouch拖動事件。

MainActivity.java

import android.app.Activity; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 

public class MainActivity extends Activity implements View.OnTouchListener { 

    private ImageView mImageView; 
    private ViewGroup mRrootLayout; 
    private int _xDelta; 
    private int _yDelta; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mRrootLayout = (ViewGroup) findViewById(R.id.root); 
     mImageView = (ImageView) mRrootLayout.findViewById(R.id.imageView); 

     RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(150, 150); 
     mImageView.setLayoutParams(layoutParams); 
     mImageView.setOnTouchListener(this); 
    } 

    public boolean onTouch(View view, MotionEvent event) { 
     final int X = (int) event.getRawX(); 
     final int Y = (int) event.getRawY(); 
     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; 
     } 
     mRrootLayout.invalidate(); 
     return true; 
    } 
} 

創建視圖佈局activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/root" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:padding="@dimen/activity_vertical_margin" > 

    <ImageView 
     android:id="@+id/imageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="124dp" 
     android:src="@drawable/ic_launcher" /> 

</RelativeLayout> 
+0

這真的是個好主意嗎?我已經看到使用onDragEvent的應用程序,並且一切正常。 – Svarog

2

設置的相對佈局知名度看不見,而不是

+0

它在佈局中消失了,但在拖動開始時顯示它。 – Svarog

+0

好吧,我的意思是將'visibility =「gone」'改爲'visibility =「invisible」',因爲這樣android會計算圖片視圖的位置。也許它有幫助。 – Afshin

相關問題