在Android中,如何在使用MotionEvent时检测最大边距?

时间:2014-07-21 05:02:13

标签: android viewgroup motionevent

我使用MotionEvent在Y轴上拖动视图。但是,我不希望视图比屏幕末端更低。目前它可以超越真正令人沮丧的。

相关代码:

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();
            _yDelta = Y - lParams.bottomMargin;
            Log.i("Test1231", "ACTION_DOWN");
            break;
        case MotionEvent.ACTION_UP:
            Log.i("Test1231", "ACTION_UP");
            break;
        case MotionEvent.ACTION_POINTER_DOWN:
            Log.i("Test1231", "ACTION_POINTER_DOWN");
            break;
        case MotionEvent.ACTION_POINTER_UP:
            Log.i("Test1231", "ACTION_POINTER_UP");
            break;
        case MotionEvent.ACTION_MOVE:

            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) view.getLayoutParams();
            layoutParams.bottomMargin = Y - _yDelta;
            view.setLayoutParams(layoutParams);
            Log.i("Test1231", "ACTION_MOVE");
            break;
    }
    _root.invalidate();
    return true;
}

1 个答案:

答案 0 :(得分:1)

尝试“match_parent”根视图内的视图。你应该在“切换”之前做到这一点。一旦指针启动(在你的情况下为ACTION_UP)将所有内容返回到wrap_content)。