Android WindowManager X Y不工作

Android WindowManager X Y不工作,android,overlay,window-managers,Android,Overlay,Window Managers,我正在使用Android window manager在用户手机上显示覆盖图像,但它没有将ImageView设置在正确的位置,它始终默认位于屏幕的中心。我知道该位置是正确的,因为我使用了辅助功能服务来触摸该位置,并且在手机中打开show Touchs时,它正在触摸正确的位置。那么为什么imageview不会转到这个位置呢 我在我的应用程序中获取要放置图像的位置和imageview: Point centerPoint = getCenterPointOfView(myImageView); in

我正在使用Android window manager在用户手机上显示覆盖图像,但它没有将ImageView设置在正确的位置,它始终默认位于屏幕的中心。我知道该位置是正确的,因为我使用了辅助功能服务来触摸该位置,并且在手机中打开show Touchs时,它正在触摸正确的位置。那么为什么imageview不会转到这个位置呢

我在我的应用程序中获取要放置图像的位置和imageview:

Point centerPoint = getCenterPointOfView(myImageView);
int xImageLocation = centerPoint.x;
int yImageLocation = centerPoint.y;
然后我用以下内容打开新的覆盖:

//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_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

params.x = xImageLocation;
params.y = yImageLocation;

mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
    if (mWindowManager != null) {
        mWindowManager.addView(mFloatingView, params);
    }else {
        return;
    }

您需要将WindowManager.LayoutParams的
gravity
设置为
gravity.TOP | gravity.LEFT


默认情况下,它被设置为<代码> Realth.Center ,所以<<代码> x=0 和<代码> y=0 >您的<代码> MFLUTIGIVIEW 应该正好在屏幕的中间。

所有这些都是把它移到屏幕的顶部和屏幕的右边,它没有将它放在我设置的X Y的中心。你通过什么样的
X
Y
进入布局,具有左上角重力和
0,0
它应该使你的视图定位到屏幕的左上角。如果我做0,0,它会将它放在左上角。。。但我并不是想做0,0,我是想把它放在我有的x,y屏幕坐标上,所以你必须从目标x和y上减去你的视图宽度和高度的一半,非常简单。