android 进入菜单按钮时如何关闭聊天头?

How to close chat head when it come in menu button in android?

我正在使用聊天头并成功实现了所有功能,但当它接近菜单按钮或位于菜单按钮上时我无法将其关闭。

编辑源代码:

   public class CustomService extends Service {

    private static WindowManager windowManager;
    private static ImageView chatHead,closeView;
    private static Context context;

    @Override
    public IBinder onBind(Intent intent) {
        // Not used
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {

        windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);

        chatHead = new ImageView(this);
        chatHead.setImageResource(R.drawable.ic_launcher);

        final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        params.gravity = Gravity.CENTER | Gravity.CENTER;
        params.x = 0;
        params.y = 100;

        closeView = new ImageView(this);
        closeView.setImageResource(R.drawable.blacklist);
        closeView.setVisibility(View.INVISIBLE);
        final WindowManager.LayoutParams params1 = new WindowManager.LayoutParams(
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.WRAP_CONTENT,
                WindowManager.LayoutParams.TYPE_PHONE,
                WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                PixelFormat.TRANSLUCENT);

        // params.gravity = Gravity.TOP | Gravity.LEFT;
        params1.gravity = Gravity.CENTER | Gravity.BOTTOM;
        params1.x = 0;
        params1.y = 100;
        windowManager.addView(closeView, params1);

        windowManager.addView(chatHead, params);
        chatHead.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_UP:
                    Rect rect1 = new Rect(v.getLeft(), v.getTop(),
                            v.getRight(), v.getBottom());
                    v.getHitRect(rect1);

                    Rect rect2 = new Rect(closeView.getLeft(), closeView.getTop(),
                            closeView.getRight(), closeView.getBottom());
                    closeView.getHitRect(rect2);

                    if (Rect.intersects(rect1, rect2)) {

              Toast.makeText(getApplicationContext(), "overlap", Toast.LENGTH_LONG).show();

                    } else {

                          Toast.makeText(getApplicationContext(), " not overlap", Toast.LENGTH_LONG).show();

                        // Here you can reset that view to it's initial position

                    }
                     return false;
                case MotionEvent.ACTION_MOVE:
                closeView.setVisibility(View.VISIBLE);
                    params.x = initialX
                            + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY
                            + (int) (event.getRawY() - initialTouchY);

                    windowManager.updateViewLayout(chatHead, params);
                    return false;
                case MotionEvent.ACTION_DOWN:
                    initialX = params.x;
                    initialY = params.y;

                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();

                    return false;

                }
                return false;
            }
        });

        return super.onStartCommand(intent, flags, startId);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

当我的聊天头像接近菜单按钮或菜单按钮时如何关闭聊天头像。



如何实施。请帮我。谢谢

首先,添加一个视图并像这样设置它的重力:

 params.gravity = Gravity.BOTTOM | Gravity.CENTER;         

然后将其可见性设置为不可见后将其添加到 windowManager:

closeView.setVisibility(View.INVISIBLE);
windowManager.addView(closeView, params);

您可以在 ACTION_MOVE 中显示该视图:

closeView.setVisibility(View.VISIBLE);

之后,在 ACTION_UP 中执行此操作以检查视图是否拦截:

                case MotionEvent.ACTION_UP:

                    Rect rect1 = new Rect(v.getLeft(), v.getTop(),
                            v.getRight(), v.getBottom());
                    v.getHitRect(rect1);

                    Rect rect2 = new Rect(closeView.getLeft(), closeView.getTop(),
                            closeView.getRight(), closeView.getBottom());
                    closeView.getHitRect(rect2);

                    if (Rect.intersects(rect1, rect2)) {

                        System.out.println("overlap");
                         // Here you can remove your view. 

                    } else {

                        System.out.println("no overlap");
                        // Here you can reset that view to it's initial position

                    }

您可以使用另一种方法:

private boolean isViewOverlapping(View firstView, View secondView) {
    int[] firstPosition = new int[2];
    int[] secondPosition = new int[2];

    firstView.measure(View.MeasureSpec.EXACTLY,
            View.MeasureSpec.EXACTLY);
    firstView.getLocationOnScreen(firstPosition);
    secondView.getLocationOnScreen(secondPosition);

    int r = firstView.getMeasuredWidth() + firstPosition[1];
    int l = secondPosition[1];

    System.out.println(r + " : r + l : " + l);

    return r == l && (r != 0 && l != 0);
}

并在 ACTION_UP 中这样调用它:

         boolean show = isViewOverlapping(chatHead, closeView);
                if (show == true) {
                    System.out.println("overlap");
                } else {
                    System.out.println("not overlap");

                }