0

i'm building chathead with closeArea.. CloseArea is bottom of screen, where when you move chathead so chathead disappear. Like facebook messanger have. I have annimation, which show and hide closeArea.

Here is code:

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); 

Problem is, that i don't know how to recognise that chathead is in closeArea. Should i use any Listener or anything else?

Lalson
  • 293
  • 1
  • 3
  • 14

1 Answers1

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;
        }
    });

This is what I did. On ACTION_UP check the distance between two views. If its less than some particular value, remove both views.

Jossy Paul
  • 1,267
  • 14
  • 26