OnTouch MotionEvent操作移动不触发

时间:2015-09-19 11:54:20

标签: java android android-activity touch draggable

我试图在我拖动按钮的同时获取X,Y坐标,但由于某种原因,触摸侦听器上附带的代码仅在触摸时触发两次,而不是在拖动时不断更新,即使是在完成任务时也是如此我会满意,以提高性能,但正如我所说,onTouch方法仅在操作时触发,再一次触发。

这是我的代码:

final Button dragBtn = (Button) findViewById(R.id.MyTestButton);

    dragBtn.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent me) {
        // TODO Auto-generated method stub
        ClipData data = ClipData.newPlainText("", "");
        View.DragShadowBuilder shadow = new View.DragShadowBuilder(dragBtn);
        v.startDrag(data, shadow, null, 0);
        int action = me.getAction();


        if (action==MotionEvent.ACTION_DOWN) {

            Log.i("TEST", "ACTION_DOWN");
            return true;
        }


        if (action==MotionEvent.ACTION_MOVE){
            Log.i("TEST", "ACTION_MOVE");

            float x = me.getX();
            float y = me.getY();

            //TODO: Rest of my code here.

            return true;
        }

        if (action==MotionEvent.ACTION_UP){

            Log.i("TEST", "ACTION_UP");
            return true;
        }

        Log.i("TEST", "UKNOWN ACTION");
        return true;
    }
});

在日志中我只是:

ACTION_DOWN
WindowManager﹕ Drag already in progress
UKNOWN ACTION

3 个答案:

答案 0 :(得分:2)

您希望捕获OnDragListener中的X和Y坐标数据,而不是OnTouchListener。在整个屏幕上拖动对象的过程中,Android会将其解释为单个ACTION_MOVE事件,因此您只能从中获取一次数据。将以下代码放在自定义OnDragListener类中:

@Override
public boolean onDrag(View v, DragEvent event) {
    int action = event.getAction();
    float xCoord;
    float yCoord;
    if(action == DragEvent.ACTION_DRAG_LOCATION) {
        xCoord = event.getX();
        yCoord = event.getY();
    }
    return event.getResult();       //returns true for valid drop or false for invalid drop
}

在自定义OnDragListener课程的其他位置,您必须对该xCoord和yCoord数据执行某些操作。例如,在我的应用程序中,我在我的"调试"上有一个TextView。应用程序的版本,我显示xCoord和yCoord数据。我在TextView.setText()部分中获取这两个值后立即执行if(action == DragEvent.ACTION_DRAG_LOCATION){...方法。

希望这有帮助。

答案 1 :(得分:1)

一旦系统具有拖动阴影,它就会通过将拖动事件发送到应用程序中当前可见的所有View对象来开始拖放操作。它通过调用View对象的拖动侦听器(onDrag()的实现或通过调用View对象的onDragEvent()方法来执行此操作。两者都传递一个具有getAction()值的DragEvent对象ACTION_DRAG_STARTED

答案 2 :(得分:0)

最终你的代码应该是这样的。

int basex;
int basey;
RelativeLayout.LayoutParams layoutParams;
RelativeLayout layBg;


....................

dragBtn.setOnTouchListener(new View.OnTouchListener() {

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


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

        switch (me.getAction()) {
                    case MotionEvent.ACTION_DOWN:

                        basex = ((int) (me.getRawX() - layoutParams.leftMargin));
                        basey = ((int) (me.getRawY() - layoutParams.topMargin));
                        break;
                    case MotionEvent.ACTION_MOVE:
                        int i = (int) event.getRawX();
                        int j = (int) event.getRawY();
                        layBg = ((RelativeLayout) dragBtn.getParent());
                        if ((i - basex > -(dragBtn.getWidth() * 2 / 3))
                                && (i - basex < layBg.getWidth() - dragBtn.getWidth() / 3)) {
                            layoutParams.leftMargin = (i - basex);
                        }
                        if ((j - basey > -(dragBtn.getHeight() * 2 / 3))
                                && (j - basey < layBg.getHeight() - dragBtn.getHeight() / 3)) {
                            layoutParams.topMargin = (j - basey);
                        }
                        layoutParams.rightMargin = -1000;
                        layoutParams.bottomMargin = -1000;
                        dragBtn.setLayoutParams(layoutParams);
                        break;

                    }

                    return true;
    }
});