Skip to content

Commit 6e75f89

Browse files
committed
优化屏幕旋转后悬浮窗显示的位置
1 parent f18dc5f commit 6e75f89

File tree

12 files changed

+222
-44
lines changed

12 files changed

+222
-44
lines changed

.github/ISSUE_TEMPLATE/issue_template_bug.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ assignees: getActivity
2222

2323
* 出现问题的安卓版本【必填】:请填写出现问题的 Android 版本
2424

25+
* 问题信息的来源渠道【必填】:请填写问题的来源(例如:自己遇到的/Bugly 看到的/用户反馈等等)
26+
2527
#### 请回答
2628

2729
* 是部分机型还是所有机型都会出现【必答】:部分/全部(例如:某为,某 Android 版本会出现)

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# 悬浮窗框架
22

3-
* 项目地址:[Github](https://github.com/getActivity/XToast)[码云](https://gitee.com/getActivity/XToast)
3+
* 项目地址:[Github](https://github.com/getActivity/XToast)
44

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

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

99
![](picture/demo_code.png)
1010

@@ -49,7 +49,7 @@ android {
4949
5050
dependencies {
5151
// 悬浮窗框架:https://github.com/getActivity/XToast
52-
implementation 'com.github.getActivity:XToast:8.5'
52+
implementation 'com.github.getActivity:XToast:8.6'
5353
}
5454
```
5555

@@ -218,6 +218,10 @@ public final class FloatingLifecycle implements Application.ActivityLifecycleCal
218218
* Android 版本适配:[AndroidVersionAdapter](https://github.com/getActivity/AndroidVersionAdapter) ![](https://img.shields.io/github/stars/getActivity/AndroidVersionAdapter.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidVersionAdapter.svg)
219219

220220
* Android 代码规范:[AndroidCodeStandard](https://github.com/getActivity/AndroidCodeStandard) ![](https://img.shields.io/github/stars/getActivity/AndroidCodeStandard.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidCodeStandard.svg)
221+
222+
* Android 资源大汇总:[AndroidIndex](https://github.com/getActivity/AndroidIndex) ![](https://img.shields.io/github/stars/getActivity/AndroidIndex.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidIndex.svg)
223+
224+
* Android 开源排行榜:[AndroidGithubBoss](https://github.com/getActivity/AndroidGithubBoss) ![](https://img.shields.io/github/stars/getActivity/AndroidGithubBoss.svg) ![](https://img.shields.io/github/forks/getActivity/AndroidGithubBoss.svg)
221225

222226
* Studio 精品插件:[StudioPlugins](https://github.com/getActivity/StudioPlugins) ![](https://img.shields.io/github/stars/getActivity/StudioPlugins.svg) ![](https://img.shields.io/github/forks/getActivity/StudioPlugins.svg)
223227

@@ -229,7 +233,7 @@ public final class FloatingLifecycle implements Application.ActivityLifecycleCal
229233

230234
![](https://raw.githubusercontent.com/getActivity/Donate/master/picture/official_ccount.png)
231235

232-
#### Android 技术分享 QQ 群:78797078
236+
#### Android 技术 Q 群:10047167
233237

234238
#### 如果您觉得我的开源库帮你节省了大量的开发时间,请扫描下方的二维码随意打赏,要是能打赏个 10.24 :monkey_face:就太:thumbsup:了。您的支持将鼓励我继续创作:octocat:
235239

app/build.gradle

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ android {
77
applicationId "com.hjq.xtoast.demo"
88
minSdkVersion 16
99
targetSdkVersion 31
10-
versionCode 85
11-
versionName "8.5"
10+
versionCode 86
11+
versionName "8.6"
1212
}
1313

1414
// 支持 Java JDK 8
@@ -52,22 +52,23 @@ android {
5252
dependencies {
5353
// 依赖 libs 目录下所有的 jar 和 aar 包
5454
implementation fileTree(include: ['*.jar', '*.aar'], dir: 'libs')
55+
5556
implementation project(':library')
5657

5758
// AndroidX 库:https://github.com/androidx/androidx
58-
implementation 'androidx.appcompat:appcompat:1.3.1'
59+
implementation 'androidx.appcompat:appcompat:1.4.0'
5960
// Material 库:https://github.com/material-components/material-components-android
6061
implementation 'com.google.android.material:material:1.4.0'
6162

6263
// 权限请求框架:https://github.com/getActivity/XXPermissions
63-
implementation 'com.github.getActivity:XXPermissions:13.2'
64+
implementation 'com.github.getActivity:XXPermissions:16.2'
6465

6566
// 标题栏框架:https://github.com/getActivity/TitleBar
66-
implementation 'com.github.getActivity:TitleBar:9.3'
67+
implementation 'com.github.getActivity:TitleBar:9.6'
6768

6869
// 吐司框架:https://github.com/getActivity/ToastUtils
69-
implementation 'com.github.getActivity:ToastUtils:10.0'
70+
implementation 'com.github.getActivity:ToastUtils:10.5'
7071

7172
// 内存泄漏捕捉:https://github.com/square/leakcanary
72-
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.7'
73+
debugImplementation 'com.squareup.leakcanary:leakcanary-android:2.9.1'
7374
}

app/src/main/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
<activity
1515
android:name=".MainActivity"
16+
android:configChanges="keyboardHidden|orientation|screenSize"
1617
android:exported="true">
1718

1819
<intent-filter>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<resources>
3+
4+
<!-- Base application theme. -->
5+
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
6+
<!-- Customize your theme here. -->
7+
<item name="colorPrimary">@color/colorPrimary</item>
8+
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
9+
<item name="colorAccent">@color/colorAccent</item>
10+
<item name="android:statusBarColor">@android:color/white</item>
11+
<item name="android:windowLightStatusBar">true</item>
12+
<!-- 解决 Android 5.1 及以上版本 Button 英文字符串自动变大写的问题 -->
13+
<item name="android:textAllCaps">false</item>
14+
</style>
15+
16+
</resources>

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<resources>
23
<string name="app_name">XToast</string>
34
</resources>

app/src/main/res/values/styles.xml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
12
<resources>
23

34
<!-- Base application theme. -->
@@ -8,8 +9,6 @@
89
<item name="colorAccent">@color/colorAccent</item>
910
<!-- 解决 Android 5.1 及以上版本 Button 英文字符串自动变大写的问题 -->
1011
<item name="android:textAllCaps">false</item>
11-
<!-- 设置窗口全屏 -->
12-
<!-- <item name="android:windowFullscreen">true</item>-->
1312
</style>
1413

1514
<!-- 缩放动画 -->

library/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ android {
55

66
defaultConfig {
77
minSdkVersion 14
8-
versionCode 85
9-
versionName "8.5"
8+
versionCode 86
9+
versionName "8.6"
1010
}
1111

1212
// 支持 JDK 1.8
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.hjq.xtoast;
2+
3+
import android.content.ComponentCallbacks;
4+
import android.content.Context;
5+
import android.content.res.Configuration;
6+
7+
/**
8+
* author : Android 轮子哥
9+
* github : https://github.com/getActivity/XToast
10+
* time : 2022/10/03
11+
* desc : 屏幕方向旋转监听
12+
*/
13+
final class ScreenOrientationMonitor implements ComponentCallbacks {
14+
15+
private int mScreenOrientation;
16+
17+
private OnScreenOrientationCallback mCallback;
18+
19+
public ScreenOrientationMonitor(Context context) {
20+
mScreenOrientation = context.getResources().getConfiguration().orientation;
21+
context.registerComponentCallbacks(this);
22+
}
23+
24+
/**
25+
* 注册监听
26+
*/
27+
void register(Context context) {
28+
context.registerComponentCallbacks(this);
29+
}
30+
31+
/**
32+
* 取消监听
33+
*/
34+
void unregister(Context context) {
35+
context.unregisterComponentCallbacks(this);
36+
}
37+
38+
@Override
39+
public void onConfigurationChanged(Configuration newConfig) {
40+
if (mScreenOrientation == newConfig.orientation) {
41+
return;
42+
}
43+
mScreenOrientation = newConfig.orientation;
44+
45+
if (mCallback == null) {
46+
return;
47+
}
48+
mCallback.onScreenOrientationChange(mScreenOrientation);
49+
}
50+
51+
@Override
52+
public void onLowMemory() {}
53+
54+
public void setOnScreenOrientationCallback(OnScreenOrientationCallback callback) {
55+
mCallback = callback;
56+
}
57+
58+
public interface OnScreenOrientationCallback {
59+
60+
/**
61+
* 显示回调
62+
*/
63+
default void onScreenOrientationChange(int orientation) {}
64+
}
65+
}

library/src/main/java/com/hjq/xtoast/XToast.java

Lines changed: 54 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
* doc : https://developer.android.google.cn/reference/android/view/WindowManager.html
3636
* https://developer.android.google.cn/reference/kotlin/android/view/WindowManager.LayoutParams?hl=en
3737
*/
38-
@SuppressWarnings({"unchecked", "unused", "deprecation", "UnusedReturnValue"})
39-
public class XToast<X extends XToast<?>> implements Runnable {
38+
@SuppressWarnings({"unchecked", "unused", "UnusedReturnValue"})
39+
public class XToast<X extends XToast<?>> implements Runnable, ScreenOrientationMonitor.OnScreenOrientationCallback {
4040

4141
private static final Handler HANDLER = new Handler(Looper.getMainLooper());
4242

@@ -60,6 +60,9 @@ public class XToast<X extends XToast<?>> implements Runnable {
6060
/** 吐司显示和取消监听 */
6161
private OnLifecycle mListener;
6262

63+
/** 屏幕旋转监听 */
64+
private ScreenOrientationMonitor mScreenOrientationMonitor;
65+
6366
/**
6467
* 创建一个局部悬浮窗
6568
*/
@@ -451,16 +454,25 @@ public X setDraggable() {
451454
* 设置拖动规则
452455
*/
453456
public X setDraggable(BaseDraggable draggable) {
454-
// 如果当前是否设置了不可触摸,如果是就擦除掉这个标记
455-
clearWindowFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
456-
// 如果当前是否设置了可移动窗口到屏幕之外,如果是就擦除这个标记
457-
clearWindowFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
458-
459457
mDraggable = draggable;
460-
if (isShowing()) {
461-
update();
462-
mDraggable.start(this);
458+
if (draggable != null) {
459+
// 如果当前是否设置了不可触摸,如果是就擦除掉这个标记
460+
clearWindowFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
461+
// 如果当前是否设置了可移动窗口到屏幕之外,如果是就擦除这个标记
462+
clearWindowFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
463+
464+
if (isShowing()) {
465+
update();
466+
draggable.start(this);
467+
}
468+
}
469+
470+
if (mScreenOrientationMonitor == null) {
471+
mScreenOrientationMonitor = new ScreenOrientationMonitor(mContext);
472+
mScreenOrientationMonitor.setOnScreenOrientationCallback(this);
463473
}
474+
mScreenOrientationMonitor.register(mContext);
475+
464476
return (X) this;
465477
}
466478

@@ -733,6 +745,9 @@ public void recycle() {
733745
if (isShowing()) {
734746
cancel();
735747
}
748+
if (mScreenOrientationMonitor != null) {
749+
mScreenOrientationMonitor.unregister(mContext);
750+
}
736751
if (mListener != null) {
737752
mListener.onRecycler(this);
738753
}
@@ -841,19 +856,33 @@ public X setText(int id, CharSequence text) {
841856
}
842857

843858
/**
844-
* 设置文本颜色
859+
* 设置字体颜色
845860
*/
846861
public X setTextColor(int id, int color) {
847862
((TextView) findViewById(id)).setTextColor(color);
848863
return (X) this;
849864
}
850865

866+
/**
867+
* 设置字体大小
868+
*/
869+
public X setTextSize(int id, float size) {
870+
((TextView) findViewById(id)).setTextSize(size);
871+
return (X) this;
872+
}
873+
874+
public X setTextSize(int id, int unit, float size) {
875+
((TextView) findViewById(id)).setTextSize(unit, size);
876+
return (X) this;
877+
}
878+
851879
/**
852880
* 设置提示
853881
*/
854882
public X setHint(int viewId, int stringId) {
855883
return setHint(viewId, mContext.getResources().getString(stringId));
856884
}
885+
857886
public X setHint(int id, CharSequence text) {
858887
((TextView) findViewById(id)).setHint(text);
859888
return (X) this;
@@ -1018,6 +1047,20 @@ public void run() {
10181047
cancel();
10191048
}
10201049

1050+
/**
1051+
* {@link ScreenOrientationMonitor.OnScreenOrientationCallback}
1052+
*/
1053+
@Override
1054+
public void onScreenOrientationChange(int orientation) {
1055+
if (!isShowing()) {
1056+
return;
1057+
}
1058+
if (mDraggable == null) {
1059+
return;
1060+
}
1061+
mDraggable.onScreenOrientationChange(orientation);
1062+
}
1063+
10211064
/**
10221065
* View 的点击事件监听
10231066
*/

0 commit comments

Comments
 (0)