├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── styles.xml
│ │ │ ├── mipmap-hdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-mdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-xxxhdpi
│ │ │ │ ├── ic_launcher.png
│ │ │ │ └── ic_launcher_round.png
│ │ │ ├── mipmap-anydpi-v26
│ │ │ │ ├── ic_launcher.xml
│ │ │ │ └── ic_launcher_round.xml
│ │ │ ├── layout
│ │ │ │ ├── small_window.xml
│ │ │ │ ├── activity_second.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── luwei
│ │ │ │ └── com
│ │ │ │ └── smallwindow
│ │ │ │ ├── SecondActivity.java
│ │ │ │ ├── MainActivity.java
│ │ │ │ ├── MyApplication.java
│ │ │ │ ├── SmallWindowView.java
│ │ │ │ └── BaseActivity.java
│ │ └── AndroidManifest.xml
│ ├── test
│ │ └── java
│ │ │ └── luwei
│ │ │ └── com
│ │ │ └── smallwindow
│ │ │ └── ExampleUnitTest.java
│ └── androidTest
│ │ └── java
│ │ └── luwei
│ │ └── com
│ │ └── smallwindow
│ │ └── ExampleInstrumentedTest.java
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .idea
├── caches
│ └── build_file_checksums.ser
├── vcs.xml
├── runConfigurations.xml
├── gradle.xml
├── codeStyles
│ └── Project.xml
└── misc.xml
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── gradlew
└── README.md
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app'
2 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | SmallWindow
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/.idea/caches/build_file_checksums.ser:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/.idea/caches/build_file_checksums.ser
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/luweicheng24/SmallWindow/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/libraries
5 | /.idea/modules.xml
6 | /.idea/workspace.xml
7 | .DS_Store
8 | /build
9 | /captures
10 | .externalNativeBuild
11 |
--------------------------------------------------------------------------------
/.idea/vcs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Aug 23 17:58:14 CST 2018
2 | distributionBase=GRADLE_USER_HOME
3 | distributionPath=wrapper/dists
4 | zipStoreBase=GRADLE_USER_HOME
5 | zipStorePath=wrapper/dists
6 | distributionUrl=https\://services.gradle.org/distributions/gradle-4.4-all.zip
7 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-anydpi-v26/ic_launcher_round.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/test/java/luwei/com/smallwindow/ExampleUnitTest.java:
--------------------------------------------------------------------------------
1 | package luwei.com.smallwindow;
2 |
3 | import org.junit.Test;
4 |
5 | import static org.junit.Assert.*;
6 |
7 | /**
8 | * Example local unit test, which will execute on the development machine (host).
9 | *
10 | * @see Testing documentation
11 | */
12 | public class ExampleUnitTest {
13 | @Test
14 | public void addition_isCorrect() {
15 | assertEquals(4, 2 + 2);
16 | }
17 | }
--------------------------------------------------------------------------------
/.idea/runConfigurations.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/small_window.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
16 |
--------------------------------------------------------------------------------
/.idea/gradle.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
--------------------------------------------------------------------------------
/app/src/main/java/luwei/com/smallwindow/SecondActivity.java:
--------------------------------------------------------------------------------
1 | package luwei.com.smallwindow;
2 |
3 | import android.os.Bundle;
4 | import android.support.annotation.Nullable;
5 | import android.view.View;
6 |
7 | /**
8 | * Author : luweicheng on 2018/8/23 18:00
9 | * E-mail :1769005961@qq.com
10 | * GitHub : https://github.com/luweicheng24
11 | * function :
12 | **/
13 |
14 | public class SecondActivity extends BaseActivity {
15 | @Override
16 | protected void onCreate(@Nullable Bundle savedInstanceState) {
17 | super.onCreate(savedInstanceState);
18 | setContentView(R.layout.activity_second);
19 | }
20 |
21 |
22 | public void dismissWindow(View view) {
23 | dismissWindow();
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 | # IDE (e.g. Android Studio) users:
3 | # Gradle settings configured through the IDE *will override*
4 | # any settings specified in this file.
5 | # For more details on how to configure your build environment visit
6 | # http://www.gradle.org/docs/current/userguide/build_environment.html
7 | # Specifies the JVM arguments used for the daemon process.
8 | # The setting is particularly useful for tweaking memory settings.
9 | org.gradle.jvmargs=-Xmx1536m
10 | # When configured, Gradle will run in incubating parallel mode.
11 | # This option should only be used with decoupled projects. More details, visit
12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
13 | # org.gradle.parallel=true
14 |
--------------------------------------------------------------------------------
/app/proguard-rules.pro:
--------------------------------------------------------------------------------
1 | # Add project specific ProGuard rules here.
2 | # You can control the set of applied configuration files using the
3 | # proguardFiles setting in build.gradle.
4 | #
5 | # For more details, see
6 | # http://developer.android.com/guide/developing/tools/proguard.html
7 |
8 | # If your project uses WebView with JS, uncomment the following
9 | # and specify the fully qualified class name to the JavaScript interface
10 | # class:
11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12 | # public *;
13 | #}
14 |
15 | # Uncomment this to preserve the line number information for
16 | # debugging stack traces.
17 | #-keepattributes SourceFile,LineNumberTable
18 |
19 | # If you keep the line number information, uncomment this to
20 | # hide the original source file name.
21 | #-renamesourcefileattribute SourceFile
22 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/luwei/com/smallwindow/ExampleInstrumentedTest.java:
--------------------------------------------------------------------------------
1 | package luwei.com.smallwindow;
2 |
3 | import android.content.Context;
4 | import android.support.test.InstrumentationRegistry;
5 | import android.support.test.runner.AndroidJUnit4;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 |
10 | import static org.junit.Assert.*;
11 |
12 | /**
13 | * Instrumented test, which will execute on an Android device.
14 | *
15 | * @see Testing documentation
16 | */
17 | @RunWith(AndroidJUnit4.class)
18 | public class ExampleInstrumentedTest {
19 | @Test
20 | public void useAppContext() {
21 | // Context of the app under test.
22 | Context appContext = InstrumentationRegistry.getTargetContext();
23 |
24 | assertEquals("luwei.com.smallwindow", appContext.getPackageName());
25 | }
26 | }
27 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
6 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
--------------------------------------------------------------------------------
/app/src/main/java/luwei/com/smallwindow/MainActivity.java:
--------------------------------------------------------------------------------
1 | package luwei.com.smallwindow;
2 |
3 | import android.content.Intent;
4 | import android.os.Bundle;
5 | import android.support.annotation.Nullable;
6 | import android.view.View;
7 |
8 | /**
9 | * Author : luweicheng on 2018/8/23 18:00
10 | * E-mail :1769005961@qq.com
11 | * GitHub : https://github.com/luweicheng24
12 | * function:
13 | **/
14 |
15 | public class MainActivity extends BaseActivity {
16 | @Override
17 | protected void onCreate(@Nullable Bundle savedInstanceState) {
18 | super.onCreate(savedInstanceState);
19 | setContentView(R.layout.activity_main);
20 | }
21 |
22 | // 弹出窗口
23 | public void alertWindow(View view) {
24 | alertWindow(); // 调用Base中的弹出窗口
25 | }
26 |
27 | public void dismissWindow(View view) {
28 | dismissWindow();
29 | }
30 |
31 | public void nextActivity(View view) {
32 | startActivity(new Intent(this, SecondActivity.class));
33 | }
34 |
35 | }
36 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 27
5 | defaultConfig {
6 | applicationId "luwei.com.smallwindow"
7 | minSdkVersion 21
8 | targetSdkVersion 27
9 | versionCode 1
10 | versionName "1.0"
11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
12 | }
13 | buildTypes {
14 | release {
15 | minifyEnabled false
16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
17 | }
18 | }
19 | }
20 |
21 | dependencies {
22 | implementation fileTree(dir: 'libs', include: ['*.jar'])
23 | implementation 'com.android.support:appcompat-v7:27.1.1'
24 | implementation 'com.android.support.constraint:constraint-layout:1.1.2'
25 | testImplementation 'junit:junit:4.12'
26 | androidTestImplementation 'com.android.support.test:runner:1.0.2'
27 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
28 | }
29 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_second.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
17 |
25 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
9 |
10 |
17 |
18 |
28 |
29 |
39 |
40 |
49 |
--------------------------------------------------------------------------------
/.idea/codeStyles/Project.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
--------------------------------------------------------------------------------
/app/src/main/java/luwei/com/smallwindow/MyApplication.java:
--------------------------------------------------------------------------------
1 | package luwei.com.smallwindow;
2 |
3 | import android.app.Application;
4 | import android.content.Context;
5 | import android.graphics.PixelFormat;
6 | import android.view.Gravity;
7 | import android.view.LayoutInflater;
8 | import android.view.ViewGroup;
9 | import android.view.WindowManager;
10 |
11 | /**
12 | * Author : luweicheng on 2018/8/25 12:06
13 | * E-mail :1769005961@qq.com
14 | * GitHub : https://github.com/luweicheng24
15 | * function:
16 | **/
17 |
18 | public class MyApplication extends Application {
19 |
20 | private SmallWindowView windowView;
21 | private WindowManager wm;
22 | private WindowManager.LayoutParams mLayoutParams;
23 | public SmallWindowView getWindowView() {
24 | return windowView;
25 | }
26 | public WindowManager getWm() {
27 | return wm;
28 | }
29 | public WindowManager.LayoutParams getmLayoutParams() {
30 | return mLayoutParams;
31 | }
32 | @Override
33 | public void onCreate() {
34 | super.onCreate();
35 | initSmallViewLayout();
36 | }
37 | public void initSmallViewLayout() {
38 | windowView = (SmallWindowView) LayoutInflater.from(this).inflate(R.layout.small_window, null);
39 | wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
40 | mLayoutParams = new WindowManager.LayoutParams(
41 | ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
42 | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
43 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
44 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
45 | PixelFormat.TRANSLUCENT);
46 | mLayoutParams.gravity = Gravity.NO_GRAVITY;
47 | //使用非CENTER时,可以通过设置XY的值来改变View的位置
48 | windowView.setWm(wm);
49 | windowView.setWmParams(mLayoutParams);
50 | }
51 | }
52 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/gradlew.bat:
--------------------------------------------------------------------------------
1 | @if "%DEBUG%" == "" @echo off
2 | @rem ##########################################################################
3 | @rem
4 | @rem Gradle startup script for Windows
5 | @rem
6 | @rem ##########################################################################
7 |
8 | @rem Set local scope for the variables with windows NT shell
9 | if "%OS%"=="Windows_NT" setlocal
10 |
11 | set DIRNAME=%~dp0
12 | if "%DIRNAME%" == "" set DIRNAME=.
13 | set APP_BASE_NAME=%~n0
14 | set APP_HOME=%DIRNAME%
15 |
16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
17 | set DEFAULT_JVM_OPTS=
18 |
19 | @rem Find java.exe
20 | if defined JAVA_HOME goto findJavaFromJavaHome
21 |
22 | set JAVA_EXE=java.exe
23 | %JAVA_EXE% -version >NUL 2>&1
24 | if "%ERRORLEVEL%" == "0" goto init
25 |
26 | echo.
27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
28 | echo.
29 | echo Please set the JAVA_HOME variable in your environment to match the
30 | echo location of your Java installation.
31 |
32 | goto fail
33 |
34 | :findJavaFromJavaHome
35 | set JAVA_HOME=%JAVA_HOME:"=%
36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe
37 |
38 | if exist "%JAVA_EXE%" goto init
39 |
40 | echo.
41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
42 | echo.
43 | echo Please set the JAVA_HOME variable in your environment to match the
44 | echo location of your Java installation.
45 |
46 | goto fail
47 |
48 | :init
49 | @rem Get command-line arguments, handling Windows variants
50 |
51 | if not "%OS%" == "Windows_NT" goto win9xME_args
52 |
53 | :win9xME_args
54 | @rem Slurp the command line arguments.
55 | set CMD_LINE_ARGS=
56 | set _SKIP=2
57 |
58 | :win9xME_args_slurp
59 | if "x%~1" == "x" goto execute
60 |
61 | set CMD_LINE_ARGS=%*
62 |
63 | :execute
64 | @rem Setup the command line
65 |
66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
67 |
68 | @rem Execute Gradle
69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
70 |
71 | :end
72 | @rem End local scope for the variables with windows NT shell
73 | if "%ERRORLEVEL%"=="0" goto mainEnd
74 |
75 | :fail
76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
77 | rem the _cmd.exe /c_ return code!
78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1
79 | exit /b 1
80 |
81 | :mainEnd
82 | if "%OS%"=="Windows_NT" endlocal
83 |
84 | :omega
85 |
--------------------------------------------------------------------------------
/app/src/main/java/luwei/com/smallwindow/SmallWindowView.java:
--------------------------------------------------------------------------------
1 | package luwei.com.smallwindow;
2 |
3 | import android.content.Context;
4 | import android.support.annotation.Nullable;
5 | import android.util.AttributeSet;
6 | import android.util.DisplayMetrics;
7 | import android.util.Log;
8 | import android.view.Gravity;
9 | import android.view.MotionEvent;
10 | import android.view.WindowManager;
11 | import android.widget.LinearLayout;
12 |
13 | /**
14 | * Author : luweicheng on 2018/8/24 11:22
15 | * E-mail :1769005961@qq.com
16 | * GitHub : https://github.com/luweicheng24
17 | * function:
18 | **/
19 |
20 | public class SmallWindowView extends LinearLayout {
21 | private final int screenHeight;
22 | private final int screenWidth;
23 | private int statusHeight;
24 | private float mTouchStartX;
25 | private float mTouchStartY;
26 | private float x;
27 | private float y;
28 |
29 | private WindowManager wm;
30 | public WindowManager.LayoutParams wmParams;
31 |
32 |
33 | public SmallWindowView(Context context) {
34 | this(context, null);
35 | }
36 |
37 | public WindowManager getWm() {
38 | return wm;
39 | }
40 |
41 | public void setWm(WindowManager wm) {
42 | this.wm = wm;
43 | }
44 |
45 | public WindowManager.LayoutParams getWmParams() {
46 | return wmParams;
47 | }
48 |
49 | public void setWmParams(WindowManager.LayoutParams wmParams) {
50 | this.wmParams = wmParams;
51 | this.wmParams.x = screenWidth;
52 | }
53 |
54 | public SmallWindowView(Context context, @Nullable AttributeSet attrs) {
55 | this(context, attrs, 0);
56 | }
57 |
58 | public SmallWindowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
59 | super(context, attrs, defStyleAttr);
60 | statusHeight = getStatusHeight(context);
61 | DisplayMetrics dm = getResources().getDisplayMetrics();
62 | screenHeight = dm.heightPixels;
63 | screenWidth = dm.widthPixels;
64 |
65 | }
66 |
67 |
68 | /**
69 | * 获得状态栏的高度
70 | *
71 | * @param context
72 | * @return
73 | */
74 | public static int getStatusHeight(Context context) {
75 | int statusHeight = -1;
76 | try {
77 | Class clazz = Class.forName("com.android.internal.R$dimen");
78 | Object object = clazz.newInstance();
79 | int height = Integer.parseInt(clazz.getField("status_bar_height")
80 | .get(object).toString());
81 | statusHeight = context.getResources().getDimensionPixelSize(height);
82 | } catch (Exception e) {
83 | e.printStackTrace();
84 | }
85 | return statusHeight;
86 | }
87 |
88 | boolean isRight = true;
89 |
90 | @Override
91 | public boolean onTouchEvent(MotionEvent event) {
92 | x = event.getRawX();
93 | y = event.getRawY() - statusHeight;
94 | switch (event.getAction()) {
95 | case MotionEvent.ACTION_DOWN:
96 | if (wmParams.x > 0) {
97 | isRight = true;
98 | }
99 | if (wmParams.x < 0) {
100 | isRight = false;
101 | }
102 | mTouchStartX = event.getX();
103 | mTouchStartY = event.getY();
104 | Log.i("startP", "startX" + mTouchStartX + "====startY" + mTouchStartY);
105 | break;
106 |
107 | case MotionEvent.ACTION_MOVE:
108 | updateViewPosition();
109 | break;
110 | case MotionEvent.ACTION_UP:
111 |
112 | if (wmParams.x <= 0) {
113 | wmParams.x = Math.abs(wmParams.x) <= screenWidth / 2 ? -screenWidth : screenWidth;
114 | } else {
115 | wmParams.x = wmParams.x <= screenWidth / 2 ? screenWidth : -screenWidth;
116 | }
117 |
118 | // wmParams.x = screenWidth;
119 | wmParams.y = (int) (y - screenHeight / 2);
120 | wm.updateViewLayout(this, wmParams);
121 | break;
122 | }
123 | return true;
124 | }
125 | private void updateViewPosition() {
126 | wmParams.gravity = Gravity.NO_GRAVITY;
127 | //更新浮动窗口位置参数
128 | int dx = (int) (mTouchStartX - x);
129 | int dy = (int) (y-screenHeight / 2);
130 | if (isRight) {
131 | wmParams.x = screenWidth / 2 - dx;
132 | } else {
133 | wmParams.x = -dx - screenWidth / 2;
134 | }
135 | wmParams.y = dy;
136 | Log.i("winParams", "x : " + wmParams.x + "y :" + wmParams.y + " dy :" + dy);
137 | wm.updateViewLayout(this, wmParams);
138 | //刷新显示
139 | }
140 | }
141 |
--------------------------------------------------------------------------------
/app/src/main/java/luwei/com/smallwindow/BaseActivity.java:
--------------------------------------------------------------------------------
1 | package luwei.com.smallwindow;
2 |
3 | import android.Manifest;
4 | import android.annotation.TargetApi;
5 | import android.content.Intent;
6 | import android.content.pm.PackageManager;
7 | import android.net.Uri;
8 | import android.os.Build;
9 | import android.os.Bundle;
10 | import android.provider.Settings;
11 | import android.support.annotation.NonNull;
12 | import android.support.v4.app.ActivityCompat;
13 | import android.support.v7.app.AppCompatActivity;
14 | import android.view.MotionEvent;
15 | import android.view.WindowManager;
16 | import android.widget.Toast;
17 |
18 | public class BaseActivity extends AppCompatActivity {
19 |
20 | private WindowManager wm;
21 | private SmallWindowView windowView;
22 | private WindowManager.LayoutParams mLayoutParams;
23 | private int OVERLAY_PERMISSION_REQ_CODE = 2;
24 | private boolean isRange = false;
25 |
26 | @Override
27 | protected void onCreate(Bundle savedInstanceState) {
28 | super.onCreate(savedInstanceState);
29 | wm = ((MyApplication)getApplication()).getWm();
30 | windowView = ((MyApplication)getApplication()).getWindowView();
31 | mLayoutParams = ((MyApplication)getApplication()).getmLayoutParams();
32 | }
33 |
34 |
35 | public void alertWindow() {
36 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // 7.0 以上需要引导用去设置开启窗口浮动权限
37 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 8.0 以上type需要设置成这个
38 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
39 | }
40 | requestDrawOverLays();
41 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // 6.0 动态申请
42 | ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, 1);
43 | }
44 | }
45 |
46 | @Override
47 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
48 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
49 | if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
50 | if (wm != null && windowView.getWm() == null) {
51 | wm.addView(windowView, mLayoutParams);
52 | }
53 | } else {
54 | Toast.makeText(this, "权限申请失败", Toast.LENGTH_SHORT).show();
55 | }
56 | }
57 |
58 |
59 | private int[] location = new int[2]; // 小窗口位置坐标
60 |
61 | @Override
62 | public boolean onTouchEvent(MotionEvent event) {
63 | if (event.getAction() == MotionEvent.ACTION_DOWN) {
64 | isRange = calcPointRange(event);
65 | }
66 | if (isRange) {
67 | windowView.dispatchTouchEvent(event);
68 | }
69 | return super.onTouchEvent(event);
70 | }
71 |
72 | /**
73 | * 计算当前点击事件坐标是否在小窗口内
74 | * @param event
75 | * @return
76 | */
77 | private boolean calcPointRange(MotionEvent event) {
78 | windowView.getLocationOnScreen(location);
79 | int width = windowView.getMeasuredWidth();
80 | int height = windowView.getMeasuredHeight();
81 | float curX = event.getRawX();
82 | float curY = event.getRawY();
83 | if (curX >= location[0] && curX <= location[0] + width && curY >= location[1] && curY <= location[1] + height) {
84 | return true;
85 | }
86 | return false;
87 | }
88 |
89 | private static final String TAG = "BaseActivity";
90 |
91 | // android 23 以上先引导用户开启这个权限 该权限动态申请不了
92 | @TargetApi(Build.VERSION_CODES.M)
93 | public void requestDrawOverLays() {
94 | if (!Settings.canDrawOverlays(BaseActivity.this)) {
95 | Toast.makeText(this, "can not DrawOverlays", Toast.LENGTH_SHORT).show();
96 | Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + BaseActivity.this.getPackageName()));
97 | startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
98 | } else {
99 | if (wm != null && windowView.getWindowId() == null) {
100 | wm.addView(windowView, mLayoutParams);
101 | }
102 | Toast.makeText(this, "权限已经授予", Toast.LENGTH_SHORT).show();
103 | }
104 | }
105 |
106 | @TargetApi(Build.VERSION_CODES.M)
107 | @Override
108 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
109 | if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
110 | if (!Settings.canDrawOverlays(this)) {
111 | Toast.makeText(this, "设置权限拒绝", Toast.LENGTH_SHORT).show();
112 | } else {
113 | Toast.makeText(this, "设置权限成功", Toast.LENGTH_SHORT).show();
114 | }
115 | }
116 | }
117 | // 移除window
118 | public void dismissWindow() {
119 | if (wm != null && windowView != null && windowView.getWindowId() != null) {
120 | wm.removeView(windowView);
121 | }
122 | }
123 | }
124 |
--------------------------------------------------------------------------------
/gradlew:
--------------------------------------------------------------------------------
1 | #!/usr/bin/env sh
2 |
3 | ##############################################################################
4 | ##
5 | ## Gradle start up script for UN*X
6 | ##
7 | ##############################################################################
8 |
9 | # Attempt to set APP_HOME
10 | # Resolve links: $0 may be a link
11 | PRG="$0"
12 | # Need this for relative symlinks.
13 | while [ -h "$PRG" ] ; do
14 | ls=`ls -ld "$PRG"`
15 | link=`expr "$ls" : '.*-> \(.*\)$'`
16 | if expr "$link" : '/.*' > /dev/null; then
17 | PRG="$link"
18 | else
19 | PRG=`dirname "$PRG"`"/$link"
20 | fi
21 | done
22 | SAVED="`pwd`"
23 | cd "`dirname \"$PRG\"`/" >/dev/null
24 | APP_HOME="`pwd -P`"
25 | cd "$SAVED" >/dev/null
26 |
27 | APP_NAME="Gradle"
28 | APP_BASE_NAME=`basename "$0"`
29 |
30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
31 | DEFAULT_JVM_OPTS=""
32 |
33 | # Use the maximum available, or set MAX_FD != -1 to use that value.
34 | MAX_FD="maximum"
35 |
36 | warn () {
37 | echo "$*"
38 | }
39 |
40 | die () {
41 | echo
42 | echo "$*"
43 | echo
44 | exit 1
45 | }
46 |
47 | # OS specific support (must be 'true' or 'false').
48 | cygwin=false
49 | msys=false
50 | darwin=false
51 | nonstop=false
52 | case "`uname`" in
53 | CYGWIN* )
54 | cygwin=true
55 | ;;
56 | Darwin* )
57 | darwin=true
58 | ;;
59 | MINGW* )
60 | msys=true
61 | ;;
62 | NONSTOP* )
63 | nonstop=true
64 | ;;
65 | esac
66 |
67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
68 |
69 | # Determine the Java command to use to start the JVM.
70 | if [ -n "$JAVA_HOME" ] ; then
71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
72 | # IBM's JDK on AIX uses strange locations for the executables
73 | JAVACMD="$JAVA_HOME/jre/sh/java"
74 | else
75 | JAVACMD="$JAVA_HOME/bin/java"
76 | fi
77 | if [ ! -x "$JAVACMD" ] ; then
78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
79 |
80 | Please set the JAVA_HOME variable in your environment to match the
81 | location of your Java installation."
82 | fi
83 | else
84 | JAVACMD="java"
85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
86 |
87 | Please set the JAVA_HOME variable in your environment to match the
88 | location of your Java installation."
89 | fi
90 |
91 | # Increase the maximum file descriptors if we can.
92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
93 | MAX_FD_LIMIT=`ulimit -H -n`
94 | if [ $? -eq 0 ] ; then
95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
96 | MAX_FD="$MAX_FD_LIMIT"
97 | fi
98 | ulimit -n $MAX_FD
99 | if [ $? -ne 0 ] ; then
100 | warn "Could not set maximum file descriptor limit: $MAX_FD"
101 | fi
102 | else
103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT"
104 | fi
105 | fi
106 |
107 | # For Darwin, add options to specify how the application appears in the dock
108 | if $darwin; then
109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\""
110 | fi
111 |
112 | # For Cygwin, switch paths to Windows format before running java
113 | if $cygwin ; then
114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"`
115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
116 | JAVACMD=`cygpath --unix "$JAVACMD"`
117 |
118 | # We build the pattern for arguments to be converted via cygpath
119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
120 | SEP=""
121 | for dir in $ROOTDIRSRAW ; do
122 | ROOTDIRS="$ROOTDIRS$SEP$dir"
123 | SEP="|"
124 | done
125 | OURCYGPATTERN="(^($ROOTDIRS))"
126 | # Add a user-defined pattern to the cygpath arguments
127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then
128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)"
129 | fi
130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh
131 | i=0
132 | for arg in "$@" ; do
133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -`
134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option
135 |
136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition
137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"`
138 | else
139 | eval `echo args$i`="\"$arg\""
140 | fi
141 | i=$((i+1))
142 | done
143 | case $i in
144 | (0) set -- ;;
145 | (1) set -- "$args0" ;;
146 | (2) set -- "$args0" "$args1" ;;
147 | (3) set -- "$args0" "$args1" "$args2" ;;
148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;;
149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;;
150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;;
151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;;
152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;;
153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;;
154 | esac
155 | fi
156 |
157 | # Escape application args
158 | save () {
159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done
160 | echo " "
161 | }
162 | APP_ARGS=$(save "$@")
163 |
164 | # Collect all arguments for the java command, following the shell quoting and substitution rules
165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS"
166 |
167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong
168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then
169 | cd "$(dirname "$0")"
170 | fi
171 |
172 | exec "$JAVACMD" "$@"
173 |
--------------------------------------------------------------------------------
/.idea/misc.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
33 |
34 |
35 |
36 |
37 |
38 |
39 |
40 |
41 |
42 |
43 |
44 |
45 |
46 |
47 |
48 |
49 |
50 |
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
60 |
61 |
62 |
63 |
64 |
65 |
66 |
67 |
68 |
69 |
70 |
71 |
72 |
73 |
74 |
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 |
84 |
85 |
86 |
87 |
88 |
89 |
90 |
91 |
92 |
93 |
94 |
95 |
96 |
97 |
98 |
99 |
100 |
101 |
102 |
103 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
11 |
16 |
21 |
26 |
31 |
36 |
41 |
46 |
51 |
56 |
61 |
66 |
71 |
76 |
81 |
86 |
91 |
96 |
101 |
106 |
111 |
116 |
121 |
126 |
131 |
136 |
141 |
146 |
151 |
156 |
161 |
166 |
171 |
172 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # SmallWindow
2 | > 需求描述
3 |
4 | 类似微信视频、语音时点击返回会形成一个App小窗口浮动在界面上,点击继续是通通话,如下图:
5 | 
6 |
7 | > 效果展示
8 |
9 | 
10 | > 技术分析
11 |
12 | 其实实现这个功能只需要你细心分析一下就有思路了:首先这个小窗口是浮动在app最上层的视图,其次所有触屏事件需先由该小窗口处理,还有就是小窗口的生命周期和Application也能虽可能不能同生,但是确是可以共死。所以可以在Application中创建一个view添加到WindowManage,这里将视图为view的window的type设置成系统级别的窗口,这样这个window可以在在全局呈现。另外,还需要让这个window可以随手指拖动而滑动,手指释放后会回弹到距离这个释放点最近的屏幕侧边,所以需要重写view 的OnTouch事件。
13 |
14 | > 代码细节实现
15 |
16 | - 创建全局Application,在Application创建的时候初始化一个view,以及一个WindowManager.LayoutParams,并设置get方法,方便外部调用:
17 |
18 |
19 | ```
20 | private SmallWindowView windowView;
21 | private WindowManager wm;
22 | private WindowManager.LayoutParams mLayoutParams;
23 | public SmallWindowView getWindowView() {
24 | return windowView;
25 | }
26 | public WindowManager getWm() {
27 | return wm;
28 | }
29 | public WindowManager.LayoutParams getmLayoutParams() {
30 | return mLayoutParams;
31 | }
32 | @Override
33 | public void onCreate() {
34 | super.onCreate();
35 | initSmallViewLayout();
36 | }
37 | public void initSmallViewLayout() {
38 | windowView = (SmallWindowView) LayoutInflater.from(this).inflate(R.layout.small_window, null);
39 | wm = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
40 | mLayoutParams = new WindowManager.LayoutParams(
41 | ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT,
42 | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
43 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
44 | | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
45 | PixelFormat.TRANSLUCENT);
46 | mLayoutParams.gravity = Gravity.NO_GRAVITY;
47 | //使用非CENTER时,可以通过设置XY的值来改变View的位置
48 | windowView.setWm(wm);
49 | windowView.setWmParams(mLayoutParams);
50 | }
51 | ```
52 | - 编写一个BaseActivity 实现可供子类来显示隐藏窗口的方法:
53 |
54 | ```
55 | private WindowManager wm;
56 | private SmallWindowView windowView;
57 | private WindowManager.LayoutParams mLayoutParams;
58 | private int OVERLAY_PERMISSION_REQ_CODE = 2;
59 | private boolean isRange = false;
60 |
61 | @Override
62 | protected void onCreate(Bundle savedInstanceState) {
63 | super.onCreate(savedInstanceState);
64 | wm = ((MyApplication)getApplication()).getWm();
65 | windowView = ((MyApplication)getApplication()).getWindowView();
66 | mLayoutParams = ((MyApplication)getApplication()).getmLayoutParams();
67 | }
68 |
69 |
70 | public void alertWindow() {
71 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { // 7.0 以上需要引导用去设置开启窗口浮动权限
72 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { // 8.0 以上type需要设置成这个
73 | mLayoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
74 | }
75 | requestDrawOverLays();
76 | } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // 6.0 动态申请
77 | ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.SYSTEM_ALERT_WINDOW}, 1);
78 | }
79 | }
80 |
81 | @Override
82 | public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
83 | super.onRequestPermissionsResult(requestCode, permissions, grantResults);
84 | if (requestCode == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
85 | if (wm != null && windowView.getWm() == null) {
86 | wm.addView(windowView, mLayoutParams);
87 | }
88 | } else {
89 | Toast.makeText(this, "权限申请失败", Toast.LENGTH_SHORT).show();
90 | }
91 | }
92 |
93 |
94 | private int[] location = new int[2]; // 小窗口位置坐标
95 |
96 | @Override
97 | public boolean onTouchEvent(MotionEvent event) {
98 | if (event.getAction() == MotionEvent.ACTION_DOWN) {
99 | isRange = calcPointRange(event);
100 | }
101 | if (isRange) {
102 | windowView.dispatchTouchEvent(event);
103 | }
104 | return super.onTouchEvent(event);
105 | }
106 |
107 | /**
108 | * 计算当前点击事件坐标是否在小窗口内
109 | * @param event
110 | * @return
111 | */
112 | private boolean calcPointRange(MotionEvent event) {
113 | windowView.getLocationOnScreen(location);
114 | int width = windowView.getMeasuredWidth();
115 | int height = windowView.getMeasuredHeight();
116 | float curX = event.getRawX();
117 | float curY = event.getRawY();
118 | if (curX >= location[0] && curX <= location[0] + width && curY >= location[1] && curY <= location[1] + height) {
119 | return true;
120 | }
121 | return false;
122 | }
123 |
124 | private static final String TAG = "BaseActivity";
125 |
126 | // android 23 以上先引导用户开启这个权限 该权限动态申请不了
127 | @TargetApi(Build.VERSION_CODES.M)
128 | public void requestDrawOverLays() {
129 | if (!Settings.canDrawOverlays(BaseActivity.this)) {
130 | Toast.makeText(this, "can not DrawOverlays", Toast.LENGTH_SHORT).show();
131 | Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + BaseActivity.this.getPackageName()));
132 | startActivityForResult(intent, OVERLAY_PERMISSION_REQ_CODE);
133 | } else {
134 | if (wm != null && windowView.getWindowId() == null) {
135 | wm.addView(windowView, mLayoutParams);
136 | }
137 | Toast.makeText(this, "权限已经授予", Toast.LENGTH_SHORT).show();
138 | }
139 | }
140 |
141 | @TargetApi(Build.VERSION_CODES.M)
142 | @Override
143 | protected void onActivityResult(int requestCode, int resultCode, Intent data) {
144 | if (requestCode == OVERLAY_PERMISSION_REQ_CODE) {
145 | if (!Settings.canDrawOverlays(this)) {
146 | Toast.makeText(this, "设置权限拒绝", Toast.LENGTH_SHORT).show();
147 | } else {
148 | Toast.makeText(this, "设置权限成功", Toast.LENGTH_SHORT).show();
149 | }
150 | }
151 | }
152 | // 移除window
153 | public void dismissWindow() {
154 | if (wm != null && windowView != null && windowView.getWindowId() != null) {
155 | wm.removeView(windowView);
156 | }
157 | }
158 | ```
159 | - 自定义一个处理Window内滑动事件的ViewGroup
160 |
161 | ```
162 | /**
163 | * Author : luweicheng on 2018/8/24 11:22
164 | * E-mail :1769005961@qq.com
165 | * GitHub : https://github.com/luweicheng24
166 | * function:
167 | **/
168 |
169 | public class SmallWindowView extends LinearLayout {
170 | private final int screenHeight;
171 | private final int screenWidth;
172 | private int statusHeight;
173 | private float mTouchStartX;
174 | private float mTouchStartY;
175 | private float x;
176 | private float y;
177 |
178 | private WindowManager wm;
179 | public WindowManager.LayoutParams wmParams;
180 |
181 |
182 | public SmallWindowView(Context context) {
183 | this(context, null);
184 | }
185 |
186 | public WindowManager getWm() {
187 | return wm;
188 | }
189 |
190 | public void setWm(WindowManager wm) {
191 | this.wm = wm;
192 | }
193 |
194 | public WindowManager.LayoutParams getWmParams() {
195 | return wmParams;
196 | }
197 |
198 | public void setWmParams(WindowManager.LayoutParams wmParams) {
199 | this.wmParams = wmParams;
200 | this.wmParams.x = screenWidth; // 窗口先贴附在右边
201 | }
202 |
203 | public SmallWindowView(Context context, @Nullable AttributeSet attrs) {
204 | this(context, attrs, 0);
205 | }
206 |
207 | public SmallWindowView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
208 | super(context, attrs, defStyleAttr);
209 | statusHeight = getStatusHeight(context);
210 | DisplayMetrics dm = getResources().getDisplayMetrics();
211 | screenHeight = dm.heightPixels;
212 | screenWidth = dm.widthPixels;
213 |
214 | }
215 |
216 |
217 | /**
218 | * 获得状态栏的高度
219 | *
220 | * @param context
221 | * @return
222 | */
223 | public static int getStatusHeight(Context context) {
224 | int statusHeight = -1;
225 | try {
226 | Class clazz = Class.forName("com.android.internal.R$dimen");
227 | Object object = clazz.newInstance();
228 | int height = Integer.parseInt(clazz.getField("status_bar_height")
229 | .get(object).toString());
230 | statusHeight = context.getResources().getDimensionPixelSize(height);
231 | } catch (Exception e) {
232 | e.printStackTrace();
233 | }
234 | return statusHeight;
235 | }
236 |
237 | boolean isRight = true;
238 |
239 | @Override
240 | public boolean onTouchEvent(MotionEvent event) {
241 | x = event.getRawX(); // 触摸点相对屏幕的x坐标
242 | y = event.getRawY() - statusHeight; // 触摸点相对于屏幕的y坐标
243 | switch (event.getAction()) {
244 | case MotionEvent.ACTION_DOWN:
245 | if (wmParams.x > 0) {
246 | isRight = true;
247 | }
248 | if (wmParams.x < 0) {
249 | isRight = false;
250 | }
251 | mTouchStartX = event.getX();// 触摸点在View内的相对x坐标
252 | mTouchStartY = event.getY();// 触摸点在View内的相对Y坐标
253 | Log.i("startP", "startX" + mTouchStartX + "====startY" + mTouchStartY);
254 | break;
255 |
256 | case MotionEvent.ACTION_MOVE:
257 | updateViewPosition(); // 跟新window布局参数
258 | break;
259 | case MotionEvent.ACTION_UP:
260 | if (wmParams.x <= 0) { //窗口贴附在左边
261 | wmParams.x = Math.abs(wmParams.x) <= screenWidth / 2 ? -screenWidth : screenWidth;
262 | } else { // 窗口贴附在右边
263 | wmParams.x = wmParams.x <= screenWidth / 2 ? screenWidth : -screenWidth;
264 | }
265 |
266 | // wmParams.x = screenWidth;
267 | wmParams.y = (int) (y - screenHeight / 2);// 跟新y坐标
268 | wm.updateViewLayout(this, wmParams);
269 | break;
270 | }
271 | return true;
272 | }
273 | private void updateViewPosition() {
274 | wmParams.gravity = Gravity.NO_GRAVITY;
275 | //更新浮动窗口位置参数
276 | int dx = (int) (mTouchStartX - x);
277 | int dy = (int) (y-screenHeight / 2);
278 | if (isRight) {
279 | wmParams.x = screenWidth / 2 - dx;
280 | } else {
281 | wmParams.x = -dx - screenWidth / 2;
282 | }
283 | wmParams.y = dy;
284 | Log.i("winParams", "x : " + wmParams.x + "y :" + wmParams.y + " dy :" + dy);
285 | wm.updateViewLayout(this, wmParams);
286 | //刷新显示
287 | }
288 | }
289 |
290 | ```
291 | 以上就能实现一个应用内小窗口了,这里windowManager的布局参数有坑要踩:
292 |
293 | - 不设置Gravity属性,window的坐标是以屏幕左上角为(0,0)原点,而当第一次接受到触摸事件之后就会以默认原点更改为屏幕中心,[Github源码,给个小星星](https://github.com/luweicheng24/SmallWindow)
294 |
--------------------------------------------------------------------------------