在Android中围绕RelativeLayout拖动最初居中的TextView

时间:2015-06-02 23:18:27

标签: android android-layout drag layoutparams

所以我正在搞乱与Android中的触摸事件/手势交互。我做的第一件事就是在相对布局中制作文本视图,我可以拖延。

Java代码是:

public class draggystuff extends ActionBarActivity implements View.OnTouchListener {

private TextView mTextView;
private ViewGroup mRootLayout;
private int _xDelta;
private int _yDelta;

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

    mRootLayout = (ViewGroup) findViewById(R.id.root);
    mTextView = (TextView) mRootLayout.findViewById(R.id.textView);

    RelativeLayout.LayoutParams layoutParams =  (RelativeLayout.LayoutParams) mTextView.getLayoutParams();

    mTextView.setOnTouchListener(this);
}

@Override
public boolean onTouch(View v, 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) mTextView.getLayoutParams();

            _xDelta = X - lParams.leftMargin;
            _yDelta = Y - lParams.topMargin;
            Log.d("x is", String.valueOf(_xDelta));
            Log.d("y is", String.valueOf(_yDelta));
            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) mTextView
                    .getLayoutParams();
            layoutParams.leftMargin = X - _xDelta;
            layoutParams.topMargin = Y - _yDelta;
            layoutParams.rightMargin = -250;
            layoutParams.bottomMargin = -250;
            mTextView.setLayoutParams(layoutParams);
            break;
    }
    mRootLayout.invalidate();
    return true;
}
}

这很好用。它在左上角设置textview,我可以在屏幕上拖动它。当我停止拖动时,它会保持在我离开的位置。

接下来我决定从屏幕中央的textview开始。为此,我修改了我的OnCreate方法,如下所示:

 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_draggystuff);

    mRootLayout = (ViewGroup) findViewById(R.id.root);
    mTextView = (TextView) mRootLayout.findViewById(R.id.textView);

    RelativeLayout.LayoutParams layoutParams =  (RelativeLayout.LayoutParams) mTextView.getLayoutParams();

    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
    mTextView.setOnTouchListener(this);
}

这实现了将TextView居中的目标,但现在拖动它时它不会移动。我想这可能是因为居中属性将其锁定到位。我想如果我在更改坐标之前删除了居中属性,那可能会有效。所以我在ontouch方法中添加了一行代码如下:

public boolean onTouch(View v, 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) mTextView.getLayoutParams();

            _xDelta = X - lParams.leftMargin;
            _yDelta = Y - lParams.topMargin;
            Log.d("x is", String.valueOf(_xDelta));
            Log.d("y is", String.valueOf(_yDelta));
            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) mTextView
                    .getLayoutParams();
            layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, 0);
            layoutParams.leftMargin = X - _xDelta;
            layoutParams.topMargin = Y - _yDelta;
            layoutParams.rightMargin = -250;
            layoutParams.bottomMargin = -250;
            mTextView.setLayoutParams(layoutParams);

            break;
    }
    mRootLayout.invalidate();
    return true;
}

然而,现在,文本视图从屏幕中间开始,但是一旦拖动开始,它立即移动到左上方,然后跟随我手指的相对运动!

如何将textview集中在relativelayout中,使其既不会锁定到位,也不会在拖动之前移回角落?

2 个答案:

答案 0 :(得分:0)

不要使用相对布局,而是尝试线性布局并在textview上使用setGravity(Gravity.CENTER)来查看它是否确实是导致textview忽略任何触摸事件的addrule方法。

另外,您是否尝试过使用XML应用布局参数,并查看问题是否在那里重现?

最后,textview是否会移动到向下操作事件或移动操作事件的左上角?

答案 1 :(得分:0)

所以为了解决这个问题,我改变了移动视图的方式。

我的xdelta和ydelta变量如下:

_xDelta = X - mTextView.getX();
_yDelta = Y - mTextView.getY();

然后在我的action_move事件中,我只是使用setX和setY方法,而不是设置左边距和上边距:

mTextView.setX(X-_xDelta);
mTextView.setY(Y-_yDelta);