简体   繁体   English

OnTouch MotionEvent操作未触发移动

[英]OnTouch MotionEvent Action Move not triggering

I'm trying to get X, Y coordinates while I'm dragging my button, but for some reason, the code bellow attached on touch listener triggers only twice instead of constant update while dragging, even with trothing I will be satisfied in order to improve performance, but as I said, onTouch method triggered just on action down, and one more time later. 我在尝试拖动按钮时试图获取X,Y坐标,但是由于某些原因,触摸侦听器上附加的以下代码仅触发两次,而不是在拖动时不断进行更新,即使使用Trothing,我也会满意,以便可以改善性能,但是正如我所说, onTouch方法仅在动作降低时触发, onTouch触发了一次。

This is my code: 这是我的代码:

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;
    }
});

And in log I have just: 在日志中,我只有:

ACTION_DOWN
WindowManager﹕ Drag already in progress
UKNOWN ACTION

You want to capture the X and Y coordinate data in your OnDragListener not your OnTouchListener . 您想在OnDragListener而不是OnTouchListener捕获X和Y坐标数据。 For the whole time that you are dragging the object around the screen, Android interprets that as one single ACTION_MOVE event, hence you only get data once from that. 在整个屏幕上拖动对象的整个过程中,Android会将其解释为单个ACTION_MOVE事件,因此您只能从中获取一次数据。 Place the following code inside your custom OnDragListener class: 将以下代码放入您的自定义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
}

Somewhere else in your custom OnDragListener class, you will have to do something with that xCoord and yCoord data. 在自定义OnDragListener类中的其他位置,您将不得不使用该xCoord和yCoord数据。 For example in my app, I have a TextView on my "debug" version of the app where I display the xCoord and yCoord data. 例如,在我的应用程序中,我的应用程序的“调试”版本上有一个TextView ,其中显示了xCoord和yCoord数据。 I execute the TextView.setText() method right after I get those two values in my if(action == DragEvent.ACTION_DRAG_LOCATION){... section. 我在if(action == DragEvent.ACTION_DRAG_LOCATION){...部分中获得了这两个值之后,立即执行TextView.setText()方法。

Hope this helps. 希望这可以帮助。

MotionEvent.getAction encodes the action type as well as additional pointer information (for multi-touch handling). MotionEvent.getAction对动作类型以及其他指针信息(用于多点触摸处理)进行编码。 You want MotionEvent.getActionMasked to get just the action part. 您希望MotionEvent.getActionMasked仅获取动作部分。

Edit: also, per discussion below there's some issue with startDragging. 编辑:此外,根据下面的讨论,startDragging存在一些问题。

Once the system has the drag shadow, it begins the drag and drop operation by sending drag events to all the View objects in your application that are currently visible. 一旦系统具有拖动阴影,它将通过将拖动事件发送到应用程序中当前可见的所有View对象来开始拖放操作。 It does this either by calling the View object's drag listener (an implementation of onDrag() or by calling the View object's onDragEvent() method. Both are passed a DragEvent object that has a getAction() value of ACTION_DRAG_STARTED 为此,它可以通过调用View对象的拖动侦听器(onDrag()的实现)或通过调用View对象的onDragEvent()方法来实现。这两个方法都传递了一个getEvent()值为ACTION_DRAG_STARTED的DragEvent对象。

finaly your code should be like this. 最后,您的代码应该是这样的。

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;
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM