3

我正在做一个项目。在我需要拖动布局的地方,我设法移动(拖动)布局,但是当我释放触摸时,它并没有到达原来的位置。

请任何人告诉我应该在 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;
    }

}
4

1 回答 1

3

新编辑

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

    <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>

然后在 Your 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 中定义的那样,为 mLLinearLayout 设置了相同的宽度和高度。在 xml 中,您将此值设置为 dp,在 LayoutParams 中,它是 px。但这与您的问题无关。我在这里所做的是,将原始 X 和 Y 设置为 ACTION_DOWN,然后在 ACTION_UP 处返回。因此,每次用户再次按下时,这些值都会“更新”,如果手指松开,布局将回到 ACTION_DOWN 的最后一个点。这应该让您了解如何处理您的问题。

于 2013-05-10T11:49:41.247 回答