Skip to content

Commit

Permalink
修复在锁屏界面进行屏幕旋转回来导致悬浮窗位置计算不正确的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
getActivity committed Nov 25, 2023
1 parent 19e2ebb commit 46053d3
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 16 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

* 博客地址:[悬浮窗需求终结者](https://www.jianshu.com/p/247d705b87b6)

* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/EasyWindow/releases/download/10.5/EasyWindow.apk)
* 可以扫码下载 Demo 进行演示或者测试,如果扫码下载不了的,[点击此处可直接下载](https://github.com/getActivity/EasyWindow/releases/download/10.6/EasyWindow.apk)

![](picture/demo_code.png)

Expand Down Expand Up @@ -49,7 +49,7 @@ android {
dependencies {
// 悬浮窗框架:https://github.com/getActivity/EasyWindow
implementation 'com.github.getActivity:EasyWindow:10.5'
implementation 'com.github.getActivity:EasyWindow:10.6'
}
```

Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.hjq.window.demo"
minSdkVersion 16
targetSdkVersion 33
versionCode 1005
versionName "10.5"
versionCode 1006
versionName "10.6"
}

// 支持 Java JDK 8
Expand Down
4 changes: 2 additions & 2 deletions library/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ android {

defaultConfig {
minSdkVersion 14
versionCode 1005
versionName "10.5"
versionCode 1006
versionName "10.6"
}

// 支持 JDK 1.8
Expand Down
50 changes: 40 additions & 10 deletions library/src/main/java/com/hjq/window/draggable/BaseDraggable.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import android.view.DisplayCutout;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnLayoutChangeListener;
import android.view.Window;
import android.view.WindowInsets;
import android.view.WindowManager;
Expand Down Expand Up @@ -136,6 +137,8 @@ public void refreshWindowInfo() {
return;
}

// Log.i(getClass().getSimpleName(), "刷新当前 Window 信息");

// 这里为什么要这么写,因为发现了鸿蒙手机在进行屏幕旋转的时候
// 回调 onConfigurationChanged 方法的时候获取到这些参数已经变化了
// 所以需要提前记录下来,避免后续进行坐标计算的时候出现问题
Expand All @@ -145,6 +148,14 @@ public void refreshWindowInfo() {

mCurrentWindowInvisibleWidth = mTempRect.left;
mCurrentWindowInvisibleHeight = mTempRect.top;

/*
Log.i(getClass().getSimpleName(),
"CurrentWindowWidth = " + mCurrentWindowWidth +
",CurrentWindowHeight = " + mCurrentWindowHeight +
",CurrentWindowInvisibleWidth = " + mCurrentWindowInvisibleWidth +
",CurrentWindowInvisibleHeight = " + mCurrentWindowInvisibleHeight);
*/
}

/**
Expand All @@ -163,9 +174,11 @@ public void refreshLocationCoordinate() {
}

/**
* 屏幕方向发生了变化
* 屏幕方向发生了改变
*/
public void onScreenOrientationChange() {
// Log.i(getClass().getSimpleName(), "屏幕方向发生了改变");

long refreshDelayMillis = 100;

if (!isFollowScreenRotationChanges()) {
Expand All @@ -179,6 +192,8 @@ public void onScreenOrientationChange() {
int viewWidth = getDecorView().getWidth();
int viewHeight = getDecorView().getHeight();

// Log.i(getClass().getSimpleName(), "当前 ViewWidth = " + viewWidth + ",ViewHeight = " + viewHeight);

int startX = mCurrentViewOnScreenX - mCurrentWindowInvisibleWidth;
int startY = mCurrentViewOnScreenY - mCurrentWindowInvisibleHeight;

Expand Down Expand Up @@ -207,15 +222,30 @@ public void onScreenOrientationChange() {
percentY = centerY / mCurrentWindowHeight;
}

getEasyWindow().postDelayed(() -> {
// 先刷新当前窗口信息
refreshWindowInfo();
int x = Math.max((int) (mCurrentWindowWidth * percentX - viewWidth / 2f), 0);
int y = Math.max((int) (mCurrentWindowHeight * percentY - viewWidth / 2f), 0);
updateLocation(x, y);
// 需要注意,这里需要延迟执行,否则会有问题
getEasyWindow().post(this::onScreenRotateInfluenceCoordinateChangeFinish);
}, refreshDelayMillis);
View decorView = getDecorView();
if (decorView == null) {
return;
}

// Github issue 地址:https://github.com/getActivity/EasyWindow/issues/49
// 修复在竖屏状态下,先锁屏,再旋转到横屏,后进行解锁,出现的 View.getWindowVisibleDisplayFrame 计算有问题的 Bug
// 这是因为屏幕在旋转的时候,视图正处于改变状态,此时通过 View 获取窗口可视区域是有问题,会获取到旧的可视区域
// 解决方案是监听一下 View 布局变化监听,在收到回调的时候再去获取 View 获取窗口可视区域
decorView.addOnLayoutChangeListener(new OnLayoutChangeListener() {
@Override
public void onLayoutChange(View view, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
view.removeOnLayoutChangeListener(this);
view.postDelayed(() -> {
// 先刷新当前窗口信息
refreshWindowInfo();
int x = Math.max((int) (mCurrentWindowWidth * percentX - viewWidth / 2f), 0);
int y = Math.max((int) (mCurrentWindowHeight * percentY - viewWidth / 2f), 0);
updateLocation(x, y);
// 需要注意,这里需要延迟执行,否则会有问题
view.post(() -> onScreenRotateInfluenceCoordinateChangeFinish());
}, refreshDelayMillis);
}
});
}

/**
Expand Down

0 comments on commit 46053d3

Please sign in to comment.