在视图上应用Fling和ACTION_MOVE

时间:2015-11-04 11:52:19

标签: android android-view

可以通过覆盖onTouch内的OnTouchListener来轻松应用动态事件。

@Override
public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE:
            paramsF.x = initialX + (int)(event.getRawX() - initialTouchX);
            paramsF.y = initialY + (int)(event.getRawY() - initialTouchY);
            wm.updateViewLayout(view, paramsF);
            break;
    }
    return false;
}

但是要应用fling,我必须实现GestureDectector

public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

如何实现fling和ACTION_MOVE

1 个答案:

答案 0 :(得分:1)

GestureDectector之后使用MotionEvent.ACTION_MOVE可以使其正常运行。

public boolean onTouch(View v, MotionEvent event) {
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            initialX = paramsF.x;
            initialY = paramsF.y;
            initialTouchX = event.getRawX();
            initialTouchY = event.getRawY();
            break;
        case MotionEvent.ACTION_UP:
            break;
        case MotionEvent.ACTION_MOVE:
            paramsF.x = initialX + (int)(event.getRawX() - initialTouchX);
            paramsF.y = initialY + (int)(event.getRawY() - initialTouchY);
            mWindowManager.updateViewLayout(v, paramsF);
            break;
    }
    return gestureDetector.onTouchEvent(event);
}