├── .gitignore
├── README.md
├── app
├── .gitignore
├── build.gradle
├── dragview.jks
├── proguard-rules.pro
├── src
│ └── main
│ │ ├── AndroidManifest.xml
│ │ ├── java
│ │ └── com
│ │ │ └── sonnyjack
│ │ │ └── drawview
│ │ │ └── MainActivity.java
│ │ └── res
│ │ ├── drawable-v24
│ │ └── ic_launcher_foreground.xml
│ │ ├── drawable
│ │ └── ic_launcher_background.xml
│ │ ├── layout
│ │ └── activity_main.xml
│ │ ├── mipmap-anydpi-v26
│ │ ├── ic_launcher.xml
│ │ └── ic_launcher_round.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
│ │ └── values
│ │ ├── colors.xml
│ │ ├── strings.xml
│ │ └── styles.xml
└── 密码.txt
├── build.gradle
├── device-2018-01-21-212602.mp4
├── device-2018-06-13-215330.png
├── device-2018-06-13-215423.png
├── device-2018-06-13-215444.png
├── gradle.properties
├── library
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── sonnyjack
│ │ └── widget
│ │ └── dragview
│ │ └── SonnyJackDragView.java
│ └── res
│ └── values
│ └── strings.xml
└── settings.gradle
/.gitignore:
--------------------------------------------------------------------------------
1 | *.iml
2 | .gradle
3 | /local.properties
4 | /.idea
5 | .DS_Store
6 | /build
7 | /gradle
8 | /captures
9 | .externalNativeBuild
10 | gradlew
11 | gradlew.bat
12 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | #使用方式
2 | implementation 'com.sonnyjack.widget:DragView:0.1.0'
3 |
4 | 或
5 |
6 | api 'com.sonnyjack.widget:DragView:0.1.0'
7 |
8 | 备注:由于之前的命名有误,DrawView改为DragView
9 |
10 | 用法如下:
11 |
12 | ImageView imageView = new ImageView(this);
13 | imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
14 | imageView.setImageResource(R.mipmap.ic_launcher_round);
15 | imageView.setOnClickListener(v -> Toast.makeText(MainActivity.this, "点击了...", Toast.LENGTH_SHORT).show());
16 |
17 | SonnyJackDragView sonnyJackDragView = new SonnyJackDragView.Builder()
18 | .setActivity(this)//当前Activity,不可为空
19 | .setDefaultLeft(30)//初始位置左边距
20 | .setDefaultTop(30)//初始位置上边距
21 | .setNeedNearEdge(false)//拖动停止后,是否移到边沿
22 | .setSize(100)//DragView大小
23 | .setView(imageView)//设置自定义的DragView,切记不可为空
24 | .build();
25 |
26 | 也可手动设置拖动停止后,是否移到边沿
27 |
28 | boolean needNearEdge = sonnyJackDragView.getNeedNearEdge();
29 | sonnyJackDragView.setNeedNearEdge(!needNearEdge);
30 |
31 | 效果如图:
32 |
33 |
34 |
39 |
40 | 如果遇到什么问题可以加我Q:252624617 或者issues反馈
41 |
42 | 不知道如何显示mp4,可自行下载device-2018-01-21-212602.mp4文件查看
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 28
5 | defaultConfig {
6 | applicationId "com.sonnyjack.drawview"
7 | minSdkVersion 14
8 | targetSdkVersion 28
9 | versionCode 1
10 | versionName "1.0"
11 | }
12 | //签名
13 | signingConfigs {
14 | release {
15 | storeFile file("dragview.jks")
16 | storePassword "123456"
17 | keyAlias "SonnyJack"
18 | keyPassword "123456"
19 | }
20 | debug {
21 | storeFile file("dragview.jks")
22 | storePassword "123456"
23 | keyAlias "SonnyJack"
24 | keyPassword "123456"
25 | }
26 | }
27 | buildTypes {
28 | debug {
29 | minifyEnabled false
30 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
31 | signingConfig signingConfigs.debug
32 | }
33 |
34 | release {
35 | minifyEnabled false
36 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
37 | signingConfig signingConfigs.release
38 | }
39 | }
40 | compileOptions {
41 | //支持java8特性
42 | sourceCompatibility JavaVersion.VERSION_1_8
43 | targetCompatibility JavaVersion.VERSION_1_8
44 | }
45 | }
46 |
47 | dependencies {
48 | implementation fileTree(include: ['*.jar'], dir: 'libs')
49 | implementation project(path: ':library')
50 | implementation 'com.android.support:appcompat-v7:28.0.0-alpha3'
51 | }
52 |
--------------------------------------------------------------------------------
/app/dragview.jks:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/app/dragview.jks
--------------------------------------------------------------------------------
/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/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
12 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
--------------------------------------------------------------------------------
/app/src/main/java/com/sonnyjack/drawview/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.sonnyjack.drawview;
2 |
3 | import android.support.v7.app.AppCompatActivity;
4 | import android.os.Bundle;
5 | import android.view.View;
6 | import android.widget.Button;
7 | import android.widget.ImageView;
8 | import android.widget.Toast;
9 |
10 | import com.sonnyjack.widget.dragview.SonnyJackDragView;
11 |
12 | public class MainActivity extends AppCompatActivity {
13 |
14 | private SonnyJackDragView mSonnyJackDragView;
15 | private Button mButton;
16 |
17 | @Override
18 | protected void onCreate(Bundle savedInstanceState) {
19 | super.onCreate(savedInstanceState);
20 | setContentView(R.layout.activity_main);
21 |
22 | mButton = findViewById(R.id.btn_move);
23 | mButton.setOnClickListener(v -> {
24 | boolean needNearEdge = mSonnyJackDragView.getNeedNearEdge();
25 | mSonnyJackDragView.setNeedNearEdge(!needNearEdge);
26 | if (needNearEdge) {
27 | mButton.setText("移至边沿");
28 | } else {
29 | mButton.setText("固定位置");
30 | }
31 | });
32 | ImageView imageView = new ImageView(this);
33 | imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
34 | imageView.setImageResource(R.mipmap.ic_launcher_round);
35 | imageView.setOnClickListener(v -> Toast.makeText(MainActivity.this, "点击了...", Toast.LENGTH_SHORT).show());
36 |
37 | mSonnyJackDragView = new SonnyJackDragView.Builder()
38 | .setActivity(this)
39 | .setDefaultLeft(30)
40 | .setDefaultTop(30)
41 | .setNeedNearEdge(false)
42 | .setSize(100)
43 | .setView(imageView)
44 | .build();
45 | }
46 | }
47 |
--------------------------------------------------------------------------------
/app/src/main/res/drawable-v24/ic_launcher_foreground.xml:
--------------------------------------------------------------------------------
1 |
7 |
12 |
13 |
19 |
22 |
25 |
26 |
27 |
28 |
34 |
35 |
--------------------------------------------------------------------------------
/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 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
8 |
9 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/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/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DragView
3 |
4 |
--------------------------------------------------------------------------------
/app/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
10 |
11 |
12 |
--------------------------------------------------------------------------------
/app/密码.txt:
--------------------------------------------------------------------------------
1 | 密码123456
2 | alins:SonnyJack
3 |
4 | MD5:
5 | SHA1:
6 | SHA256:
7 |
8 |
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | // Top-level build file where you can add configuration options common to all sub-projects/modules.
2 |
3 | buildscript {
4 |
5 | repositories {
6 | google()
7 | jcenter()
8 | }
9 | dependencies {
10 | classpath 'com.android.tools.build:gradle:3.1.1'
11 |
12 |
13 | // NOTE: Do not place your application dependencies here; they belong
14 | // in the individual module build.gradle files
15 |
16 | //https://bintray.com/novoda
17 | //https://github.com/novoda/bintray-release
18 | classpath 'com.novoda:bintray-release:0.8.0'
19 | }
20 | }
21 |
22 | allprojects {
23 | repositories {
24 | google()
25 | jcenter()
26 | }
27 |
28 | //加上这些
29 | tasks.withType(Javadoc) {
30 | options{ encoding "UTF-8"
31 | charSet 'UTF-8'
32 | links "http://docs.oracle.com/javase/7/docs/api"
33 | }
34 | }
35 | }
36 |
37 | task clean(type: Delete) {
38 | delete rootProject.buildDir
39 | }
40 |
--------------------------------------------------------------------------------
/device-2018-01-21-212602.mp4:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/device-2018-01-21-212602.mp4
--------------------------------------------------------------------------------
/device-2018-06-13-215330.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/device-2018-06-13-215330.png
--------------------------------------------------------------------------------
/device-2018-06-13-215423.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/device-2018-06-13-215423.png
--------------------------------------------------------------------------------
/device-2018-06-13-215444.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/linqssonny/DragView/34e62100893bbb0440bdffc3fe84c08e2db13153/device-2018-06-13-215444.png
--------------------------------------------------------------------------------
/gradle.properties:
--------------------------------------------------------------------------------
1 | # Project-wide Gradle settings.
2 |
3 | # IDE (e.g. Android Studio) users:
4 | # Gradle settings configured through the IDE *will override*
5 | # any settings specified in this file.
6 |
7 | # For more details on how to configure your build environment visit
8 | # http://www.gradle.org/docs/current/userguide/build_environment.html
9 |
10 | # Specifies the JVM arguments used for the daemon process.
11 | # The setting is particularly useful for tweaking memory settings.
12 | org.gradle.jvmargs=-Xmx1536m
13 |
14 | # When configured, Gradle will run in incubating parallel mode.
15 | # This option should only be used with decoupled projects. More details, visit
16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
17 | # org.gradle.parallel=true
18 |
--------------------------------------------------------------------------------
/library/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/library/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.novoda.bintray-release'//添加
3 |
4 | android {
5 | compileSdkVersion 28
6 |
7 |
8 |
9 | defaultConfig {
10 | minSdkVersion 14
11 | targetSdkVersion 28
12 | versionCode 2
13 | versionName "0.1.2"
14 | }
15 |
16 | buildTypes {
17 | release {
18 | minifyEnabled true
19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
20 | }
21 |
22 | debug {
23 | minifyEnabled false
24 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
25 | }
26 | }
27 |
28 | }
29 |
30 | dependencies {
31 | api fileTree(dir: 'libs', include: ['*.jar'])
32 | api 'com.android.support:appcompat-v7:28.0.0-alpha3'
33 | }
34 |
35 |
36 | task makeJar(type: proguard.gradle.ProGuardTask, dependsOn: "build") {
37 | delete 'build/outputs/aar/library-release.aar' //先删除输出文件夹中之前已经混淆好的旧jar
38 | injars 'build/intermediates-jars/release/classes.jar' //输入流的源文件地址
39 | outjars 'build/outputs/aar/library-release.aar' //输出流的文件地址
40 | configuration 'proguard-rules.pro' //混淆配置
41 | }
42 |
43 | //添加
44 | publish {
45 | userOrg = 'sonnyjack'//bintray.com用户名
46 | groupId = 'com.sonnyjack.widget'//jcenter上的路径
47 | artifactId = 'DragView'//项目名称
48 | publishVersion = '0.1.0'//版本号
49 | desc = '这是个可拖动的悬浮按钮'//描述,不重要
50 | website = 'https://github.com/linqssonny/DragView'//网站,不重要;尽量模拟github上的地址,例如我这样的;当然你有地址最好了
51 | }
52 |
--------------------------------------------------------------------------------
/library/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 |
23 | #-libraryjars '/Users/linqs/Library/Java/jdk1.7.0_67/jre/lib/rt.jar'
24 |
25 | #-libraryjars '/Users/linqs/Library/Android/sdk/platforms/android-26/android.jar'
26 |
27 | -optimizationpasses 5
28 | -dontusemixedcaseclassnames
29 | -dontskipnonpubliclibraryclasses
30 | -dontpreverify
31 | -verbose
32 | -optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
33 | -keepattributes SourceFile,LineNumberTable
34 |
35 | -dontwarn com.sonnyjack.widget.dragview.**
36 | -keep class com.sonnyjack.widget.dragview.SonnyJackDragView
37 | -keepclassmembers class com.sonnyjack.widget.dragview.SonnyJackDragView {
38 | public *;
39 | }
40 |
41 | -keep class com.sonnyjack.widget.dragview.SonnyJackDragView$* {
42 | public *;
43 | }
--------------------------------------------------------------------------------
/library/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
3 |
--------------------------------------------------------------------------------
/library/src/main/java/com/sonnyjack/widget/dragview/SonnyJackDragView.java:
--------------------------------------------------------------------------------
1 | package com.sonnyjack.widget.dragview;
2 |
3 | import android.animation.ValueAnimator;
4 | import android.app.Activity;
5 | import android.content.Context;
6 | import android.graphics.Rect;
7 | import android.os.Build;
8 | import android.util.DisplayMetrics;
9 | import android.view.MotionEvent;
10 | import android.view.View;
11 | import android.view.WindowManager;
12 | import android.view.animation.BounceInterpolator;
13 | import android.widget.FrameLayout;
14 |
15 | import java.lang.reflect.Field;
16 |
17 | /**
18 | * 可拖动的悬浮按钮
19 | * Created by linqs on 2017/12/21.
20 | */
21 |
22 | public class SonnyJackDragView implements View.OnTouchListener {
23 | private Builder mBuilder;
24 |
25 | private int mStatusBarHeight, mScreenWidth, mScreenHeight;
26 |
27 | //手指按下位置
28 | private int mStartX, mStartY, mLastX, mLastY;
29 | private boolean mTouchResult = false;
30 |
31 | private SonnyJackDragView(SonnyJackDragView.Builder builder) {
32 | mBuilder = builder;
33 | initDragView();
34 | }
35 |
36 | public View getDragView() {
37 | return mBuilder.view;
38 | }
39 |
40 | public Activity getActivity() {
41 | return mBuilder.activity;
42 | }
43 |
44 | public boolean getNeedNearEdge() {
45 | return mBuilder.needNearEdge;
46 | }
47 |
48 | public void setNeedNearEdge(boolean needNearEdge) {
49 | mBuilder.needNearEdge = needNearEdge;
50 | if (mBuilder.needNearEdge) {
51 | moveNearEdge();
52 | }
53 | }
54 |
55 | private void initDragView() {
56 | if (null == getActivity()) {
57 | throw new NullPointerException("the activity is null");
58 | }
59 | if (null == mBuilder.view) {
60 | throw new NullPointerException("the dragView is null");
61 | }
62 | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1 && mBuilder.activity.isDestroyed()) {
63 | return;
64 | }
65 |
66 | //屏幕宽高
67 | WindowManager windowManager = (WindowManager) getActivity().getSystemService(Context.WINDOW_SERVICE);
68 | if (null != windowManager) {
69 | DisplayMetrics displayMetrics = getActivity().getResources().getDisplayMetrics();
70 | mScreenWidth = displayMetrics.widthPixels;
71 | mScreenHeight = displayMetrics.heightPixels;
72 | }
73 |
74 | //状态栏高度
75 | Rect frame = new Rect();
76 | getActivity().getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
77 | mStatusBarHeight = frame.top;
78 | if (mStatusBarHeight <= 0) {
79 | try {
80 | Class> c = Class.forName("com.android.internal.R$dimen");
81 | Object obj = c.newInstance();
82 | Field field = c.getField("status_bar_height");
83 | int x = Integer.parseInt(field.get(obj).toString());
84 | mStatusBarHeight = getActivity().getResources().getDimensionPixelSize(x);
85 | } catch (Exception e1) {
86 | e1.printStackTrace();
87 | }
88 | }
89 |
90 | int left = mBuilder.needNearEdge ? 0 : mBuilder.defaultLeft;
91 | FrameLayout.LayoutParams layoutParams = createLayoutParams(left, mStatusBarHeight + mBuilder.defaultTop, 0, 0);
92 | FrameLayout rootLayout = (FrameLayout) getActivity().getWindow().getDecorView();
93 | rootLayout.addView(getDragView(), layoutParams);
94 | getDragView().setOnTouchListener(this);
95 | }
96 |
97 | private static SonnyJackDragView createDragView(SonnyJackDragView.Builder builder) {
98 | if (null == builder) {
99 | throw new NullPointerException("the param builder is null when execute method createDragView");
100 | }
101 | if (null == builder.activity) {
102 | throw new NullPointerException("the activity is null");
103 | }
104 | if (null == builder.view) {
105 | throw new NullPointerException("the view is null");
106 | }
107 | SonnyJackDragView sonnyJackDragView = new SonnyJackDragView(builder);
108 | return sonnyJackDragView;
109 | }
110 |
111 | @Override
112 | public boolean onTouch(View v, MotionEvent event) {
113 | switch (event.getAction()) {
114 | case MotionEvent.ACTION_DOWN:
115 | mTouchResult = false;
116 | mStartX = mLastX = (int) event.getRawX();
117 | mStartY = mLastY = (int) event.getRawY();
118 | break;
119 | case MotionEvent.ACTION_MOVE:
120 | int left, top, right, bottom;
121 | int dx = (int) event.getRawX() - mLastX;
122 | int dy = (int) event.getRawY() - mLastY;
123 | left = v.getLeft() + dx;
124 | if (left < 0) {
125 | left = 0;
126 | }
127 | right = left + v.getWidth();
128 | if (right > mScreenWidth) {
129 | right = mScreenWidth;
130 | left = right - v.getWidth();
131 | }
132 | top = v.getTop() + dy;
133 | if (top < mStatusBarHeight + 2) {
134 | top = mStatusBarHeight + 2;
135 | }
136 | bottom = top + v.getHeight();
137 | if (bottom > mScreenHeight) {
138 | bottom = mScreenHeight;
139 | top = bottom - v.getHeight();
140 | }
141 | v.layout(left, top, right, bottom);
142 | mLastX = (int) event.getRawX();
143 | mLastY = (int) event.getRawY();
144 | break;
145 | case MotionEvent.ACTION_UP:
146 | //这里需设置LayoutParams,不然按home后回再到页面等view会回到原来的地方
147 | v.setLayoutParams(createLayoutParams(v.getLeft(), v.getTop(), 0, 0));
148 | float endX = event.getRawX();
149 | float endY = event.getRawY();
150 | if (Math.abs(endX - mStartX) > 5 || Math.abs(endY - mStartY) > 5) {
151 | //防止点击的时候稍微有点移动点击事件被拦截了
152 | mTouchResult = true;
153 | }
154 | if (mTouchResult && mBuilder.needNearEdge) {
155 | //是否每次都移至屏幕边沿
156 | moveNearEdge();
157 | }
158 | break;
159 | }
160 | return mTouchResult;
161 | }
162 |
163 | /**
164 | * 移至最近的边沿
165 | */
166 | private void moveNearEdge() {
167 | int left = getDragView().getLeft();
168 | int lastX;
169 | if (left + getDragView().getWidth() / 2 <= mScreenWidth / 2) {
170 | lastX = 0;
171 | } else {
172 | lastX = mScreenWidth - getDragView().getWidth();
173 | }
174 | ValueAnimator valueAnimator = ValueAnimator.ofInt(left, lastX);
175 | valueAnimator.setDuration(1000);
176 | valueAnimator.setRepeatCount(0);
177 | valueAnimator.setInterpolator(new BounceInterpolator());
178 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
179 | @Override
180 | public void onAnimationUpdate(ValueAnimator animation) {
181 | int left = (int) animation.getAnimatedValue();
182 | getDragView().setLayoutParams(createLayoutParams(left, getDragView().getTop(), 0, 0));
183 | }
184 | });
185 | valueAnimator.start();
186 | }
187 |
188 | private FrameLayout.LayoutParams createLayoutParams(int left, int top, int right, int bottom) {
189 | FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(mBuilder.size, mBuilder.size);
190 | layoutParams.setMargins(left, top, right, bottom);
191 | return layoutParams;
192 | }
193 |
194 | public static class Builder {
195 | private Activity activity;
196 | private int size = FrameLayout.LayoutParams.WRAP_CONTENT;
197 | private int defaultTop = 0;
198 | private int defaultLeft = 0;
199 | private boolean needNearEdge = false;
200 | private View view;
201 |
202 | public Builder setActivity(Activity activity) {
203 | this.activity = activity;
204 | return this;
205 | }
206 |
207 | public Builder setSize(int size) {
208 | this.size = size;
209 | return this;
210 | }
211 |
212 | public Builder setDefaultTop(int top) {
213 | this.defaultTop = top;
214 | return this;
215 | }
216 |
217 | public Builder setDefaultLeft(int left) {
218 | this.defaultLeft = left;
219 | return this;
220 | }
221 |
222 | public Builder setNeedNearEdge(boolean needNearEdge) {
223 | this.needNearEdge = needNearEdge;
224 | return this;
225 | }
226 |
227 | public Builder setView(View view) {
228 | this.view = view;
229 | return this;
230 | }
231 |
232 | public SonnyJackDragView build() {
233 | return createDragView(this);
234 | }
235 | }
236 | }
237 |
--------------------------------------------------------------------------------
/library/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | DragView
3 |
4 |
--------------------------------------------------------------------------------
/settings.gradle:
--------------------------------------------------------------------------------
1 | include ':app', ':library'
2 |
--------------------------------------------------------------------------------