如何检测ImageView何时与另一个ImageView发生冲突? [英] how to detect when a ImageView is in collision with another ImageView?
本文介绍了如何检测ImageView何时与另一个ImageView发生冲突?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在屏幕上开发了一些带有一些图像视图的测试游戏。
用手指,我正在移动另一个图像视图。
我想检测手指移动的图像视图何时触及另一个图像视图。
¿这是实现它的最好方法吗?我在google上找不到关于它的信息...
谢谢
解决方案
我会回答一般性问题 - 找出两个视图重叠的时间:
private boolean viewsOverlap(View v1,View v2){
int [] v1_coords = new int [2];
v1.getLocationOnScreen(v1_coords);
int v1_w = v1.getWidth();
int v1_h = v1.getHeight();
Rect v1_rect = new Rect(v1_coords [0],v1_coords [1],v1_coords [0] + v1_w,v1_coords [1] + v1_h);
int [] v2_coords = new int [2];
v2.getLocationOnScreen(v1_coords);
int v2_w = v2.getWidth();
int v2_h = v2.getHeight();
Rect v2_rect = new Rect(v2_coords [0],v2_coords [1],v2_coords [0] + v2_w,v2_coords [1] + v2_h);
返回v1_rect.intersect(v2_rect)|| v1_rect.contains(v2_rect)|| v2_rect.contains(v1_rect);
}
<这些天我的三角学有点摇晃,所以我不确定这部分:
OVERLAP == v1_rect.intersect (v2_rect)|| v1_rect.contains(v2_rect)|| v2_rect.contains(v1_rect);
仔细检查我。祝你好运。
Im developing a test game with some imageviews on the screen.
with the finger, i am moving another imageview.
I want to detect when the imageview moved by the finger has touched another imageview.
¿Wich is the best way to achieve it? I can't find info about it on google...
Thanks
解决方案
I'll answer the general question - find out when two views overlap:
private boolean viewsOverlap(View v1, View v2) {
int[] v1_coords = new int[2];
v1.getLocationOnScreen(v1_coords);
int v1_w = v1.getWidth();
int v1_h = v1.getHeight();
Rect v1_rect = new Rect(v1_coords[0], v1_coords[1], v1_coords[0] + v1_w, v1_coords[1] + v1_h);
int[] v2_coords = new int[2];
v2.getLocationOnScreen(v1_coords);
int v2_w = v2.getWidth();
int v2_h = v2.getHeight();
Rect v2_rect = new Rect(v2_coords[0], v2_coords[1], v2_coords[0] + v2_w, v2_coords[1] + v2_h);
return v1_rect.intersect(v2_rect) || v1_rect.contains(v2_rect) || v2_rect.contains(v1_rect);
}
My trigonometry is a little shaky these days so I'm not sure about this part:
OVERLAP == v1_rect.intersect(v2_rect) || v1_rect.contains(v2_rect) || v2_rect.contains(v1_rect);
Double check me. Good luck.
这篇关于如何检测ImageView何时与另一个ImageView发生冲突?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文