├── .idea ├── .name ├── copyright │ └── profiles_settings.xml ├── dictionaries │ └── enniu.xml ├── encodings.xml ├── inspectionProfiles │ ├── profiles_settings.xml │ └── Project_Default.xml ├── runConfigurations.xml ├── modules.xml ├── compiler.xml ├── gradle.xml └── misc.xml ├── demo ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── bupt │ │ │ └── edison │ │ │ └── magicreddotdemo │ │ │ └── MainActivity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── bupt │ │ │ └── edison │ │ │ └── magicreddotdemo │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── bupt │ │ └── edison │ │ └── magicreddotdemo │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── magicreddot ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ └── attrs.xml │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── pop1.png │ │ │ │ ├── pop2.png │ │ │ │ ├── pop3.png │ │ │ │ ├── pop4.png │ │ │ │ └── pop5.png │ │ │ └── drawable │ │ │ │ └── dismiss_anim.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── bupt │ │ │ └── edison │ │ │ └── magicreddot │ │ │ ├── Utils.java │ │ │ ├── MathUtils.java │ │ │ └── MagicRedDotView.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── bupt │ │ │ └── edison │ │ │ └── magicreddot │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── bupt │ │ └── edison │ │ └── magicreddot │ │ └── ApplicationTest.java ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── screenshot ├── QQRedDotView.gif ├── QQRedDotView_common.png ├── QQRedDotView_qqdot.gif ├── QQRedDotView_solid.png └── QQRedDotView_updateMsgCount.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── gradlew.bat ├── README.md └── gradlew /.idea/.name: -------------------------------------------------------------------------------- 1 | QQRedDotDemo -------------------------------------------------------------------------------- /demo/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /magicreddot/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':demo', ':magicreddot' 2 | -------------------------------------------------------------------------------- /screenshot/QQRedDotView.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/screenshot/QQRedDotView.gif -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/dictionaries/enniu.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /demo/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MagicRedDotDemo 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /screenshot/QQRedDotView_common.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/screenshot/QQRedDotView_common.png -------------------------------------------------------------------------------- /screenshot/QQRedDotView_qqdot.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/screenshot/QQRedDotView_qqdot.gif -------------------------------------------------------------------------------- /screenshot/QQRedDotView_solid.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/screenshot/QQRedDotView_solid.png -------------------------------------------------------------------------------- /magicreddot/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MagicRedDot 3 | 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | -------------------------------------------------------------------------------- /screenshot/QQRedDotView_updateMsgCount.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/screenshot/QQRedDotView_updateMsgCount.gif -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/demo/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/demo/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/demo/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/demo/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /magicreddot/src/main/res/mipmap-xhdpi/pop1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/magicreddot/src/main/res/mipmap-xhdpi/pop1.png -------------------------------------------------------------------------------- /magicreddot/src/main/res/mipmap-xhdpi/pop2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/magicreddot/src/main/res/mipmap-xhdpi/pop2.png -------------------------------------------------------------------------------- /magicreddot/src/main/res/mipmap-xhdpi/pop3.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/magicreddot/src/main/res/mipmap-xhdpi/pop3.png -------------------------------------------------------------------------------- /magicreddot/src/main/res/mipmap-xhdpi/pop4.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/magicreddot/src/main/res/mipmap-xhdpi/pop4.png -------------------------------------------------------------------------------- /magicreddot/src/main/res/mipmap-xhdpi/pop5.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/magicreddot/src/main/res/mipmap-xhdpi/pop5.png -------------------------------------------------------------------------------- /demo/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kanglongba/MagicRedDot/HEAD/demo/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /demo/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /demo/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Jul 26 22:08:17 CST 2016 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-2.14.1-bin.zip 7 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 7 | -------------------------------------------------------------------------------- /magicreddot/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /demo/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /demo/src/test/java/com/bupt/edison/magicreddotdemo/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.bupt.edison.magicreddotdemo; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /magicreddot/src/test/java/com/bupt/edison/magicreddot/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.bupt.edison.magicreddot; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /demo/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /demo/src/androidTest/java/com/bupt/edison/magicreddotdemo/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.bupt.edison.magicreddotdemo; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /magicreddot/src/androidTest/java/com/bupt/edison/magicreddot/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.bupt.edison.magicreddot; 2 | 3 | import android.app.Application; 4 | import android.test.ApplicationTestCase; 5 | 6 | /** 7 | * Testing Fundamentals 8 | */ 9 | public class ApplicationTest extends ApplicationTestCase { 10 | public ApplicationTest() { 11 | super(Application.class); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /magicreddot/src/main/res/drawable/dismiss_anim.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 11 | 14 | 17 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /demo/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/enniu/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /magicreddot/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/enniu/Library/Android/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /demo/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "24.0.1" 6 | 7 | defaultConfig { 8 | applicationId "com.bupt.edison.magicreddotdemo" 9 | minSdkVersion 14 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | testCompile 'junit:junit:4.12' 25 | compile 'com.android.support:appcompat-v7:23.4.0' 26 | compile project(':magicreddot') 27 | 28 | compile 'com.jakewharton:butterknife:7.0.1' 29 | } 30 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | ## Project-wide Gradle settings. 2 | # 3 | # For more details on how to configure your build environment visit 4 | # http://www.gradle.org/docs/current/userguide/build_environment.html 5 | # 6 | # Specifies the JVM arguments used for the daemon process. 7 | # The setting is particularly useful for tweaking memory settings. 8 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 9 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 10 | # 11 | # When configured, Gradle will run in incubating parallel mode. 12 | # This option should only be used with decoupled projects. More details, visit 13 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 14 | # org.gradle.parallel=true 15 | #Tue Jul 26 16:41:35 CST 2016 16 | systemProp.http.proxyHost=127.0.0.1 17 | systemProp.http.proxyPort=8787 18 | -------------------------------------------------------------------------------- /demo/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 6 | 7 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 25 | 26 | -------------------------------------------------------------------------------- /magicreddot/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /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 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | Android 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 62 | 63 | 64 | 65 | 66 | 67 | 72 | 73 | 74 | 75 | 76 | 77 | -------------------------------------------------------------------------------- /magicreddot/src/main/java/com/bupt/edison/magicreddot/Utils.java: -------------------------------------------------------------------------------- 1 | package com.bupt.edison.magicreddot; 2 | 3 | import android.content.Context; 4 | import android.graphics.Paint; 5 | import android.graphics.Rect; 6 | import android.text.TextUtils; 7 | import android.util.TypedValue; 8 | import android.view.View; 9 | import android.view.Window; 10 | 11 | /** 12 | * Created by edison on 16/7/3. 13 | */ 14 | public class Utils { 15 | /** 16 | * dip转化为px 17 | * @param context 18 | * @param dipValue 19 | * @return 20 | */ 21 | public static int dp2px(Context context, int dipValue){ 22 | if (context == null) { 23 | return dipValue; 24 | } 25 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dipValue, context.getResources().getDisplayMetrics()); 26 | 27 | } 28 | 29 | /** 30 | * 将sp值转换为px值,保证文字大小不变 31 | * 32 | * @param context 33 | * @param spValue 34 | * @return 35 | */ 36 | public static int sp2px(Context context, float spValue) { 37 | if (context == null) { 38 | return (int)spValue; 39 | } 40 | return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spValue, context.getResources().getDisplayMetrics()); 41 | } 42 | 43 | /** 44 | * 获取标题栏高度 45 | * @param window 46 | * @return 47 | */ 48 | public static int getTitleBarHeight(Window window){ 49 | int contentTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop(); 50 | return contentTop - getStatusBarHeight(window); 51 | } 52 | 53 | /** 54 | * 获取状态栏的高度 55 | * @param view 56 | * @return 57 | */ 58 | public static int getStatusBarHeight(View view) { 59 | Rect rect = new Rect(); 60 | view.getWindowVisibleDisplayFrame(rect); 61 | return rect.top; 62 | } 63 | 64 | /** 65 | * 获取状态栏的高度 66 | * @param window 67 | * @return 68 | */ 69 | public static int getStatusBarHeight(Window window){ 70 | Rect frame = new Rect(); 71 | window.getDecorView().getWindowVisibleDisplayFrame(frame); 72 | return frame.top; 73 | } 74 | 75 | /** 76 | * 计算字符串的绘制宽度 77 | * 78 | * @param paint 79 | * @param str 80 | * @return 81 | */ 82 | public static int computeStringWidth(Paint paint, String str) { 83 | int iRet = 0; 84 | if (!TextUtils.isEmpty(str)) { 85 | int len = str.length(); 86 | float[] widths = new float[len]; 87 | paint.getTextWidths(str, widths); 88 | for (int j = 0; j < len; j++) { 89 | iRet += (int) Math.ceil(widths[j]); 90 | } 91 | } 92 | // Log.d("edison","text width: "+iRet); 93 | return iRet; 94 | } 95 | 96 | /** 97 | * 计算字符串的绘制高度 98 | * 99 | * @param paint 100 | * @param string 101 | * @return 只有这种方法可以计算字符串的高度 102 | */ 103 | public static int computeStringHeight(Paint paint, String string) { 104 | Rect rect = new Rect(); 105 | 106 | //返回包围整个字符串的最小的一个Rect区域 107 | paint.getTextBounds(string, 0, 1, rect); 108 | // Log.d("edison","text height: "+rect.height()); 109 | return rect.height(); 110 | } 111 | } 112 | -------------------------------------------------------------------------------- /magicreddot/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | //应用插件 3 | apply plugin: 'com.github.dcendents.android-maven' 4 | apply plugin: 'com.jfrog.bintray' 5 | 6 | android { 7 | compileSdkVersion 23 8 | buildToolsVersion "24.0.1" 9 | 10 | defaultConfig { 11 | minSdkVersion 14 12 | targetSdkVersion 23 13 | versionCode 1 14 | versionName "1.0" 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | testCompile 'junit:junit:4.12' 27 | compile 'com.android.support:appcompat-v7:23.4.0' 28 | } 29 | 30 | //定义公共参数 31 | def siteUrl = 'https://github.com/kanglongba/MagicRedDot' // 项目的主页 32 | def gitUrl = 'https://github.com/kanglongba/MagicRedDot.git' // Git仓库的url 33 | 34 | //配置android-maven-gradle-plugin插件 35 | group = "com.bupt.edison.magicreddot" // groupId,一般由host+artifactId组成 36 | version = "1.0.0" //版本号,以后更新library时,它也要跟着增加 37 | 38 | //配置android-maven-gradle-plugin插件 39 | install { 40 | repositories.mavenInstaller { 41 | // This generates POM.xml with proper parameters 42 | pom { 43 | project { 44 | packaging 'aar' 45 | 46 | // Add your description here 47 | name 'Android Magic Dot' //项目的名称 48 | description 'a powerful red dot widget for android' //项目的描述 49 | url siteUrl 50 | 51 | // Set your license 52 | licenses { 53 | license { 54 | name 'The Apache Software License, Version 2.0' 55 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt' 56 | } 57 | } 58 | 59 | developers { 60 | developer { 61 | id 'kanglongba' //bintray的username 62 | name 'edison' //姓名 63 | email 'kanglongba@gmail.com' //邮箱 64 | } 65 | } 66 | 67 | scm { 68 | connection gitUrl 69 | developerConnection gitUrl 70 | url siteUrl 71 | } 72 | } 73 | } 74 | } 75 | } 76 | 77 | task sourcesJar(type: Jar) { 78 | from android.sourceSets.main.java.srcDirs 79 | classifier = 'sources' 80 | } 81 | 82 | task javadoc(type: Javadoc) { 83 | source = android.sourceSets.main.java.srcDirs 84 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator)) 85 | } 86 | 87 | task javadocJar(type: Jar, dependsOn: javadoc) { 88 | classifier = 'javadoc' 89 | from javadoc.destinationDir 90 | } 91 | 92 | artifacts { 93 | archives javadocJar 94 | archives sourcesJar 95 | } 96 | 97 | 98 | //配置gradle-bintray-plugin插件 99 | Properties properties = new Properties() 100 | properties.load(project.rootProject.file('local.properties').newDataInputStream()) 101 | 102 | //对gradle-bintray-plugin插件的配置,全部写在 bintray{ ... } 中 103 | bintray { 104 | user = properties.getProperty("bintray.user") 105 | key = properties.getProperty("bintray.apikey") 106 | 107 | configurations = ['archives'] 108 | 109 | pkg { 110 | repo = "maven" 111 | name = "magicreddot" //artifactId,会作为library的名称显示在JCenter中 112 | websiteUrl = siteUrl 113 | vcsUrl = gitUrl 114 | licenses = ["Apache-2.0"] 115 | publish = true 116 | 117 | version { 118 | desc = 'a powerful red dot widget for android' 119 | gpg { 120 | sign = true //Determines whether to GPG sign the files. The default is false 121 | passphrase = properties.getProperty("bintray.gpg.password") //Optional. The passphrase for GPG signing' 122 | } 123 | } 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /demo/src/main/java/com/bupt/edison/magicreddotdemo/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.bupt.edison.magicreddotdemo; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.util.Log; 6 | import android.view.View; 7 | import android.widget.Button; 8 | import android.widget.TextView; 9 | import android.widget.Toast; 10 | 11 | import com.bupt.edison.magicreddot.MagicRedDotView; 12 | import com.bupt.edison.magicreddot.Utils; 13 | 14 | import butterknife.Bind; 15 | import butterknife.ButterKnife; 16 | 17 | public class MainActivity extends AppCompatActivity { 18 | 19 | @Bind(R.id.qqdot_0) 20 | MagicRedDotView qqdot0; 21 | @Bind(R.id.qqdot_1) 22 | MagicRedDotView qqdot1; 23 | @Bind(R.id.qqdot_2) 24 | MagicRedDotView qqdot2; 25 | @Bind(R.id.qqdot_3) 26 | MagicRedDotView qqdot3; 27 | @Bind(R.id.qqdot_4) 28 | MagicRedDotView qqdot4; 29 | @Bind(R.id.btn_updateMsg) 30 | Button btnUpdateMsg; 31 | @Bind(R.id.qqdot_5) 32 | MagicRedDotView qqdot5; 33 | @Bind(R.id.qqdot_6) 34 | MagicRedDotView qqdot6; 35 | @Bind(R.id.textMsgCount) 36 | TextView textMsgCount; 37 | 38 | @Override 39 | protected void onCreate(Bundle savedInstanceState) { 40 | super.onCreate(savedInstanceState); 41 | setContentView(R.layout.activity_main); 42 | ButterKnife.bind(this); 43 | 44 | setQQRedDotViewWithText(); 45 | setQQRedDotView(); 46 | } 47 | 48 | private void setQQRedDotViewWithText() { 49 | qqdot0.setUnreadCount(9); 50 | qqdot1.setUnreadCount(279); 51 | qqdot2.setUnreadCount(100); 52 | qqdot3.setUnreadCount(89); 53 | qqdot4.setUnreadCount(301); 54 | 55 | btnUpdateMsg.setOnClickListener(new View.OnClickListener() { 56 | @Override 57 | public void onClick(View v) { 58 | int f = (int) (Math.random() * 10); 59 | int msgCount = 1; 60 | switch (f % 4) { 61 | case 0: 62 | msgCount = (int) (Math.random() * 10); //个位数 63 | break; 64 | case 1: 65 | msgCount = (int) (Math.random() * 100); 66 | break; 67 | case 2: 68 | msgCount = (int) (Math.random() * 1000); 69 | break; 70 | case 3: 71 | msgCount = (int) (Math.random() * 1000); 72 | break; 73 | } 74 | Log.d("edison", "count " + msgCount); 75 | qqdot5.setUnreadCount(msgCount); 76 | textMsgCount.setText(String.valueOf(msgCount)); 77 | } 78 | }); 79 | } 80 | 81 | private void setQQRedDotView() { 82 | qqdot6.setUnreadCount(666); 83 | qqdot6.setOnDragStartListener(new MagicRedDotView.OnDragStartListener() { 84 | @Override 85 | public void OnDragStart() { 86 | Toast.makeText(MainActivity.this, "开始拖拽", Toast.LENGTH_SHORT).show(); 87 | } 88 | }); 89 | qqdot6.setOnDotResetListener(new MagicRedDotView.OnDotResetListener() { 90 | @Override 91 | public void OnDotReset() { 92 | Toast.makeText(MainActivity.this, "红点复位", Toast.LENGTH_SHORT).show(); 93 | } 94 | }); 95 | qqdot6.setOnDotDismissListener(new MagicRedDotView.OnDotDismissListener() { 96 | @Override 97 | public void OnDotDismiss() { 98 | Toast.makeText(MainActivity.this, "红点消失", Toast.LENGTH_SHORT).show(); 99 | } 100 | }); 101 | } 102 | 103 | @Override 104 | protected void onResume() { 105 | super.onResume(); 106 | //这里还有个坑,状态栏和标题栏的高度计算结果都是0 107 | Log.d("edison titlebar", Utils.getTitleBarHeight(getWindow()) + ""); 108 | Log.d("edison statusbar", Utils.getStatusBarHeight(getWindow()) + ""); 109 | Log.d("edison statusbar", Utils.getStatusBarHeight(qqdot6) + ""); 110 | } 111 | 112 | @Override 113 | protected void onDestroy() { 114 | super.onDestroy(); 115 | ButterKnife.unbind(this); 116 | } 117 | 118 | @Override 119 | protected void onStart() { 120 | super.onStart(); 121 | } 122 | } 123 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android-MagicRedDot 2 | Android平台,一站式小红点解决方案,先看效果图: 3 | 4 | ![](https://github.com/kanglongba/MagicRedDot/blob/master/screenshot/QQRedDotView.gif) 5 | 6 | PS,原本只是想写一个仿新版QQ可拖拽小红点的控件,然后一路写下来,发现可以扩展成一站式小红点解决方案,于是就有了这个控件。 7 | 8 | 9 | # Introduction 10 | 11 | 控件可配置的属性有: 12 | 13 | * 红点的模式 14 | * 实心红点:不可拖动,不显示数字,最基本的红点形式,类似于一张小红点图片 15 | * 普通红点:显示数字,且大小可以随数字的大小而变化,但是不可拖动 16 | * QQ红点:显示数字,大小可随数字的大小而变化,可拖动,拖动过程中,会有橡皮筋效果,且可对拖拽过程设置监听 17 | * 红点的半径 18 | * 锚点(拖拽时固定在屏幕上的点)的半径 19 | * 锚点的半径不能大于红点的半径 20 | * 红点的颜色 21 | * 红点只是一个名字,它可以是任意颜色 22 | * 数字的颜色 23 | * 数字的大小 24 | * 数字的显示方式 25 | * 准确显示:如实显示数字 26 | * 模糊显示:当超过设置的阈值时,只显示一个范围,例如:99+,199+ 27 | * 阈值 28 | * 阈值不能小于99,否则会被强制设为99.而且,只有在模糊模式下,才有效果 29 | * 红点可拖动的距离 30 | * 在这个范围内,会有一条皮筋连接红点与锚点 31 | * 超过这个范围后,皮筋会断裂 32 | 33 | 34 | # Usage 35 | 36 | ### 添加依赖 37 | 38 | > compile 'com.bupt.edison.magicreddot:magicreddot:1.0.0' 39 | 40 | ### 在layout xml中使用 41 | 42 | * QQ红点 43 | 1. 在XML文件中设置属性 44 | 45 | ``` 46 | 58 | ``` 59 | 2. 在代码中更新数字和设置监听器 60 | 61 | ``` 62 | //更新数字 63 | qqdot6.setUnreadCount(666); 64 | //开始拖动的监听 65 | qqdot6.setOnDragStartListener(new MagicRedDotView.OnDragStartListener() { 66 | @Override 67 | public void OnDragStart() { 68 | Toast.makeText(MainActivity.this, "开始拖拽", Toast.LENGTH_SHORT).show(); 69 | } 70 | }); 71 | //复位的监听 72 | qqdot6.setOnDotResetListener(new MagicRedDotView.OnDotResetListener() { 73 | @Override 74 | public void OnDotReset() { 75 | Toast.makeText(MainActivity.this, "红点复位", Toast.LENGTH_SHORT).show(); 76 | } 77 | }); 78 | //消失的监听 79 | qqdot6.setOnDotDismissListener(new MagicRedDotView.OnDotDismissListener() { 80 | @Override 81 | public void OnDotDismiss() { 82 | Toast.makeText(MainActivity.this, "红点消失", Toast.LENGTH_SHORT).show(); 83 | } 84 | }); 85 | ``` 86 | 3. 效果图 87 | ![](https://github.com/kanglongba/MagicRedDot/blob/master/screenshot/QQRedDotView_qqdot.gif) 88 | 89 | * 普通红点 90 | 1. 在xml中设置属性 91 | 92 | ``` 93 | 105 | ``` 106 | 2. 在代码中更新数字 107 | 108 | ``` 109 | qqdot5.setUnreadCount(msgCount); 110 | ``` 111 | 3. 效果图 112 | ![](https://github.com/kanglongba/MagicRedDot/blob/master/screenshot/QQRedDotView_updateMsgCount.gif) 113 | 114 | * 实心红点 115 | 1. 在xml中设置属性 116 | 117 | ``` 118 | 125 | ``` 126 | 2. 不需要在代码中特别设置什么 127 | 3. 效果图 128 | ![](https://github.com/kanglongba/MagicRedDot/blob/master/screenshot/QQRedDotView_solid.png) 129 | 130 | 131 | # Reference 132 | 133 | * [QQ手机版 5.0“一键下班”设计小结](https://isux.tencent.com/qq-mobile-off-duty.html) 134 | * [贝塞尔曲线扫盲](http://www.html-js.com/article/1628) 135 | * [Path之贝塞尔曲线](https://github.com/GcsSloop/AndroidNote/blob/master/CustomView/Advance/%5B6%5DPath_Bezier.md) 136 | * [类似QQ的小红点](https://github.com/mabeijianxi/stickyDots) 137 | 138 | # License 139 | 140 | Copyright 2016 qianhailong 141 | 142 | Licensed under the Apache License, Version 2.0 (the "License"); 143 | you may not use this file except in compliance with the License. 144 | You may obtain a copy of the License at 145 | 146 | http://www.apache.org/licenses/LICENSE-2.0 147 | 148 | Unless required by applicable law or agreed to in writing, software 149 | distributed under the License is distributed on an "AS IS" BASIS, 150 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 151 | See the License for the specific language governing permissions and 152 | limitations under the License. 153 | 154 | 155 | 156 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 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 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 158 | function splitJvmOpts() { 159 | JVM_OPTS=("$@") 160 | } 161 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 162 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 163 | 164 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 165 | -------------------------------------------------------------------------------- /magicreddot/src/main/java/com/bupt/edison/magicreddot/MathUtils.java: -------------------------------------------------------------------------------- 1 | package com.bupt.edison.magicreddot; 2 | 3 | import android.graphics.PointF; 4 | 5 | /** 6 | * Created by edison on 16/7/3. 7 | */ 8 | public class MathUtils { 9 | 10 | /** 11 | * 返回两点之间的距离 12 | * @param x1 13 | * @param y1 14 | * @param x2 15 | * @param y2 16 | * @return 17 | */ 18 | public static float getDistanceBetweenPoints(float x1,float y1,float x2,float y2){ 19 | return (float)Math.sqrt(Math.pow(x2-x1,2)+Math.pow(y2-y1,2)); 20 | } 21 | 22 | /** 23 | * 返回两点的中点 24 | * @param x1 25 | * @param y1 26 | * @param x2 27 | * @param y2 28 | * @return 29 | * 把中点当成贝塞尔曲线的控制点 30 | */ 31 | public static PointF getMiddlePoint(float x1,float y1,float x2,float y2){ 32 | PointF pointF = new PointF((x1+x2)/2.0f,(y1+y2)/2.0f); 33 | return pointF; 34 | } 35 | 36 | /** 37 | * 获取两点之间的斜率 38 | * @param x1 39 | * @param y1 40 | * @param x2 41 | * @param y2 42 | * @return 43 | */ 44 | public static double getGradient(float x1,float y1,float x2,float y2){ 45 | float dy = Math.abs(y2-y1); 46 | float dx = Math.abs(x2-x1); 47 | double arcT = Math.atan(dy/dx); 48 | return arcT; 49 | } 50 | 51 | /** 52 | * 计算拖拽时,贝塞尔曲线的四个数据点 53 | * http://isux.tencent.com/wp-content/uploads/2014/10/201410171340142.png 54 | * center2是拖拽点,center1是固定点.一般radius2要大于radius1. 55 | * 56 | * @param centerX1 57 | * @param centerY1 58 | * @param radius1 59 | * @param centerX2 60 | * @param centerY2 61 | * @param radius2 62 | * @return 63 | */ 64 | public static PointF[] getTangentPoint(float centerX1,float centerY1,float radius1,float centerX2,float centerY2,float radius2){ 65 | PointF[] pointFs = new PointF[4]; 66 | PointF p1 = new PointF(); 67 | PointF p2 = new PointF(); 68 | PointF p3 = new PointF(); 69 | PointF p4 = new PointF(); 70 | 71 | double arcGradient = getGradient(centerX1,centerY1,centerX2,centerY2); //因为象限的不同,斜率是会变的.所以要分象限计算 72 | double arcP2C2C1 = Math.acos((radius2-radius1)/getDistanceBetweenPoints(centerX1,centerY1,centerX2,centerY2)); 73 | 74 | double arcP2C2X; 75 | if(centerX1>=centerX2&¢erY1>=centerY2){//三象限 76 | arcP2C2X = Math.PI - arcP2C2C1 - arcGradient; 77 | p2.set((float)(centerX2-radius2*Math.cos(arcP2C2X)),(float)(centerY2+radius2*Math.sin(arcP2C2X))); 78 | }else if(centerX1<=centerX2&¢erY1<=centerY2){//一象限 79 | arcP2C2X = Math.PI - arcP2C2C1 - arcGradient; 80 | p2.set((float)(centerX2+radius2*Math.cos(arcP2C2X)),(float)(centerY2-radius2*Math.sin(arcP2C2X))); 81 | }else if(centerX1centerY2){ //四象限 82 | arcP2C2X = arcP2C2C1-arcGradient; 83 | p2.set((float)(centerX2-radius2*Math.cos(arcP2C2X)),(float)(centerY2-radius2*Math.sin(arcP2C2X))); 84 | }else{ //二象限 85 | arcP2C2X = arcP2C2C1-arcGradient; 86 | p2.set((float)(centerX2+radius2*Math.cos(arcP2C2X)),(float)(centerY2+radius2*Math.sin(arcP2C2X))); 87 | } 88 | 89 | double arcP1C1C2 = Math.PI - arcP2C2C1; 90 | double arcP1C1X; 91 | if(centerX1>=centerX2&¢erY1>=centerY2){//三象限 92 | arcP1C1X = arcP1C1C2 - arcGradient; 93 | p1.set((float)(centerX1-radius1*Math.cos(arcP1C1X)),(float)(centerY1+radius1*Math.sin(arcP1C1X))); 94 | }else if(centerX1<=centerX2&¢erY1<=centerY2){ //一象限 95 | arcP1C1X = arcP1C1C2 - arcGradient; 96 | p1.set((float)(centerX1+radius1*Math.cos(arcP1C1X)),(float)(centerY1-radius1*Math.sin(arcP1C1X))); 97 | }else if(centerX1centerY2){ //四象限 98 | arcP1C1X = arcP2C2C1-arcGradient; 99 | p1.set((float)(centerX1-radius1*Math.cos(arcP1C1X)),(float)(centerY1-radius1*Math.sin(arcP1C1X))); 100 | }else{ //二象限 101 | arcP1C1X = arcP2C2C1-arcGradient; 102 | p1.set((float)(centerX1+radius1*Math.cos(arcP1C1X)),(float)(centerY1+radius1*Math.sin(arcP1C1X))); 103 | } 104 | 105 | //p3,p4与p1,p2是对称的 106 | double arcC1C2Y = Math.PI/2-arcGradient; 107 | double arcP4C2Y; 108 | if(centerX1>=centerX2&¢erY1>=centerY2){ //三象限 109 | arcP4C2Y = Math.PI/2 - arcP2C2C1 + arcGradient; 110 | p4.set((float)(centerX2+radius2*Math.sin(arcP4C2Y)),(float)(centerY2-radius2*Math.cos(arcP4C2Y))); 111 | }else if(centerX1<=centerX2&¢erY1<=centerY2){ //一象限 112 | arcP4C2Y = Math.PI/2 - arcP2C2C1 + arcGradient; 113 | p4.set((float)(centerX2-radius2*Math.sin(arcP4C2Y)),(float)(centerY2+radius2*Math.cos(arcP4C2Y))); 114 | }else if(centerX1centerY2){ //四象限 115 | arcP4C2Y = arcP2C2C1 - arcC1C2Y; 116 | p4.set((float)(centerX2+radius2*Math.sin(arcP4C2Y)),(float)(centerY2+radius2*Math.cos(arcP4C2Y))); 117 | }else{//二象限 118 | arcP4C2Y = arcP2C2C1 - arcC1C2Y; 119 | p4.set((float)(centerX2-radius2*Math.sin(arcP4C2Y)),(float)(centerY2-radius2*Math.cos(arcP4C2Y))); 120 | } 121 | 122 | double arcP3C1X; 123 | if(centerX1>=centerX2&¢erY1>=centerY2){//三象限 124 | arcP3C1X = Math.PI - arcP1C1C2 - arcGradient; 125 | p3.set((float)(centerX1+radius1*Math.cos(arcP3C1X)),(float)(centerY1-radius1*Math.sin(arcP3C1X))); 126 | }else if(centerX1<=centerX2&¢erY1<=centerY2){//一象限 127 | arcP3C1X = Math.PI - arcP1C1C2 - arcGradient; 128 | p3.set((float)(centerX1-radius1*Math.cos(arcP3C1X)),(float)(centerY1+radius1*Math.sin(arcP3C1X))); 129 | }else if(centerX1centerY2){ //四象限{ 130 | arcP3C1X = arcP1C1C2 - arcGradient; 131 | p3.set((float)(centerX1+radius1*Math.cos(arcP3C1X)),(float)(centerY1+radius1*Math.sin(arcP3C1X))); 132 | }else{//二象限 133 | arcP3C1X = arcP1C1C2 - arcGradient; 134 | p3.set((float)(centerX1-radius1*Math.cos(arcP3C1X)),(float)(centerY1-radius1*Math.sin(arcP3C1X))); 135 | } 136 | 137 | // Log.d("edison","斜率: "+arcGradient); 138 | // Log.d("edison","p1 arc: "+arcP1C1X); 139 | // Log.d("edison","p2 arc: "+arcP2C2X); 140 | // Log.d("edison","p3 arc: "+arcP3C1X); 141 | // Log.d("edison","p4 arc: "+arcP4C2Y); 142 | 143 | pointFs[0] = p1; 144 | pointFs[1] = p2; 145 | pointFs[2] = p4; 146 | pointFs[3] = p3; 147 | 148 | return pointFs; 149 | } 150 | } 151 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 78 | -------------------------------------------------------------------------------- /demo/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 14 | 15 | 21 | 27 | 28 | 37 | 38 | 50 | 51 | 62 | 63 | 74 | 75 | 86 | 87 | 88 | 89 | 96 | 102 | 103 | 109 | 110 | 117 | 118 | 125 | 126 | 133 | 134 | 142 | 143 | 144 | 151 | 159 | 160 | 173 | 174 | 175 | 182 | 188 | 189 |