p = new ImageView(this); p.setImageResource(R.drawable.k); pp = new WindowManager.LayoutParams( WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, PixelFormat.TRANSLUCENT); pp.gravity = Gravity.TOP | Gravity.LEFT; pp.x = 100; pp.y = 100; window.addView(p,pp); // ============= makes it a button for dragging and stuff // p.setOnTouchListener(new View.OnTouchListener() { private int initialX; private int initialY; private float initialTouchX; private float initialTouchY; @Override public boolean onTouch(View arg0, MotionEvent event) { if (gestureDetector.onTouchEvent(event)) { //detect tap return true; } else { // detects move and drag switch (event.getAction()) { case MotionEvent.ACTION_DOWN: initialX = pp.x; initialY = pp.y; initialTouchX = event.getRawX(); initialTouchY = event.getRawY(); return true; case MotionEvent.ACTION_UP: return true; case MotionEvent.ACTION_MOVE: pp.x = initialX + (int) (event.getRawX() - initialTouchX); pp.y = initialY + (int) (event.getRawY() - initialTouchY); windowManager.updateViewLayout(p, pp); return true; } return false; } } });

问题描述 投票:0回答:1
[与此类似,我制作了另一个imageview q。因此,我可以自由拖动这两个图像视图。

然后尝试检测这两个图像视图的冲突,我尝试了3种在stackoverflow上找到的方法。

喜欢这些:

public boolean CheckCollision(View v1,View v2) {
    Rect R1=new Rect(v1.getLeft(), v1.getTop(), v1.getRight(), v1.getBottom());
    Rect R2=new Rect(v2.getLeft(), v2.getTop(), v2.getRight(), v2.getBottom());
    return R1.intersect(R2);
}
public boolean chk(ImageView v1,ImageView v2) {
    Rect R1=new Rect();
    v1.getDrawingRect(R1);
    Rect R2=new Rect();
    v2.getDrawingRect(R2);

    return Rect.intersects(R1,R2);
}
public boolean chkm(ImageView v1,ImageView v2) {
    Rect R1=new Rect();
    v1.getHitRect(R1);
    Rect R2=new Rect();
    v2.getHitRect(R2);

    return R1.intersect(R2);
}

[无论我尝试哪种方法,在应用程序启动时,拖动过程中或拖动之后,它们总是会给我'true',而我可以正确地检测每个imageview的坐标而没有问题。

[如果有人可以帮助我纠正我可能犯的愚蠢错误,将不胜感激>。<

谢谢!

正如该主题中简要说明的那样,我正在尝试检测我在一个显示在所有内容之上的应用程序中显示的2个图像视图的冲突,这是一个浮动窗口小部件服务。我的第一个图像视图是p:p ...
android service imageview collision-detection
1个回答
0
投票
有人可以请教我吗?预先谢谢!
© www.soinside.com 2019 - 2024. All rights reserved.