我有两个 ImageView
我需要的:
第二个拖到ImageView另一个ImageView,当它们相交时,我需要调用一个方法.
编辑1:
相交b/w两个imageView DONE但是在拖拽上它没有发生.
编辑2:
我怎样才能实现这个目标Drag and drop?
int*_*upt 10
I assume that you need this functionality for Android. The easiest way is to use intersects method found in Rect class. Link to documentation.
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
}
Run Code Online (Sandbox Code Playgroud)
*EDIT added missing right bracket
这是我的解决方案
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));
}
Run Code Online (Sandbox Code Playgroud)
调用上面的函数
@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();
}
Run Code Online (Sandbox Code Playgroud)
希望这可以帮助
您是否已经为第二个创建了拖放功能ImageView?
如果是这样,您需要做的就是调用getLeft()、getRight() 等来找到拖动后第二个视图的位置(或者在拖动时每隔几个增量在处理程序上),然后这就变得简单了矩形相交问题,这里已经有答案了。
| 归档时间: |
|
| 查看次数: |
4213 次 |
| 最近记录: |