如何实现将视图拖动到另一个视图上的检测? [英] How to implement detection of a View being dragged over another view?

查看:77
本文介绍了如何实现将视图拖动到另一个视图上的检测?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有一个可拖动的视图,同时能够不断监视到目标视图的距离并检测何时将可拖动的视图拖动到目标视图上。我的想法是获取两个视图的 Rect ,并使用 intersect()检查它们是否在触摸。但是,我的实现无法顺利进行-有时它会在错误的位置检测到重叠,或者没有检测到重叠结束。
您能告诉我我做错了什么,或者导致正确的做事方式吗? (我不能使用 startDrag(),因为它不允许在拖动时监视事件)。

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).

onCreate

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

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

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


推荐答案

动画翻译是不必要的,因为触摸事件可以使TextView平稳移动。只需将新的X,Y坐标分配给TextView,如下所示:

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

这篇关于如何实现将视图拖动到另一个视图上的检测?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆