如何在拖动后将布局恢复到android中的原始位置

时间:2013-05-10 11:43:28

标签: android drag-and-drop

我正在研究一个项目。在我需要拖动布局的地方,我设法移动(拖动)布局,但是当我释放触摸时它不会到达原始位置。

请有人告诉我在MotionEvent.ACTION_UP中应该怎么做才能让布局回到原来的位置。

以下是我的代码。

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

public class HomeActivity extends Activity implements OnTouchListener {

    RelativeLayout _view;
    RelativeLayout _view1;
    // TextView _view;
    ViewGroup _root;
    private int _xDelta;
    RelativeLayout.LayoutParams lParams;
    RelativeLayout.LayoutParams mainlParams;
    private int _yDelta;

    // private int X1, Y1, width;

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

        _root = (ViewGroup) findViewById(R.id.root);

        _view = (RelativeLayout) findViewById(R.id.relativeLayout);



        _view.setOnTouchListener(this);
        lParams = (RelativeLayout.LayoutParams) _view.getLayoutParams();
        mainlParams = (RelativeLayout.LayoutParams) _view.getLayoutParams();
        // _root.addView(_view1);

    }

    public boolean onTouch(View view, MotionEvent event) {
        int X = (int) event.getRawX();
        int Y = (int) event.getRawY();

        RelativeLayout.LayoutParams layoutParams;
        lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();

        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
            Log.e("", "inside ACTION_DOWN");
            lParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
            _xDelta = X - lParams.leftMargin;
            _yDelta = Y - lParams.topMargin;

            break;
        case MotionEvent.ACTION_UP:
            Log.e("", "inside ACTION_UP");
            if ((X - _xDelta) < -150) {
                _root.removeView(_view);
            } else {
                view.setLayoutParams(mainlParams);
            }

            break;
        case MotionEvent.ACTION_MOVE:
            Log.e("", "inside ACTION_MOVE");

            layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
            layoutParams.leftMargin = X - _xDelta;
            layoutParams.topMargin = Y - _yDelta;
            layoutParams.rightMargin = -250;
            layoutParams.bottomMargin = -250;

            view.setLayoutParams(layoutParams);
            break;
        }
        _root.invalidate();
        return true;
    }

}

1 个答案:

答案 0 :(得分:3)

NEW EDIT

在这里,我向您展示了一个非常简单的(并且不是非常干净的代码)如何做您想做的事情。我有一个带有绿色LinearLayout的relativeLayout:

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<LinearLayout
    android:id="@+id/moving_layout"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:background="#00ff00"
    android:gravity="center"
    android:orientation="vertical" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />
</LinearLayout>

    </RelativeLayout>

然后在您的MainActivity中,将要移动的视图所在的topLayout设置为onTouchListener。在ACTION_DOWN设置originalX和originalY,在ACTION_MOVE设置moveX和moveY。当用户松开手指时,请返回原始位置:

    public class MainActivity extends Activity implements OnTouchListener {

private LinearLayout mLinearLayout;
private RelativeLayout mRelativeLayout;
private RelativeLayout.LayoutParams params;
private float originalX = 0;
private float originalY = 0;
private float moveX = 0;
private float moveY = 0;

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

    mLinearLayout = (LinearLayout) findViewById(R.id.moving_layout);
    mRelativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);
    mRelativeLayout.setOnTouchListener(this);

    int width = getMetrics(100);
    int height = getMetrics(100);

    params = new RelativeLayout.LayoutParams(width, height);

}



/**
 * converts dp to px
 * 
 * @param dp
 * @return
 */
private int getMetrics(int dp) {

    DisplayMetrics displayMetrics = this.getResources().getDisplayMetrics();
    return (int) ((dp * displayMetrics.density) + 0.5);
}

public boolean onTouch(View v, MotionEvent event) {
    // TODO Auto-generated method stub

    switch (event.getAction()) {

    case (MotionEvent.ACTION_DOWN):

        originalX = event.getX(); //set X start position
        originalY = event.getY();//set Y start position
        moveX = event.getX();//first move x
        moveY = event.getY();//first move y
        break;

    case (MotionEvent.ACTION_MOVE):

        moveX = event.getX();//set move x
        moveY = event.getY();//set move y

        //set LayoutParams to mLinearLayout
        params.leftMargin = (int) moveX;
        params.topMargin = (int) moveY;
        mLinearLayout.setLayoutParams(params);

        break;

    case (MotionEvent.ACTION_UP):

        //set mLinearLayout back to original position
        params.leftMargin = (int) originalX;
        params.topMargin = (int) originalY;
        mLinearLayout.setLayoutParams(params);
        break;

    }
    return true;
}

    }

不要怀疑getMetrics()方法,这是因为将相同的宽度和高度设置为与xml中定义的mLinearLayout相同。在xml中,您将此值设置为dp,在LayoutParams中,它是px。但这与您的问题无关。 我在这里做的是,在ACTION_DOWN设置原始X和Y并在ACTION_UP返回到此处。因此,每次用户再次按下时,这些值将“更新”,如果手指释放,布局将返回到ACTION_DOWN的最后几个点。这应该让你知道如何处理你的问题。