ChatHead CloseArea

时间:2014-09-01 14:55:12

标签: android android-windowmanager

我正在使用closeArea构建聊天头.CloseArea是屏幕的底部,当你移动chathead时,聊天消失。像facebook Messanger一样。我有一个annimation,它显示和隐藏closeArea。

这是代码:

private ImageView chatHead;
private TextView deleteArea;
chatHead = new ImageView(this);
chatHead.setImageResource(R.drawable.ic_launcher);
closeArea = new TextView(this);
closeArea.setText("Close Area");
closeArea.setFocusable(true);
mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
mMainIconLayoutParams = 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);

            mMainIconLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;
            mMainIconLayoutParams.x = 0;
            mMainIconLayoutParams.y = 100;

            mWindowManager.addView(chatHead, mMainIconLayoutParams);

            mDeleteAreaLayoutParams = 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);
    mDeleteAreaLayoutParams.gravity = Gravity.TOP /*| Gravity.LEFT*/;
    mDeleteAreaLayoutParams.x = 0;
    mDeleteAreaLayoutParams.y = mDisplayHeight-closeArea.getHeight();

    mWindowManager.addView(closeArea, mDeleteAreaLayoutParams); 

问题是,我不知道如何识别聊天室是在closeArea。我应该使用任何听众还是其他任何东西?

1 个答案:

答案 0 :(得分:1)

chatHead.setOnTouchListener(new OnTouchListener() {

        private int initialX;
        private int initialY;
        private float initialTouchX;
        private float initialTouchY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            chattail.setVisibility(View.VISIBLE);
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                initialX = params.x;
                initialY = params.y;
                initialTouchX = event.getRawX();
                initialTouchY = event.getRawY();
                return true;
            case MotionEvent.ACTION_UP:
                chattail.setVisibility(View.INVISIBLE);
                if(((params.x-deleteArea.x)<50)&&((params.y-deleteArea.y)<50))
                {
                    if (chatHead != null)
                        windowManager.removeView(chatHead);
                    windowManager.removeView(chattail);
                }
                return true;
            case MotionEvent.ACTION_MOVE:
                params.x = initialX
                        + (int) (event.getRawX() - initialTouchX);
                params.y = initialY
                        - (int) (event.getRawY() - initialTouchY);
                windowManager.updateViewLayout(chatHead, params);
                return true;
            }
            return false;
        }
    });

这就是我所做的。在ACTION_UP上检查两个视图之间的距离。如果小于某个特定值,请删除两个视图。