我正在使用 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.LayoutParams
的gravity
设置为Gravity.TOP | Gravity.LEFT
。
正如您所注意到的,它默认设置为 Gravity.CENTER
,因此对于 x=0
和 y=0
您的 mFloatingView
应该正好在屏幕中间。
关于android - WindowManager X Y 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51224921/