如何在Android中使叠加窗口视图可点击和可触摸

时间:2018-01-21 00:22:26

标签: java android

我正在尝试学习如何在Android中创建类似于Facebook聊天头信使图标的叠加窗口。

我设法创建了以下类,这使我能够创建类似于fb的聊天头的叠加窗口视图。但是,我对Clickable Listener和Touchable Listener有困难。如果我使用Clickable Listener,Touchable Listener不起作用,反之亦然。

我希望覆盖窗口视图可以点击以便在我点击它时可以恢复应用程序并可触摸以在屏幕上的任何位置移动图标。

代码:

public class FloatingViewService extends Service {

private WindowManager mWindowManager;
private View mFloatingView;

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

@Override
public void onCreate() {
    super.onCreate();
    Log.d(TAG, "onCreate: Floating Service is create");

    //the xml file
    //Inflate the floating view layout we created
    mFloatingView = LayoutInflater.from(this).inflate(R.layout.layout_floating_widget, null);

    //Add the view to the window.
    final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            LAYOUT_FLAG,
            WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);



    //Specify the view position
    params.gravity = Gravity.TOP | Gravity.LEFT;        //Initially view will be added to top-left corner
    params.x = 0;
    params.y = 100;

    //Add the view to the window
    mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    mWindowManager.addView(mFloatingView, params);

    //The root element of the iconView view layout
    final View iconView = mFloatingView.findViewById(R.id.icon_view);


    //the icon view inside the xml file
    //Open the application on the tap click
    iconView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //Open the application with click.
            Intent intent = new Intent(FloatingViewService.this, MyActivity.class);
            startActivity(intent);

            //close the service and remove view from the view hierarchy
            stopSelf();
        }
    });

    //Drag and move floating view using user's touch action.
    mFloatingView.findViewById(R.id.root_container).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:

                    //remember the initial position.
                    initialX = params.x;
                    initialY = params.y;

                    //get the touch location
                    initialTouchX = event.getRawX();
                    initialTouchY = event.getRawY();
                    return true;
                case MotionEvent.ACTION_UP:
                    return true;
                case MotionEvent.ACTION_MOVE:
                    //Calculate the X and Y coordinates of the view.
                    params.x = initialX + (int) (event.getRawX() - initialTouchX);
                    params.y = initialY + (int) (event.getRawY() - initialTouchY);


                    //Update the layout with new X & Y coordinate
                    mWindowManager.updateViewLayout(mFloatingView, params);
                    return true;
            }
            return false;
        }
    });
}


@Override
public void onDestroy() {
    super.onDestroy();
    if (mFloatingView != null) mWindowManager.removeView(mFloatingView);
}
}

0 个答案:

没有答案