How to implement detection of a View being dragged over another view?

回眸只為那壹抹淺笑 提交于 2019-12-23 03:32:11

问题


I want to have a draggable view, while being able to constantly monitor distance to the "target" view and to detect when draggable view is dragged over target view. My idea was to get Rect's of both views and to use intersect() to check if they are touching. However my implementation doesnt work smoothly - sometimes it detects overlap in wrong location or doesnt detect the end of overlapping. Could you tell me what I'm doing wrong or maybe lead to correct way of doing it? (I cant use startDrag(), because it doesnt allow to monitor events while dragging).

Code in the onCreate:

//find UI
myTextView = (TextView) findViewById(R.id.textView);
myImageView = (ImageView) findViewById(R.id.imageView);

//assign touch listener
myTextView.setOnTouchListener(new MyTouchListener());

Code for MyTouchListener:

class MyTouchListener implements View.OnTouchListener {

        //last coordinates
        float lastX;
        float lastY;

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {

            switch (motionEvent.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    view.bringToFront();
                    lastX = motionEvent.getRawX();
                    lastY = motionEvent.getRawY();
                }

                case MotionEvent.ACTION_MOVE: {

                    //motion difference
                    float deltaX = motionEvent.getRawX() - lastX;
                    float deltaY = motionEvent.getRawY() - lastY;

                    //set new last coordinates
                    lastX = motionEvent.getRawX();
                    lastY = motionEvent.getRawY();

                    //animate view to new location
                    view.animate().translationXBy(deltaX).translationYBy(deltaY).setDuration(0).start();

                    //get Rects
                    Rect textViewRect = new Rect((int)lastX+myTextView.getLeft(), (int)lastY+myTextView.getTop(), (int)lastX+myTextView.getRight(), (int)lastY+myTextView.getBottom());
                    Rect imageViewRect = new Rect(myImageView.getLeft(), myImageView.getTop(), myImageView.getRight(), myImageView.getBottom());


                    if (imageViewRect.intersect(textViewRect)) {
                        myImageView.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_dialog_info));
                    } else {
                        myImageView.setImageDrawable(getResources().getDrawable(android.R.drawable.ic_delete));
                    }
                }
            }

            return true;
        }
    }

回答1:


The animated translation is unnecessary since the touch events should move the TextView smoothly about. Just assign new X,Y coordinates to the TextView, like this:

view.setX(view.getX() + deltaX);
view.setY(view.getY() + deltaY);


来源:https://stackoverflow.com/questions/39187745/how-to-implement-detection-of-a-view-being-dragged-over-another-view

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!