├── app
├── .gitignore
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── values
│ │ │ │ ├── strings.xml
│ │ │ │ ├── colors.xml
│ │ │ │ └── themes.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
│ │ │ │ ├── activity_second.xml
│ │ │ │ ├── activity_third.xml
│ │ │ │ ├── layout_float_view.xml
│ │ │ │ └── activity_main.xml
│ │ │ ├── values-night
│ │ │ │ └── themes.xml
│ │ │ ├── drawable-v24
│ │ │ │ └── ic_launcher_foreground.xml
│ │ │ └── drawable
│ │ │ │ └── ic_launcher_background.xml
│ │ ├── java
│ │ │ └── com
│ │ │ │ └── zj
│ │ │ │ └── sample
│ │ │ │ ├── SecondActivity.kt
│ │ │ │ ├── ThirdActivity.kt
│ │ │ │ ├── widget
│ │ │ │ └── FloatingBall.java
│ │ │ │ └── MainActivity.kt
│ │ └── AndroidManifest.xml
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── zj
│ │ └── sample
│ │ └── ExampleInstrumentedTest.kt
├── proguard-rules.pro
└── build.gradle
├── easyfloat
├── .gitignore
├── consumer-rules.pro
├── src
│ ├── main
│ │ ├── res
│ │ │ ├── drawable
│ │ │ │ └── imuxuan.png
│ │ │ ├── values
│ │ │ │ └── ids.xml
│ │ │ └── layout
│ │ │ │ └── en_floating_view.xml
│ │ ├── AndroidManifest.xml
│ │ └── java
│ │ │ └── com
│ │ │ └── zj
│ │ │ └── easyfloat
│ │ │ ├── floatingview
│ │ │ ├── MagnetViewListener.java
│ │ │ ├── EnFloatingView.java
│ │ │ ├── utils
│ │ │ │ ├── EnContext.java
│ │ │ │ └── SystemUtils.java
│ │ │ ├── IFloatingView.java
│ │ │ ├── FloatingView.java
│ │ │ └── FloatingMagnetView.java
│ │ │ └── EasyFloat.kt
│ └── androidTest
│ │ └── java
│ │ └── com
│ │ └── zj
│ │ └── floatlibrary
│ │ └── ExampleInstrumentedTest.kt
├── proguard-rules.pro
└── build.gradle
├── settings.gradle
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── .gitignore
├── gradle.properties
├── gradlew.bat
├── README.md
├── publish-mavencentral.gradle
└── gradlew
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/easyfloat/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/easyfloat/consumer-rules.pro:
--------------------------------------------------------------------------------
1 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':easyfloat'
2 | include ':app'
3 | rootProject.name = "EasyFloat"
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | EasyFloat
3 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicardoJiang/EasyFloat/HEAD/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicardoJiang/EasyFloat/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicardoJiang/EasyFloat/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicardoJiang/EasyFloat/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/easyfloat/src/main/res/drawable/imuxuan.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicardoJiang/EasyFloat/HEAD/easyfloat/src/main/res/drawable/imuxuan.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicardoJiang/EasyFloat/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicardoJiang/EasyFloat/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicardoJiang/EasyFloat/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/RicardoJiang/EasyFloat/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/easyfloat/src/main/res/values/ids.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/RicardoJiang/EasyFloat/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/RicardoJiang/EasyFloat/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/RicardoJiang/EasyFloat/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/easyfloat/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Tue May 04 14:44:02 CST 2021
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-6.5-all.zip
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea/caches
5 | /.idea/libraries
6 | /.idea/modules.xml
7 | /.idea/workspace.xml
8 | /.idea/navEditor.xml
9 | /.idea/assetWizardSettings.xml
10 | .DS_Store
11 | /build
12 | /captures
13 | .externalNativeBuild
14 | .cxx
15 | local.properties
16 | .idea/*
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/easyfloat/src/main/java/com/zj/easyfloat/floatingview/MagnetViewListener.java:
--------------------------------------------------------------------------------
1 | package com.zj.easyfloat.floatingview;
2 |
3 | /**
4 | * Created by liyunpeng on 17/11/29.
5 | */
6 | public interface MagnetViewListener {
7 |
8 | void onRemove(FloatingMagnetView magnetView);
9 |
10 | void onClick(FloatingMagnetView magnetView);
11 | }
12 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zj/sample/SecondActivity.kt:
--------------------------------------------------------------------------------
1 | package com.zj.sample
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 |
6 | class SecondActivity : AppCompatActivity() {
7 | override fun onCreate(savedInstanceState: Bundle?) {
8 | super.onCreate(savedInstanceState)
9 | setContentView(R.layout.activity_second)
10 | }
11 | }
--------------------------------------------------------------------------------
/app/src/main/java/com/zj/sample/ThirdActivity.kt:
--------------------------------------------------------------------------------
1 | package com.zj.sample
2 |
3 | import androidx.appcompat.app.AppCompatActivity
4 | import android.os.Bundle
5 |
6 | class ThirdActivity : AppCompatActivity() {
7 | override fun onCreate(savedInstanceState: Bundle?) {
8 | super.onCreate(savedInstanceState)
9 | setContentView(R.layout.activity_third)
10 | }
11 | }
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #FFBB86FC
4 | #FF6200EE
5 | #FF3700B3
6 | #FF03DAC5
7 | #FF018786
8 | #FF000000
9 | #FFFFFFFF
10 |
--------------------------------------------------------------------------------
/easyfloat/src/main/res/layout/en_floating_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 |
6 |
11 |
12 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_second.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_third.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
--------------------------------------------------------------------------------
/app/src/androidTest/java/com/zj/sample/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.zj.sample
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.zj.easyfloat", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/easyfloat/src/androidTest/java/com/zj/floatlibrary/ExampleInstrumentedTest.kt:
--------------------------------------------------------------------------------
1 | package com.zj.floatlibrary
2 |
3 | import androidx.test.platform.app.InstrumentationRegistry
4 | import androidx.test.ext.junit.runners.AndroidJUnit4
5 |
6 | import org.junit.Test
7 | import org.junit.runner.RunWith
8 |
9 | import org.junit.Assert.*
10 |
11 | /**
12 | * Instrumented test, which will execute on an Android device.
13 | *
14 | * See [testing documentation](http://d.android.com/tools/testing).
15 | */
16 | @RunWith(AndroidJUnit4::class)
17 | class ExampleInstrumentedTest {
18 | @Test
19 | fun useAppContext() {
20 | // Context of the app under test.
21 | val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22 | assertEquals("com.zj.floatlibrary.test", appContext.packageName)
23 | }
24 | }
--------------------------------------------------------------------------------
/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
--------------------------------------------------------------------------------
/easyfloat/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
--------------------------------------------------------------------------------
/app/src/main/res/values/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/main/res/values-night/themes.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
16 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/layout_float_view.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
12 |
13 |
19 |
20 |
26 |
--------------------------------------------------------------------------------
/easyfloat/src/main/java/com/zj/easyfloat/floatingview/EnFloatingView.java:
--------------------------------------------------------------------------------
1 | package com.zj.easyfloat.floatingview;
2 |
3 | import android.content.Context;
4 | import android.widget.ImageView;
5 |
6 | import androidx.annotation.DrawableRes;
7 | import androidx.annotation.LayoutRes;
8 | import androidx.annotation.NonNull;
9 |
10 | import com.zj.easyfloat.R;
11 |
12 | /**
13 | * @ClassName EnFloatingView
14 | * @Description 悬浮窗
15 | * @Author Yunpeng Li
16 | * @Creation 2018/3/15 下午5:04
17 | * @Mender Yunpeng Li
18 | * @Modification 2018/3/15 下午5:04
19 | */
20 | public class EnFloatingView extends FloatingMagnetView {
21 |
22 | private final ImageView mIcon;
23 |
24 | public EnFloatingView(@NonNull Context context) {
25 | this(context, R.layout.en_floating_view);
26 | }
27 |
28 | public EnFloatingView(@NonNull Context context, @LayoutRes int resource) {
29 | super(context, null);
30 | inflate(context, resource, this);
31 | mIcon = findViewById(R.id.icon);
32 | }
33 |
34 | public void setIconImage(@DrawableRes int resId){
35 | mIcon.setImageResource(resId);
36 | }
37 |
38 | }
39 |
--------------------------------------------------------------------------------
/easyfloat/src/main/java/com/zj/easyfloat/floatingview/utils/EnContext.java:
--------------------------------------------------------------------------------
1 | package com.zj.easyfloat.floatingview.utils;
2 |
3 | import android.app.Application;
4 |
5 | /**
6 | * Created by Yunpeng Li on 2018/11/8.
7 | */
8 | public class EnContext {
9 |
10 | private static final Application INSTANCE;
11 |
12 | static {
13 | Application app = null;
14 | try {
15 | app = (Application) Class.forName("android.app.AppGlobals").getMethod("getInitialApplication").invoke(null);
16 | if (app == null)
17 | throw new IllegalStateException("Static initialization of Applications must be on main thread.");
18 | } catch (final Exception e) {
19 | e.printStackTrace();
20 | try {
21 | app = (Application) Class.forName("android.app.ActivityThread").getMethod("currentApplication").invoke(null);
22 | } catch (final Exception ex) {
23 | e.printStackTrace();
24 | }
25 | } finally {
26 | INSTANCE = app;
27 | }
28 | }
29 |
30 | public static Application get() {
31 | return INSTANCE;
32 | }
33 | }
34 |
--------------------------------------------------------------------------------
/easyfloat/src/main/java/com/zj/easyfloat/floatingview/IFloatingView.java:
--------------------------------------------------------------------------------
1 | package com.zj.easyfloat.floatingview;
2 |
3 | import android.app.Activity;
4 | import android.view.ViewGroup;
5 | import android.widget.FrameLayout;
6 |
7 | import androidx.annotation.DrawableRes;
8 | import androidx.annotation.LayoutRes;
9 |
10 |
11 | /**
12 | * Created by Yunpeng Li on 2018/3/15.
13 | */
14 |
15 | public interface IFloatingView {
16 |
17 | FloatingView remove();
18 |
19 | FloatingView add();
20 |
21 | FloatingView attach(Activity activity);
22 |
23 | FloatingView attach(FrameLayout container);
24 |
25 | FloatingView detach(Activity activity);
26 |
27 | FloatingView detach(FrameLayout container);
28 |
29 | FloatingMagnetView getView();
30 |
31 | FloatingView icon(@DrawableRes int resId);
32 |
33 | FloatingView customView(FloatingMagnetView viewGroup);
34 |
35 | FloatingView customView(@LayoutRes int resource);
36 |
37 | FloatingView layoutParams(ViewGroup.LayoutParams params);
38 |
39 | FloatingView listener(MagnetViewListener magnetViewListener);
40 |
41 | FloatingView dragEnable(boolean dragEnable);
42 |
43 | FloatingView setAutoMoveToEdge(boolean autoMoveToEdge);
44 |
45 | }
46 |
--------------------------------------------------------------------------------
/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=-Xmx2048m -Dfile.encoding=UTF-8
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 | # AndroidX package structure to make it clearer which packages are bundled with the
15 | # Android operating system, and which are packaged with your app"s APK
16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn
17 | android.useAndroidX=true
18 | # Automatically convert third-party libraries to use AndroidX
19 | android.enableJetifier=true
20 | # Kotlin code style for this project: "official" or "obsolete":
21 | kotlin.code.style=official
22 | useLocal=false
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
13 |
14 |
20 |
21 |
27 |
28 |
34 |
--------------------------------------------------------------------------------
/easyfloat/src/main/java/com/zj/easyfloat/floatingview/utils/SystemUtils.java:
--------------------------------------------------------------------------------
1 | package com.zj.easyfloat.floatingview.utils;
2 |
3 | import android.content.Context;
4 |
5 | /**
6 | * Created by Yunpeng Li on 2018/3/15.
7 | */
8 | public class SystemUtils {
9 |
10 | public static int getStatusBarHeight(Context context) {
11 | int result = 0;
12 | int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
13 | if (resourceId > 0) {
14 | result = context.getResources().getDimensionPixelSize(resourceId);
15 | }
16 | return result;
17 | }
18 |
19 | public static int getScreenWidth(Context context) {
20 | int screenWith = -1;
21 | try {
22 | screenWith = context.getResources().getDisplayMetrics().widthPixels;
23 | } catch (Exception e) {
24 | e.printStackTrace();
25 | }
26 | return screenWith;
27 | }
28 |
29 | public static int getScreenHeight(Context context) {
30 | int screenHeight = -1;
31 | try {
32 | screenHeight = context.getResources().getDisplayMetrics().heightPixels;
33 | } catch (Exception e) {
34 | e.printStackTrace();
35 | }
36 | return screenHeight;
37 | }
38 |
39 | }
40 |
--------------------------------------------------------------------------------
/easyfloat/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.library'
3 | id 'kotlin-android'
4 | }
5 |
6 | android {
7 | compileSdkVersion 30
8 | buildToolsVersion "30.0.3"
9 |
10 | defaultConfig {
11 | minSdkVersion 19
12 | targetSdkVersion 30
13 | versionCode 1
14 | versionName "1.0"
15 |
16 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17 | consumerProguardFiles "consumer-rules.pro"
18 | }
19 |
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 | compileOptions {
27 | sourceCompatibility JavaVersion.VERSION_1_8
28 | targetCompatibility JavaVersion.VERSION_1_8
29 | }
30 | kotlinOptions {
31 | jvmTarget = '1.8'
32 | }
33 | }
34 |
35 | dependencies {
36 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
37 | implementation 'androidx.core:core-ktx:1.3.2'
38 | }
39 |
40 | ext {
41 | PUBLISH_GROUP_ID = "io.github.shenzhen2017" //项目包名
42 | PUBLISH_ARTIFACT_ID = 'easyfloat' //项目名
43 | PUBLISH_VERSION = "1.0.2" //版本号
44 | }
45 | apply from: "${rootProject.projectDir}/publish-mavencentral.gradle"
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | plugins {
2 | id 'com.android.application'
3 | id 'kotlin-android'
4 | }
5 |
6 | android {
7 | compileSdkVersion 30
8 | buildToolsVersion "30.0.3"
9 |
10 | defaultConfig {
11 | applicationId "com.zj.sample"
12 | minSdkVersion 19
13 | targetSdkVersion 30
14 | versionCode 1
15 | versionName "1.0"
16 |
17 | testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
18 | }
19 |
20 | buildTypes {
21 | release {
22 | minifyEnabled false
23 | proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
24 | }
25 | }
26 | compileOptions {
27 | sourceCompatibility JavaVersion.VERSION_1_8
28 | targetCompatibility JavaVersion.VERSION_1_8
29 | }
30 | kotlinOptions {
31 | jvmTarget = '1.8'
32 | }
33 | }
34 |
35 | dependencies {
36 | implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
37 | implementation 'androidx.core:core-ktx:1.3.2'
38 | implementation 'androidx.appcompat:appcompat:1.2.0'
39 | implementation 'com.google.android.material:material:1.3.0'
40 | implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
41 | if (useLocal == "true") {
42 | implementation project(path: ':easyfloat')
43 | } else {
44 | implementation 'io.github.shenzhen2017:easyfloat:1.0.2'
45 | }
46 | }
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
8 |
9 |
15 |
18 |
21 |
22 |
23 |
24 |
30 |
--------------------------------------------------------------------------------
/app/src/main/java/com/zj/sample/widget/FloatingBall.java:
--------------------------------------------------------------------------------
1 | package com.zj.sample.widget;
2 |
3 | import android.content.Context;
4 | import android.graphics.Canvas;
5 | import android.graphics.Color;
6 | import android.graphics.Paint;
7 | import android.text.TextPaint;
8 | import android.util.AttributeSet;
9 | import android.view.View;
10 |
11 | import androidx.annotation.Nullable;
12 |
13 | public class FloatingBall extends View {
14 | Paint mPaint;
15 | TextPaint mTextPaint;
16 |
17 | public FloatingBall(Context context) {
18 | super(context);
19 | init();
20 | }
21 |
22 | public FloatingBall(Context context, @Nullable AttributeSet attrs) {
23 | super(context, attrs);
24 | init();
25 | }
26 |
27 | public FloatingBall(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
28 | super(context, attrs, defStyleAttr);
29 | init();
30 | }
31 |
32 | private void init() {
33 | Context context = getContext();
34 |
35 | mPaint = new Paint();
36 | mPaint.setColor(Color.parseColor("#ffe100"));
37 | mPaint.setStyle(Paint.Style.FILL);
38 |
39 | mTextPaint = new TextPaint();
40 | mTextPaint.setColor(Color.BLACK);
41 | mTextPaint.setTextAlign(Paint.Align.CENTER);
42 | mTextPaint.setTextSize(sp2px(context, 12));
43 | }
44 |
45 | @Override
46 | public void draw(Canvas canvas) {
47 | super.draw(canvas);
48 |
49 | int width = getWidth();
50 | int height = getHeight();
51 |
52 | canvas.drawCircle(width >> 1, height >> 1, width >> 1, mPaint);
53 |
54 | canvas.drawText("Tools", width >> 1, (float) (width * 0.60), mTextPaint);
55 | }
56 |
57 | private int sp2px(Context context, float sp) {
58 | final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
59 | return (int) (sp * fontScale + 0.5f);
60 | }
61 | }
--------------------------------------------------------------------------------
/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/com/zj/sample/MainActivity.kt:
--------------------------------------------------------------------------------
1 | package com.zj.sample
2 |
3 | import android.animation.ValueAnimator
4 | import android.content.Intent
5 | import android.os.Bundle
6 | import android.view.Gravity
7 | import android.view.View
8 | import android.widget.FrameLayout
9 | import android.widget.Toast
10 | import androidx.appcompat.app.AppCompatActivity
11 | import com.zj.easyfloat.EasyFloat
12 |
13 | class MainActivity : AppCompatActivity() {
14 | override fun onCreate(savedInstanceState: Bundle?) {
15 | super.onCreate(savedInstanceState)
16 | setContentView(R.layout.activity_main)
17 | }
18 |
19 | fun show(view: View) {
20 | EasyFloat
21 | .layout(R.layout.layout_float_view)
22 | .blackList(mutableListOf(ThirdActivity::class.java))
23 | .layoutParams(initLayoutParams())
24 | .dragEnable(true)
25 | .setAutoMoveToEdge(true)
26 | .listener {
27 | initListener(it)
28 | }
29 | .show(this)
30 | }
31 |
32 | fun dismiss(view: View) {
33 | EasyFloat.dismiss(this)
34 | }
35 |
36 | fun jumpOne(view: View) {
37 | val intent = Intent(this, SecondActivity::class.java)
38 | startActivity(intent)
39 | }
40 |
41 | fun jumpTwo(view: View) {
42 | val intent = Intent(this, ThirdActivity::class.java)
43 | startActivity(intent)
44 | }
45 |
46 | private fun initLayoutParams(): FrameLayout.LayoutParams {
47 | val params = FrameLayout.LayoutParams(
48 | FrameLayout.LayoutParams.WRAP_CONTENT,
49 | FrameLayout.LayoutParams.WRAP_CONTENT
50 | )
51 | params.gravity = Gravity.BOTTOM or Gravity.END
52 | params.setMargins(0, params.topMargin, params.rightMargin, 500)
53 | return params
54 | }
55 |
56 | private fun initListener(root: View?) {
57 | val rootView = root?.findViewById(R.id.ll_root)
58 | root?.findViewById(R.id.floating_ball)?.setOnClickListener {
59 | if (rootView != null) {
60 | if (rootView.getTag(R.id.animate_type) == null) {
61 | rootView.setTag(R.id.animate_type, true)
62 | }
63 | val isCollapse = rootView.getTag(R.id.animate_type) as Boolean
64 | animScale(rootView, isCollapse)
65 | rootView.setTag(R.id.animate_type, isCollapse.not())
66 | }
67 | }
68 | root?.findViewById(R.id.floating_ball_one)?.setOnClickListener {
69 | Toast.makeText(this, "click", Toast.LENGTH_SHORT).show()
70 | }
71 | }
72 |
73 | private fun animScale(view: View, isCollapse: Boolean) {
74 | val start = if (isCollapse) dip2px(172f) else dip2px(60f)
75 | val end = if (isCollapse) dip2px(60f) else dip2px(172f)
76 |
77 | val scaleBig = ValueAnimator.ofFloat(start, end)
78 | scaleBig.duration = 1000
79 | scaleBig.addUpdateListener {
80 | val layoutParams = view.layoutParams
81 | layoutParams.height = (it.animatedValue as Float).toInt()
82 | view.layoutParams = layoutParams
83 | }
84 | scaleBig.start()
85 | }
86 |
87 | private fun dip2px(dpValue: Float): Float {
88 | val scale: Float = getResources().getDisplayMetrics().density
89 | return (dpValue * scale + 0.5f)
90 | }
91 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## 前言
2 | 全局悬浮窗是项目中的一个常见需求,目前比较常见的实现是将要悬浮的`View`添加到`WindowManager`中
3 |
4 | 这种方案的主要痛点在于需要用户申请`TYPE_SYSTEM_ALERT`权限,并且需要用户去设置中手动打开,使用起来很不方便,同时需要申请权限可能会劝退用户.
5 |
6 | 针对这种情况下面介绍一种不需要权限的悬浮窗方案
7 |
8 | ## 效果图
9 | 首先看下最终的效果图
10 |
11 | 
12 |
13 | ## 特性
14 | - 1.不需要申请权限,可以直接打开悬浮窗,使用便捷
15 | - 2.支持自定义布局,自定义显示样式,自定义初始显示位置
16 | - 3.支持拖拽,可自动吸附到屏幕边缘
17 | - 4.可过滤不需要显示悬浮窗的黑名单界面
18 | - 5.支持自定义点击事件,可支持展开折叠等功能
19 | - 6.`API`链式调用,使用简洁优雅
20 | - 7.添加是否可拖动,是否自动靠边的配置和对应状态的获取
21 |
22 | ## 集成
23 | 第 1 步:在工程的 `build.gradle` 中添加:
24 | ```groovy
25 | allprojects {
26 | repositories {
27 | ...
28 | mavenCentral()
29 | }
30 | }
31 | ```
32 |
33 | 第2步:在应用的 `build.gradle` 中添加:
34 | ```groovy
35 | dependencies {
36 | implementation 'io.github.shenzhen2017:easyfloat:1.0.2'
37 | }
38 | ```
39 |
40 | ## 使用
41 | `API`链式调用,使用起来非常方便
42 |
43 | ### 1.初始化
44 | ```kotlin
45 | EasyFloat
46 | .layout(R.layout.layout_float_view)
47 | .blackList(mutableListOf(ThirdActivity::class.java))
48 | .layoutParams(initLayoutParams())
49 | .listener {
50 | initListener(it)
51 | }
52 | .show(this)
53 | ```
54 |
55 | 如上所示:
56 |
57 | 1.通过`layout`指定自定义布局
58 |
59 | 2.通过`blackList`指定不展示悬浮窗界面
60 |
61 | 3.通过`layoutParams`指定初始展示位置
62 |
63 | 4.通过`listener`处理自定义点击事件
64 |
65 | ### 2.销毁悬浮窗
66 | ```
67 | EasyFloat.dismiss(this)
68 | ```
69 | 直接调用`dismiss`销毁即可
70 |
71 | ## 主要原理
72 | 我们都知道,当我们需要设置布局的时候,是通过`setContentView`设置的
73 |
74 | 而`setContentView`实际上是将我们的布局添加到了`DecoreView`上,布局层级如下所示:
75 |
76 | 
77 |
78 | 1.`Activity` 类似于一个框架,负责容器生命周期及活动,窗口通过 `Window` 来管理;
79 |
80 | 2.`Window` 负责窗口管理(实际是子类 `PhoneWindow`),窗口的绘制和渲染交给 `DecorView`完成;
81 |
82 | 3.`DecorView` 是 `View` 树的根,开发人员为 `Activity` 定义的 `layout` 将成为 `DecorView` 的子视图 `ContentParent` 的子视图;
83 |
84 | 4.`layout.xml` 是开发人员定义的布局文件,最终 `inflate` 为 `DecorView` 的子组件;
85 |
86 | 由上我们可以想到一个方案:
87 | **我们在`Activity onStart`时,将要悬浮的`View`添加到`ContentParent`上就可以实现不需要权限的悬浮窗了**
88 |
89 | 当然我们还需要注意以下几点
90 |
91 | 1.因为我们需要在多个页面展示悬浮窗,可以通过`ActivityLifecycleCallbacks`监听所有`Activity`的生命周期,`onStart`时添加,`onStop`时移除
92 |
93 | 2.因为要在多个页面共享状态,所以应该有一个单例类管理`View`,做到只创建一个`View`,页面切换时只做添加与移除
94 |
95 | 3.因为要添加到`ContentParent`中,持有了`Activity`的引用,所以要注意处理内存泄漏的问题,在项目中我们使用了弱引用来防止内存泄漏
96 |
97 | 部分代码如下:
98 | ```kotlin
99 | object EasyFloat : Application.ActivityLifecycleCallbacks {
100 | override fun onActivityStarted(activity: Activity) {
101 | FloatingView.get().attach(it)
102 | }
103 |
104 | override fun onActivityStopped(activity: Activity) {
105 | FloatingView.get().detach(activity)
106 | }
107 |
108 | fun show(activity: Activity) {
109 | initShow(activity)
110 | activity.application.registerActivityLifecycleCallbacks(this)
111 | }
112 |
113 | fun dismiss(activity: Activity) {
114 | FloatingView.get().remove()
115 | FloatingView.get().detach(activity)
116 | activity.application.unregisterActivityLifecycleCallbacks(this)
117 | }
118 | }
119 | ```
120 |
121 | ## 总结
122 | ### 特别鸣谢
123 | 在实现这个开源框架的过程中,主要借鉴了[EnFloatingView](https://github.com/leotyndale/EnFloatingView)的一些思路
124 |
125 | 并在其基础上进行了一定的封装,优化了`API`调用并解决了滑动冲突等一些问题
126 |
127 | ### 项目地址
128 | [EasyFloat](https://github.com/shenzhen2017/EasyFloat)
129 |
130 | 开源不易,如果项目对你有所帮助,欢迎点赞,`Star`,收藏~
--------------------------------------------------------------------------------
/publish-mavencentral.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'maven-publish'
2 | apply plugin: 'signing'
3 |
4 | task androidSourcesJar(type: Jar) {
5 | classifier = 'sources'
6 | from android.sourceSets.main.java.source
7 | }
8 |
9 | ext["signing.keyId"] = ''
10 | ext["signing.password"] = ''
11 | ext["signing.secretKeyRingFile"] = ''
12 | ext["ossrhUsername"] = ''
13 | ext["ossrhPassword"] = ''
14 |
15 | File secretPropsFile = project.rootProject.file('local.properties')
16 | if (secretPropsFile.exists()) {
17 | println "Found secret props file, loading props"
18 | Properties p = new Properties()
19 | p.load(new FileInputStream(secretPropsFile))
20 | p.each { name, value ->
21 | ext[name] = value
22 | }
23 | } else {
24 | println "No props file, loading env vars"
25 | }
26 | publishing {
27 | publications {
28 | release(MavenPublication) {
29 | // The coordinates of the library, being set from variables that
30 | // we'll set up in a moment
31 | groupId PUBLISH_GROUP_ID
32 | artifactId PUBLISH_ARTIFACT_ID
33 | version PUBLISH_VERSION
34 |
35 | // Two artifacts, the `aar` and the sources
36 | artifact("$buildDir/outputs/aar/${project.getName()}-release.aar")
37 | artifact androidSourcesJar
38 |
39 | // Self-explanatory metadata for the most part
40 | pom {
41 | name = PUBLISH_ARTIFACT_ID
42 | description = 'Easy Float 项目'
43 | // If your project has a dedicated site, use its URL here
44 | url = 'Github地址'
45 | licenses {
46 | license {
47 | //协议类型,一般默认Apache License2.0的话不用改:
48 | name = 'The Apache License, Version 2.0'
49 | url = 'http://www.apache.org/licenses/LICENSE-2.0.txt'
50 | }
51 | }
52 | developers {
53 | developer {
54 | id = '用户ID'
55 | name = '用户名'
56 | email = '邮箱'
57 | }
58 | }
59 | // Version control info, if you're using GitHub, follow the format as seen here
60 | scm {
61 | //修改成你的Git地址:
62 | connection = 'scm:git:github.com/xxx/xxxx.git'
63 | developerConnection = 'scm:git:ssh://github.com/xxx/xxxx.git'
64 | //分支地址:
65 | url = 'https://github.com/xxx/xxxx/tree/master'
66 | }
67 | // A slightly hacky fix so that your POM will include any transitive dependencies
68 | // that your library builds upon
69 | withXml {
70 | def dependenciesNode = asNode().appendNode('dependencies')
71 |
72 | project.configurations.implementation.allDependencies.each {
73 | def dependencyNode = dependenciesNode.appendNode('dependency')
74 | dependencyNode.appendNode('groupId', it.group)
75 | dependencyNode.appendNode('artifactId', it.name)
76 | dependencyNode.appendNode('version', it.version)
77 | }
78 | }
79 | }
80 | }
81 | }
82 | repositories {
83 | // The repository to publish to, Sonatype/MavenCentral
84 | maven {
85 | // This is an arbitrary name, you may also use "mavencentral" or
86 | // any other name that's descriptive for you
87 | name = "easyfloat"
88 |
89 | def releasesRepoUrl = "https://s01.oss.sonatype.org/service/local/staging/deploy/maven2/"
90 | def snapshotsRepoUrl = "https://s01.oss.sonatype.org/content/repositories/snapshots/"
91 | // You only need this if you want to publish snapshots, otherwise just set the URL
92 | // to the release repo directly
93 | url = version.endsWith('SNAPSHOT') ? snapshotsRepoUrl : releasesRepoUrl
94 |
95 | // The username and password we've fetched earlier
96 | credentials {
97 | username ossrhUsername
98 | password ossrhPassword
99 | }
100 | }
101 | }
102 | }
103 | signing {
104 | sign publishing.publications
105 | }
--------------------------------------------------------------------------------
/easyfloat/src/main/java/com/zj/easyfloat/EasyFloat.kt:
--------------------------------------------------------------------------------
1 | package com.zj.easyfloat
2 |
3 | import android.app.Activity
4 | import android.app.Application
5 | import android.os.Bundle
6 | import android.view.Gravity
7 | import android.view.View
8 | import android.widget.FrameLayout
9 | import com.zj.easyfloat.floatingview.EnFloatingView
10 | import com.zj.easyfloat.floatingview.FloatingMagnetView
11 | import com.zj.easyfloat.floatingview.FloatingView
12 | import com.zj.easyfloat.floatingview.MagnetViewListener
13 |
14 | object EasyFloat : Application.ActivityLifecycleCallbacks {
15 | private var mLayoutParams = getFloatingLayoutParams()
16 | private val blackList = mutableListOf>()
17 | private var mLayout: Int = 0
18 |
19 | //编辑添加点击和移除事件, 拖动状态,靠边状态
20 | private var onRemoveListener: ((FloatingMagnetView) -> Unit)? = null
21 | private var onClickListener: ((FloatingMagnetView) -> Unit)? = null
22 | private var dragEnable = true
23 | private var autoMoveToEdge = true
24 |
25 | fun layout(layout: Int): EasyFloat {
26 | mLayout = layout
27 | return this
28 | }
29 |
30 | fun layoutParams(layoutParams: FrameLayout.LayoutParams): EasyFloat {
31 | mLayoutParams = layoutParams
32 | return this
33 | }
34 |
35 | fun blackList(blackList: MutableList>): EasyFloat {
36 | EasyFloat.blackList.addAll(blackList)
37 | return this
38 | }
39 |
40 | fun listener(
41 | onRemoveListener: ((View?) -> Unit)? = null,
42 | onClickListener: ((View) -> Unit)? = null
43 | ): EasyFloat {
44 | this.onRemoveListener = onRemoveListener
45 | this.onClickListener = onClickListener
46 | return this
47 | }
48 |
49 | /**
50 | * 是否可拖拽(位置是否固定)
51 | */
52 | fun dragEnable(dragEnable: Boolean): EasyFloat {
53 | this.dragEnable = dragEnable
54 | FloatingView.get().view?.updateDragState(dragEnable)
55 | return this
56 | }
57 |
58 | fun isDragEnable(): Boolean {
59 | return dragEnable
60 | }
61 |
62 | /**
63 | * 是否自动靠边
64 | */
65 | fun setAutoMoveToEdge(autoMoveToEdge: Boolean): EasyFloat {
66 | this.autoMoveToEdge = autoMoveToEdge
67 | FloatingView.get().view?.setAutoMoveToEdge(autoMoveToEdge)
68 | return this
69 | }
70 |
71 | fun isAutoMoveToEdge(): Boolean {
72 | return autoMoveToEdge
73 | }
74 |
75 | override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
76 |
77 | }
78 |
79 | override fun onActivityStarted(activity: Activity) {
80 | if (isActivityInValid(activity)) {
81 | return
82 | }
83 | initShow(activity)
84 | }
85 |
86 | override fun onActivityResumed(activity: Activity) {
87 | if (isActivityInValid(activity)) {
88 | return
89 | }
90 | }
91 |
92 | override fun onActivityPaused(activity: Activity) {
93 | }
94 |
95 | override fun onActivityStopped(activity: Activity) {
96 | if (isActivityInValid(activity)) {
97 | return
98 | }
99 | FloatingView.get().detach(activity)
100 | }
101 |
102 | override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
103 |
104 | }
105 |
106 | override fun onActivityDestroyed(activity: Activity) {
107 |
108 | }
109 |
110 | private fun isActivityInValid(activity: Activity): Boolean {
111 | return blackList.contains(activity::class.java)
112 | }
113 |
114 | private fun getFloatingLayoutParams(): FrameLayout.LayoutParams {
115 | val params = FrameLayout.LayoutParams(
116 | FrameLayout.LayoutParams.WRAP_CONTENT,
117 | FrameLayout.LayoutParams.WRAP_CONTENT
118 | )
119 | params.gravity = Gravity.BOTTOM or Gravity.START
120 | params.setMargins(0, params.topMargin, params.rightMargin, 500)
121 | return params
122 | }
123 |
124 | private fun initShow(activity: Activity) {
125 | activity.let {
126 | if (FloatingView.get().view == null) {
127 | FloatingView.get().customView(
128 | EnFloatingView(activity, mLayout)
129 | )
130 | }
131 | FloatingView.get().run {
132 | layoutParams(mLayoutParams)
133 | attach(it)
134 | dragEnable(dragEnable)
135 | this.listener(object : MagnetViewListener {
136 | override fun onRemove(magnetView: FloatingMagnetView) {
137 | onRemoveListener?.invoke(magnetView)
138 | }
139 | override fun onClick(magnetView: FloatingMagnetView) {
140 | onClickListener?.invoke(magnetView)
141 | }
142 | })
143 | }
144 | }
145 | }
146 |
147 | fun show(activity: Activity) {
148 | initShow(activity)
149 | activity.application.registerActivityLifecycleCallbacks(this)
150 | }
151 |
152 | fun dismiss(activity: Activity) {
153 | FloatingView.get().remove()
154 | FloatingView.get().detach(activity)
155 | activity.application.unregisterActivityLifecycleCallbacks(this)
156 | }
157 | }
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable/ic_launcher_background.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
10 |
15 |
20 |
25 |
30 |
35 |
40 |
45 |
50 |
55 |
60 |
65 |
70 |
75 |
80 |
85 |
90 |
95 |
100 |
105 |
110 |
115 |
120 |
125 |
130 |
135 |
140 |
145 |
150 |
155 |
160 |
165 |
170 |
171 |
--------------------------------------------------------------------------------
/easyfloat/src/main/java/com/zj/easyfloat/floatingview/FloatingView.java:
--------------------------------------------------------------------------------
1 | package com.zj.easyfloat.floatingview;
2 |
3 | import android.app.Activity;
4 | import android.os.Handler;
5 | import android.os.Looper;
6 | import android.view.Gravity;
7 | import android.view.View;
8 | import android.view.ViewGroup;
9 | import android.widget.FrameLayout;
10 | import android.widget.RelativeLayout;
11 |
12 | import androidx.annotation.DrawableRes;
13 | import androidx.annotation.LayoutRes;
14 | import androidx.core.view.ViewCompat;
15 | import com.zj.easyfloat.R;
16 | import com.zj.easyfloat.floatingview.utils.EnContext;
17 |
18 | import java.lang.ref.WeakReference;
19 |
20 |
21 | /**
22 | * @ClassName FloatingView
23 | * @Description 悬浮窗管理器
24 | * @Author Yunpeng Li
25 | * @Creation 2018/3/15 下午5:05
26 | * @Mender Yunpeng Li
27 | * @Modification 2018/3/15 下午5:05
28 | */
29 | public class FloatingView implements IFloatingView {
30 |
31 | private FloatingMagnetView mEnFloatingView;
32 | private static volatile FloatingView mInstance;
33 | private WeakReference mContainer;
34 | @LayoutRes
35 | private int mLayoutId = R.layout.en_floating_view;
36 | @DrawableRes
37 | private int mIconRes = R.drawable.imuxuan;
38 | private ViewGroup.LayoutParams mLayoutParams = getParams();
39 |
40 | private FloatingView() {
41 | }
42 |
43 | public static FloatingView get() {
44 | if (mInstance == null) {
45 | synchronized (FloatingView.class) {
46 | if (mInstance == null) {
47 | mInstance = new FloatingView();
48 | }
49 | }
50 | }
51 | return mInstance;
52 | }
53 |
54 | @Override
55 | public FloatingView remove() {
56 | new Handler(Looper.getMainLooper()).post(new Runnable() {
57 | @Override
58 | public void run() {
59 | if (mEnFloatingView == null) {
60 | return;
61 | }
62 | if (ViewCompat.isAttachedToWindow(mEnFloatingView) && getContainer() != null) {
63 | getContainer().removeView(mEnFloatingView);
64 | }
65 | mEnFloatingView = null;
66 | }
67 | });
68 | return this;
69 | }
70 |
71 | private void ensureFloatingView() {
72 | synchronized (this) {
73 | if (mEnFloatingView != null) {
74 | return;
75 | }
76 | EnFloatingView enFloatingView = new EnFloatingView(EnContext.get(), mLayoutId);
77 | mEnFloatingView = enFloatingView;
78 | enFloatingView.setLayoutParams(mLayoutParams);
79 | enFloatingView.setIconImage(mIconRes);
80 | addViewToWindow(enFloatingView);
81 | }
82 | }
83 |
84 | @Override
85 | public FloatingView add() {
86 | ensureFloatingView();
87 | return this;
88 | }
89 |
90 | @Override
91 | public FloatingView attach(Activity activity) {
92 | attach(getActivityRoot(activity));
93 | return this;
94 | }
95 |
96 | @Override
97 | public FloatingView attach(FrameLayout container) {
98 | if (container == null || mEnFloatingView == null) {
99 | mContainer = new WeakReference<>(container);
100 | return this;
101 | }
102 | if (mEnFloatingView.getParent() == container) {
103 | return this;
104 | }
105 | if (mEnFloatingView.getParent() != null) {
106 | ((ViewGroup) mEnFloatingView.getParent()).removeView(mEnFloatingView);
107 | }
108 | mContainer = new WeakReference<>(container);
109 | container.addView(mEnFloatingView);
110 | return this;
111 | }
112 |
113 | @Override
114 | public FloatingView detach(Activity activity) {
115 | detach(getActivityRoot(activity));
116 | return this;
117 | }
118 |
119 | @Override
120 | public FloatingView detach(FrameLayout container) {
121 | if (mEnFloatingView != null && container != null && ViewCompat.isAttachedToWindow(mEnFloatingView)) {
122 | container.removeView(mEnFloatingView);
123 | }
124 | if (getContainer() == container) {
125 | mContainer = null;
126 | }
127 | return this;
128 | }
129 |
130 | @Override
131 | public FloatingMagnetView getView() {
132 | return mEnFloatingView;
133 | }
134 |
135 | @Override
136 | public FloatingView icon(@DrawableRes int resId) {
137 | mIconRes = resId;
138 | return this;
139 | }
140 |
141 | @Override
142 | public FloatingView customView(FloatingMagnetView viewGroup) {
143 | mEnFloatingView = viewGroup;
144 | return this;
145 | }
146 |
147 | @Override
148 | public FloatingView customView(@LayoutRes int resource) {
149 | mLayoutId = resource;
150 | return this;
151 | }
152 |
153 | @Override
154 | public FloatingView layoutParams(ViewGroup.LayoutParams params) {
155 | mLayoutParams = params;
156 | if (mEnFloatingView != null) {
157 | mEnFloatingView.setLayoutParams(params);
158 | }
159 | return this;
160 | }
161 |
162 | @Override
163 | public FloatingView listener(MagnetViewListener magnetViewListener) {
164 | if (mEnFloatingView != null) {
165 | mEnFloatingView.setMagnetViewListener(magnetViewListener);
166 | }
167 | return this;
168 | }
169 |
170 | @Override
171 | public FloatingView dragEnable(boolean dragEnable) {
172 | if (mEnFloatingView != null) {
173 | mEnFloatingView.updateDragState(dragEnable);
174 | }
175 | return this;
176 | }
177 |
178 | @Override
179 | public FloatingView setAutoMoveToEdge(boolean autoMoveToEdge) {
180 | if (mEnFloatingView != null) {
181 | mEnFloatingView.setAutoMoveToEdge(autoMoveToEdge);
182 | }
183 | return this;
184 | }
185 |
186 | private void addViewToWindow(final View view) {
187 | if (getContainer() == null) {
188 | return;
189 | }
190 | getContainer().addView(view);
191 | }
192 |
193 | private FrameLayout getContainer() {
194 | if (mContainer == null) {
195 | return null;
196 | }
197 | return mContainer.get();
198 | }
199 |
200 | private FrameLayout.LayoutParams getParams() {
201 | FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
202 | RelativeLayout.LayoutParams.WRAP_CONTENT,
203 | RelativeLayout.LayoutParams.WRAP_CONTENT);
204 | params.gravity = Gravity.BOTTOM | Gravity.START;
205 | params.setMargins(13, params.topMargin, params.rightMargin, 500);
206 | return params;
207 | }
208 |
209 | private FrameLayout getActivityRoot(Activity activity) {
210 | if (activity == null) {
211 | return null;
212 | }
213 | try {
214 | return (FrameLayout) activity.getWindow().getDecorView().findViewById(android.R.id.content);
215 | } catch (Exception e) {
216 | e.printStackTrace();
217 | }
218 | return null;
219 | }
220 | }
--------------------------------------------------------------------------------
/easyfloat/src/main/java/com/zj/easyfloat/floatingview/FloatingMagnetView.java:
--------------------------------------------------------------------------------
1 | package com.zj.easyfloat.floatingview;
2 |
3 | import android.content.Context;
4 | import android.content.res.Configuration;
5 | import android.os.Handler;
6 | import android.os.Looper;
7 | import android.util.AttributeSet;
8 | import android.view.MotionEvent;
9 | import android.view.ViewConfiguration;
10 | import android.view.ViewGroup;
11 | import android.widget.FrameLayout;
12 |
13 | import com.zj.easyfloat.floatingview.utils.SystemUtils;
14 |
15 | /**
16 | * @ClassName FloatingMagnetView
17 | * @Description 磁力吸附悬浮窗
18 | * @Author Yunpeng Li
19 | * @Creation 2018/3/15 下午5:02
20 | * @Mender Yunpeng Li
21 | * @Modification 2018/3/15 下午5:02
22 | */
23 | public class FloatingMagnetView extends FrameLayout {
24 |
25 | public static final int MARGIN_EDGE = 13;
26 | private float mOriginalRawX;
27 | private float mOriginalRawY;
28 | private float mOriginalX;
29 | private float mOriginalY;
30 | private MagnetViewListener mMagnetViewListener;
31 | private static final int TOUCH_TIME_THRESHOLD = 150;
32 | private long mLastTouchDownTime;
33 | protected MoveAnimator mMoveAnimator;
34 | protected int mScreenWidth;
35 | private int mScreenHeight;
36 | private int mStatusBarHeight;
37 | private boolean isNearestLeft = true;
38 | private float mPortraitY;
39 | private boolean dragEnable = true;
40 | private boolean autoMoveToEdge = true;
41 |
42 | public void setMagnetViewListener(MagnetViewListener magnetViewListener) {
43 | this.mMagnetViewListener = magnetViewListener;
44 | }
45 |
46 | public FloatingMagnetView(Context context) {
47 | this(context, null);
48 | }
49 |
50 | public FloatingMagnetView(Context context, AttributeSet attrs) {
51 | this(context, attrs, 0);
52 | }
53 |
54 | public FloatingMagnetView(Context context, AttributeSet attrs, int defStyleAttr) {
55 | super(context, attrs, defStyleAttr);
56 | init();
57 | }
58 |
59 | private void init() {
60 | mMoveAnimator = new MoveAnimator();
61 | mStatusBarHeight = SystemUtils.getStatusBarHeight(getContext());
62 | setClickable(true);
63 | // updateSize();
64 | }
65 |
66 | /**
67 | * @param dragEnable 是否可拖动
68 | */
69 | public void updateDragState(boolean dragEnable) {
70 | this.dragEnable = dragEnable;
71 | }
72 |
73 | /**
74 | * @param autoMoveToEdge 是否自动到边缘
75 | */
76 | public void setAutoMoveToEdge(boolean autoMoveToEdge) {
77 | this.autoMoveToEdge = autoMoveToEdge;
78 | }
79 |
80 | @Override
81 | public boolean onTouchEvent(MotionEvent event) {
82 | if (event == null) {
83 | return false;
84 | }
85 | switch (event.getAction()) {
86 | case MotionEvent.ACTION_DOWN:
87 | break;
88 | case MotionEvent.ACTION_MOVE:
89 | updateViewPosition(event);
90 | break;
91 | case MotionEvent.ACTION_UP:
92 | clearPortraitY();
93 | if (autoMoveToEdge) {
94 | moveToEdge();
95 | }
96 | if (isOnClickEvent()) {
97 | dealClickEvent();
98 | }
99 | break;
100 | }
101 | return true;
102 | }
103 |
104 | protected void dealClickEvent() {
105 | if (mMagnetViewListener != null) {
106 | mMagnetViewListener.onClick(this);
107 | }
108 | }
109 |
110 | protected boolean isOnClickEvent() {
111 | return System.currentTimeMillis() - mLastTouchDownTime < TOUCH_TIME_THRESHOLD;
112 | }
113 |
114 | private void updateViewPosition(MotionEvent event) {
115 | //dragEnable
116 | if (!dragEnable) return;
117 | //占满width或height时不用变
118 | LayoutParams params = (LayoutParams) getLayoutParams();
119 | //限制不可超出屏幕宽度
120 | float desX = mOriginalX + event.getRawX() - mOriginalRawX;
121 | if (params.width == FrameLayout.LayoutParams.WRAP_CONTENT) {
122 | if (desX < 0) {
123 | desX = MARGIN_EDGE;
124 | }
125 | if (desX > mScreenWidth) {
126 | desX = mScreenWidth - MARGIN_EDGE;
127 | }
128 | setX(desX);
129 | }
130 | // 限制不可超出屏幕高度
131 | float desY = mOriginalY + event.getRawY() - mOriginalRawY;
132 | if (params.height == FrameLayout.LayoutParams.WRAP_CONTENT) {
133 | if (desY < mStatusBarHeight) {
134 | desY = mStatusBarHeight;
135 | }
136 | if (desY > mScreenHeight - getHeight()) {
137 | desY = mScreenHeight - getHeight();
138 | }
139 | setY(desY);
140 | }
141 | }
142 |
143 | private void changeOriginalTouchParams(MotionEvent event) {
144 | mOriginalX = getX();
145 | mOriginalY = getY();
146 | mOriginalRawX = event.getRawX();
147 | mOriginalRawY = event.getRawY();
148 | mLastTouchDownTime = System.currentTimeMillis();
149 | }
150 |
151 | protected void updateSize() {
152 | ViewGroup viewGroup = (ViewGroup) getParent();
153 | if (viewGroup != null) {
154 | mScreenWidth = viewGroup.getWidth() - getWidth();
155 | mScreenHeight = viewGroup.getHeight();
156 | }
157 | // mScreenWidth = (SystemUtils.getScreenWidth(getContext()) - this.getWidth());
158 | // mScreenHeight = SystemUtils.getScreenHeight(getContext());
159 | }
160 |
161 | public void moveToEdge() {
162 | //dragEnable
163 | if (!dragEnable) return;
164 | moveToEdge(isNearestLeft(), false);
165 | }
166 |
167 | public void moveToEdge(boolean isLeft, boolean isLandscape) {
168 | float moveDistance = isLeft ? MARGIN_EDGE : mScreenWidth - MARGIN_EDGE;
169 | float y = getY();
170 | if (!isLandscape && mPortraitY != 0) {
171 | y = mPortraitY;
172 | clearPortraitY();
173 | }
174 | mMoveAnimator.start(moveDistance, Math.min(Math.max(0, y), mScreenHeight - getHeight()));
175 | }
176 |
177 | private void clearPortraitY() {
178 | mPortraitY = 0;
179 | }
180 |
181 | protected boolean isNearestLeft() {
182 | int middle = mScreenWidth / 2;
183 | isNearestLeft = getX() < middle;
184 | return isNearestLeft;
185 | }
186 |
187 | public void onRemove() {
188 | if (mMagnetViewListener != null) {
189 | mMagnetViewListener.onRemove(this);
190 | }
191 | }
192 |
193 | protected class MoveAnimator implements Runnable {
194 |
195 | private Handler handler = new Handler(Looper.getMainLooper());
196 | private float destinationX;
197 | private float destinationY;
198 | private long startingTime;
199 |
200 | void start(float x, float y) {
201 | this.destinationX = x;
202 | this.destinationY = y;
203 | startingTime = System.currentTimeMillis();
204 | handler.post(this);
205 | }
206 |
207 | @Override
208 | public void run() {
209 | if (getRootView() == null || getRootView().getParent() == null) {
210 | return;
211 | }
212 | float progress = Math.min(1, (System.currentTimeMillis() - startingTime) / 400f);
213 | float deltaX = (destinationX - getX()) * progress;
214 | float deltaY = (destinationY - getY()) * progress;
215 | move(deltaX, deltaY);
216 | if (progress < 1) {
217 | handler.post(this);
218 | }
219 | }
220 |
221 | private void stop() {
222 | handler.removeCallbacks(this);
223 | }
224 | }
225 |
226 | private void move(float deltaX, float deltaY) {
227 | setX(getX() + deltaX);
228 | setY(getY() + deltaY);
229 | }
230 |
231 | @Override
232 | protected void onConfigurationChanged(Configuration newConfig) {
233 | super.onConfigurationChanged(newConfig);
234 | if (getParent() != null) {
235 | final boolean isLandscape = newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE;
236 | markPortraitY(isLandscape);
237 | ((ViewGroup) getParent()).post(new Runnable() {
238 | @Override
239 | public void run() {
240 | updateSize();
241 | moveToEdge(isNearestLeft, isLandscape);
242 | }
243 | });
244 | }
245 | }
246 |
247 | private void markPortraitY(boolean isLandscape) {
248 | if (isLandscape) {
249 | mPortraitY = getY();
250 | }
251 | }
252 |
253 | private float touchDownX;
254 |
255 | private void initTouchDown(MotionEvent ev) {
256 | changeOriginalTouchParams(ev);
257 | updateSize();
258 | mMoveAnimator.stop();
259 | }
260 |
261 | @Override
262 | public boolean onInterceptTouchEvent(MotionEvent ev) {
263 | boolean intercepted = false;
264 | switch (ev.getActionMasked()) {
265 | case MotionEvent.ACTION_DOWN:
266 | intercepted = false;
267 | touchDownX = ev.getX();
268 | initTouchDown(ev);
269 | break;
270 | case MotionEvent.ACTION_MOVE:
271 | intercepted = Math.abs(touchDownX - ev.getX()) >= ViewConfiguration.get(getContext()).getScaledTouchSlop();
272 | break;
273 | case MotionEvent.ACTION_UP:
274 | intercepted = false;
275 | break;
276 | }
277 | return intercepted;
278 | }
279 | }
280 |
--------------------------------------------------------------------------------