我正在尝试创建一个类似于Facebook的信使应用程序的浮动布局,它具有删除视图,当拖动它时。它将删除聊天气泡。
但我的问题是我似乎无法检测到我的气泡布局是否在删除视图的范围内。
提前感谢您的任何建议或帮助! :d
这是我的活动:
private WindowManager mWindowManager;
private View FloatingView, DeleteView;
@Override
public void onCreate() {
super.onCreate();
mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
Display display = mWindowManager.getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
final float dpWidth = size.x;
final float dpHeight = size.y;
FloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);
DeleteView = LayoutInflater.from(this).inflate(R.layout.layout_delete, null);
WindowManager.LayoutParams paramss = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH,
PixelFormat.TRANSLUCENT);
mWindowManager.addView(DeleteView, paramss);
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_PHONE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
PixelFormat.TRANSLUCENT);
mWindowManager.addView(FloatingView, params);
FloatingView.setOnTouchListener(new View.OnTouchListener() {
private int initialX;
private int initialY;
private float initialTouchX;
private float initialTouchY;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialX = params.x;
initialY = params.y;
initialTouchX = event.getRawX();
initialTouchY = event.getRawY();
return true;
case MotionEvent.ACTION_MOVE:
DeleteView.setVisibility(View.VISIBLE);
params.x = initialX + (int) (event.getRawX() - initialTouchX);
params.y = initialY + (int) (event.getRawY() - initialTouchY);
mWindowManager.updateViewLayout(FloatingView, params);
return true;
case MotionEvent.ACTION_UP:
DeleteView.setVisibility(View.INVISIBLE);
//THIS IS WHAT I HAVE DONE SO FAR FOR THE BOUNDS DETECTION
if (event.getRawY() >= DeleteView.getHeight()) {
Toast.makeText(FloatingViewService.this, "WITHIN DELETE VIEW", Toast.LENGTH_SHORT).show();
if (FloatingView != null) mWindowManager.removeView(FloatingView);
}
return true;
}
return false;
}
});
}
答案 0 :(得分:1)
试试这个:
Rect outRect = new Rect();
int[] location = new int[2];
添加此方法
private boolean isViewInBounds(View view, int x, int y){
view.getDrawingRect(outRect);
view.getLocationOnScreen(location);
outRect.offset(location[0], location[1]);
return outRect.contains(x, y);
}
并将其添加到您的ACTION_UP
case MotionEvent.ACTION_UP:
DeleteView.setVisibility(View.INVISIBLE);
if(isViewInBounds(DeleteView, x, y)){
DeleteView.dispatchTouchEvent(event);
Toast.makeText(FloatingViewService.this, "WITHIN", Toast.LENGTH_SHORT).show();
}
return true;