android - WindowManager X Y 不工作

标签 android overlay window-managers

我正在使用 Android 窗口管理器在用户手机上显示叠加图像,但它没有将 ImageView 设置在正确的位置,它始终默认位于屏幕中央。我知道该位置是正确的,因为我已经使用无障碍服务来触摸该位置,并且在我的手机中打开了显示触摸功能,它正在触摸正确的位置。那么为什么 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.LayoutParamsgravity 设置为Gravity.TOP | Gravity.LEFT

正如您所注意到的,它默认设置为 Gravity.CENTER,因此对于 x=0y=0 您的 mFloatingView 应该正好在屏幕中间。

关于android - WindowManager X Y 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51224921/

相关文章:

android - Heyzap Video Ad Sdk 6.5.1 Android应用程序在Fetch上崩溃

android - 加快 FFmpeg 处理时间

android - 状态栏上的图像叠加

twitter-bootstrap-3 - 覆盖 bootstrap div 行

android - 带动画的窗口管理器

java - 使用 WindowManager 覆盖页脚

linux - LightDM 添加窗口管理器(Openbox)

Android:AsyncTask onPreExecute() 方法未在 UI 线程中执行

android - MapItemizedOverlay 中的 OnTouchEvent

android - 在应用程序处于非 Activity 状态时处理 ActivityRecognition.API Intent