如何在拖放时查找两个ImageView之间的交集?

6

我有两个ImageView

  1. 其中一个固定在某个位置。
  2. 另一个需要应用拖放功能。

我的需求是:

将第二个ImageView拖放到另一个ImageView上,当它们相交时调用一个方法。

编辑 1:

已经实现了两个imageView之间的相交,但在拖放时未能实现。

编辑 2:

如何在拖放中实现这一点?

5个回答

10

我假设您需要在Android上使用此功能。最简单的方法是使用Rect类中的intersects方法。文档链接。

import android.graphics.Rect;

...

Rect rc1 = new Rect();
imageView1.getDrawingRect(rc1);
Rect rc2 = new Rect();
imageView2.getDrawingRect(rc2);
if (Rect.intersects(rc1, rc2)) {
  // intersection is detected
  // here is your method call
}

*编辑:添加了缺失的右括号


不获取交集 - Akarsh M
查看EDIT 1:否则部分已打印 - Akarsh M
很遗憾,您的代码无法帮助您解决问题。由于我非常有信心Rect.intersects按预期工作 - 您需要对代码进行故障排除。我建议从将myViewRect和otherViewRect1打印到输出日志中以验证矩形数据开始。 - interrupt
在上面的编辑中,两个imageView都在布局中重叠在一起,并且出现了这段代码。我有什么地方做错了吗? - Akarsh M

5

这是我可行的解决方案。

private boolean isViewOverlapping(View firstView, View secondView) {

        final int[] location = new int[2];

        firstView.getLocationInWindow(location);
        Rect rect1 = new Rect(location[0], location[1],location[0] + firstView.getWidth(), location[1] + firstView.getHeight());

        secondView.getLocationInWindow(location);
        Rect rect2 = new Rect(location[0], location[1],location[0] + secondView.getWidth(), location[1] + secondView.getHeight());

        return rect1.intersect(rect2);
//        return  (rect1.contains(rect2)) || (rect2.contains(rect1));
    }

在代码中调用上述函数:

@Override
    public boolean onDrag(View v, DragEvent event) {
        switch (event.getAction()) {
case DragEvent.ACTION_DROP:

if(isViewOverlapping(view,child))
                                {Toast.makeText(this,"Overlapping with "+child.getTag() or text ,Toast.LENGTH_SHORT).show();
                                }

希望这可以帮到你。

2
 @override 
 onWindowFocusChanged(boolean focus) 

把你的代码放在onCreate() / onStart()中是不起作用的,请将其放在那里。


拖放功能怎么样? - Akarsh M

1

你是否已经为第二个ImageView创建了拖放功能?

如果是这样,你只需要调用getLeft(),getRight()等来找到拖动后(或在处理程序每隔几个增量时)第二个视图的位置,然后这就变成了一个简单的矩形交叉问题,这里已经有答案了


请您检查一下编辑1。 - Akarsh M

0
晚了点回复,但我也遇到了这个问题,并通过以下代码进行了修复。关于操作丢弃的解决方案已在此处回答,因此我不再发布它。但是,如果您想要在拖动时检测它,则必须在DragEvent.ACTION_DRAG_LOCATION中执行该操作。
  1. 获取当前拖动事件的x和y坐标:

    int x_cord = (int) event.getX();
    int y_cord = (int) event.getY();
    
  2. 现在计算矩形的大小。对于这个(在我的情况下),我必须将视图的大小除以2,因为拖动位置位于其中心,然后计算上/左/右/下:

    int left = (int) (x_cord - halfViewSize);
    int top = (int) (y_cord - halfViewSize);
    int right = (int) (x_cord + halfVaieSize);
    int bottom = (int) (y_cord + halfViewSize);
    

我进行了值转换,因为在我的情况下halfViewSize是一个浮点数float。如果你有一个整数integer,你不需要进行转换。

  • 现在您可以使用这些值构建一个矩形:

    Rect rect = new Rect(left, top, right, bottom);
    
  • 创建一个方法来检查该矩形是否与您的子视图的矩形相交:

    private boolean collideWithOthers(Rect rect) {
    
        int count = yourParentLayout.getChildCount();
    
        boolean intersects = false;
        for (int i = 0; i < count; i++) {
    
            ImageView squareInside = (ImageView) yourParentLayout.getChildAt(i);
    
            Rect rectInside = squareInside.getCollisionRect();
            if (rectInside.intersect(rect)) {
                Log.d("TAG", "ATTENTION INTERSECT!!!");
                intersects = true;
                break;//停止循环,因为检测到了相交
    
            }
    
        }
    
        return intersects;
    }
    
  • 现在你可以在 ACTION_DRAG_LOCATION 中执行这些操作:

        int x_cord = (int) event.getX();
        int y_cord = (int) event.getY();
    
        int left = (int) (x_cord - halfViewSize);
        int top = (int) (y_cord - halfViewSize);
        int right = (int) (x_cord + halfVaieSize);
        int bottom = (int) (y_cord + halfViewSize);
    
        Rect rect = new Rect(left, top, right, bottom);
        boolean collide = collideWithOthers(rect);
    

    我希望这能满足您的需求,如果不行,或许对其他人有所帮助。


    网页内容由stack overflow 提供, 点击上面的
    可以查看英文原文,
    原文链接