├── app ├── .gitignore ├── src │ ├── main │ │ ├── res │ │ │ ├── values │ │ │ │ ├── strings.xml │ │ │ │ ├── colors.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── styles.xml │ │ │ ├── mipmap-xhdpi │ │ │ │ ├── icon_gps.png │ │ │ │ ├── icon_home.png │ │ │ │ ├── icon_menu.png │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── icon_cancel.png │ │ │ │ ├── icon_notify.png │ │ │ │ ├── icon_search.png │ │ │ │ └── icon_setting.png │ │ │ ├── mipmap-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── mipmap-xxhdpi │ │ │ │ ├── icon_gps.png │ │ │ │ ├── icon_home.png │ │ │ │ ├── icon_menu.png │ │ │ │ ├── ic_launcher.png │ │ │ │ ├── icon_cancel.png │ │ │ │ ├── icon_notify.png │ │ │ │ ├── icon_search.png │ │ │ │ └── icon_setting.png │ │ │ ├── mipmap-xxxhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── com │ │ │ └── hitomi │ │ │ └── circlemenu │ │ │ └── MainActivity.java │ ├── test │ │ └── java │ │ │ └── com │ │ │ └── hitomi │ │ │ └── circlemenu │ │ │ └── ExampleUnitTest.java │ └── androidTest │ │ └── java │ │ └── com │ │ └── hitomi │ │ └── circlemenu │ │ └── ApplicationTest.java ├── build.gradle └── proguard-rules.pro ├── cmlibrary ├── .gitignore ├── src │ └── main │ │ ├── res │ │ └── values │ │ │ └── strings.xml │ │ ├── java │ │ └── com │ │ │ └── hitomi │ │ │ └── cmlibrary │ │ │ ├── OnMenuSelectedListener.java │ │ │ ├── OnMenuStatusChangeListener.java │ │ │ └── CircleMenu.java │ │ └── AndroidManifest.xml ├── proguard-rules.pro └── build.gradle ├── settings.gradle ├── preview └── circle_menu.gif ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── gradle.properties ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /cmlibrary/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':cmlibrary' 2 | -------------------------------------------------------------------------------- /preview/circle_menu.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/preview/circle_menu.gif -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | CircleMenu 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /cmlibrary/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | cmlibrary 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/icon_gps.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xhdpi/icon_gps.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/icon_home.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xhdpi/icon_home.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/icon_menu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xhdpi/icon_menu.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/icon_gps.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xxhdpi/icon_gps.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/icon_home.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xxhdpi/icon_home.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/icon_menu.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xxhdpi/icon_menu.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/icon_cancel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xhdpi/icon_cancel.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/icon_notify.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xhdpi/icon_notify.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/icon_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xhdpi/icon_search.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/icon_setting.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xhdpi/icon_setting.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/icon_cancel.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xxhdpi/icon_cancel.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/icon_notify.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xxhdpi/icon_notify.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/icon_search.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xxhdpi/icon_search.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/icon_setting.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xxhdpi/icon_setting.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Hitomis/CircleMenu/HEAD/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /.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 | 15 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Sep 23 20:39:01 CST 2019 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-5.1.1-all.zip 7 | -------------------------------------------------------------------------------- /cmlibrary/src/main/java/com/hitomi/cmlibrary/OnMenuSelectedListener.java: -------------------------------------------------------------------------------- 1 | package com.hitomi.cmlibrary; 2 | 3 | /** 4 | * 点击子菜单项按钮的回调接口 5 | * 6 | * Created by hitomi on 2016/10/9. 7 | */ 8 | public interface OnMenuSelectedListener { 9 | 10 | void onMenuSelected(int index); 11 | 12 | } 13 | -------------------------------------------------------------------------------- /cmlibrary/src/main/java/com/hitomi/cmlibrary/OnMenuStatusChangeListener.java: -------------------------------------------------------------------------------- 1 | package com.hitomi.cmlibrary; 2 | 3 | /** 4 | * 菜单状态发生改变时的回调接口 5 | * 6 | * Created by hitomi on 2016/10/9. 7 | */ 8 | public interface OnMenuStatusChangeListener { 9 | 10 | void onMenuOpened(); 11 | 12 | void onMenuClosed(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /cmlibrary/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/test/java/com/hitomi/circlemenu/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.hitomi.circlemenu; 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 | } -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/hitomi/circlemenu/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package com.hitomi.circlemenu; 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 | } -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 28 5 | 6 | defaultConfig { 7 | applicationId "com.hitomi.circlemenu" 8 | minSdkVersion 19 9 | targetSdkVersion 28 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(include: ['*.jar'], dir: 'libs') 23 | implementation 'com.android.support:appcompat-v7:28.0.0' 24 | implementation project(':cmlibrary') 25 | } 26 | -------------------------------------------------------------------------------- /app/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 E:\androidstuido_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 | -------------------------------------------------------------------------------- /cmlibrary/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 E:\androidstuido_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 | -------------------------------------------------------------------------------- /cmlibrary/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | // JitPack Maven 3 | apply plugin: 'com.github.dcendents.android-maven' 4 | // Group 5 | group='com.github.Hitomi' 6 | 7 | android { 8 | compileSdkVersion 28 9 | 10 | defaultConfig { 11 | minSdkVersion 19 12 | targetSdkVersion 28 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 | implementation fileTree(dir: 'libs', include: ['*.jar']) 26 | implementation 'com.android.support:appcompat-v7:28.0.0' 27 | } 28 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /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 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /app/src/main/java/com/hitomi/circlemenu/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.hitomi.circlemenu; 2 | 3 | import android.graphics.Color; 4 | import android.os.Bundle; 5 | import android.support.v7.app.AppCompatActivity; 6 | import android.view.Menu; 7 | 8 | import com.hitomi.cmlibrary.CircleMenu; 9 | import com.hitomi.cmlibrary.OnMenuSelectedListener; 10 | import com.hitomi.cmlibrary.OnMenuStatusChangeListener; 11 | 12 | public class MainActivity extends AppCompatActivity { 13 | 14 | private CircleMenu circleMenu; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | 21 | circleMenu = (CircleMenu) findViewById(R.id.circle_menu); 22 | 23 | circleMenu.setMainMenu(Color.parseColor("#CDCDCD"), R.mipmap.icon_menu, R.mipmap.icon_cancel) 24 | .addSubMenu(Color.parseColor("#258CFF"), R.mipmap.icon_home) 25 | .addSubMenu(Color.parseColor("#30A400"), R.mipmap.icon_search) 26 | .addSubMenu(Color.parseColor("#FF4B32"), R.mipmap.icon_notify) 27 | .addSubMenu(Color.parseColor("#8A39FF"), R.mipmap.icon_setting) 28 | .addSubMenu(Color.parseColor("#FF6A00"), R.mipmap.icon_gps) 29 | .setOnMenuSelectedListener(new OnMenuSelectedListener() { 30 | 31 | @Override 32 | public void onMenuSelected(int index) {} 33 | 34 | }).setOnMenuStatusChangeListener(new OnMenuStatusChangeListener() { 35 | 36 | @Override 37 | public void onMenuOpened() {} 38 | 39 | @Override 40 | public void onMenuClosed() {} 41 | 42 | }); 43 | } 44 | 45 | @Override 46 | public boolean onMenuOpened(int featureId, Menu menu) { 47 | circleMenu.openMenu(); 48 | return super.onMenuOpened(featureId, menu); 49 | } 50 | 51 | @Override 52 | public void onBackPressed() { 53 | circleMenu.closeMenu(); 54 | } 55 | } 56 | -------------------------------------------------------------------------------- /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 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 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 Windowz 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 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # CircleMenu 2 | 3 | [![Android Arsenal](https://img.shields.io/badge/Android%20Arsenal-CircleMenu-brightgreen.svg?style=flat)](http://android-arsenal.com/details/1/4631) 4 | [![](https://jitpack.io/v/Hitomis/CircleMenu.svg)](https://jitpack.io/#Hitomis/CircleMenu) 5 | 6 | CircleMenu 是一个精美别致支持定制的圆形菜单,可以有 0 到 8 个子菜单按钮,按钮背景色,图标都可以修改。CircleMenu 比较适合放在屏幕中间,以得到完好的展现。 7 | 8 | 小记:
9 | 10 | CircleMenu 是在 dribbble 上看到的一个比较有感觉的设计,并不炫酷,但是 UI 很精致,值得推敲的细节比较多。因为没有比较标准的原型图。在还原设计的时候,来来回回修改好多次。到最后也不知道跟原设计有多少差距。不过自己看着还算满意。 11 | 12 | 该库在实现的时候碰到了许多困难。比如选中子菜单项绘制圆环轨迹路径的时候,使用 PathMeasure 发现 getSegment 方法并不是可以截取任何两个位置之间的 Path(因为要从选中的子菜单按钮的位置开始绘制圆环轨迹路径),思考良久后,使用旋转画布的方法巧妙解决。 13 | 14 | 又例如,在圆环绘制完成后,圆环会逐渐放大扩散变透明,然后消失的动画。如果这个动画针对的是一个 View 对象,我想使用 ObjectAnimator 可以很快解决。但是现在是在 onDraw 中绘制这一动画效果。最困难的是绘制圆环扩散变透明直至消失这一动画效果。(ps:期间还问过我 QQ 中所有技术群。都说很简单,就是没人说具体,群中绝大部分都是各种灌水,到最后也得不到答案。那时候的感觉很糟糕,感觉再也不相信技术群了,伤心···)最后实现很简单,也是无意中发现 ColorUtils 这个类, ColorUtils 是 Support.v4 中提供的,封装了对 Color 的各种操作。我使用了 ColorUtils.setAlphaComponent(color, alpha) 来操作圆环的颜色的透明度,从而达到一个圆环逐渐消失的效果。 15 | 16 | 最后谢谢 [Aige](http://blog.csdn.net/aigestudio?viewmode=contents) 和 [GcsSloop](http://www.gcssloop.com/#blog) 两位大神无私奉献。让我能深入学习自定义 View 范畴的知识。让我有能力去完成这个项目。 17 | 18 | 19 | # Preview 20 | 21 | 22 | 23 | 24 | # Import 25 | 26 | ### Gradle 27 | 28 | Step 1. Add the JitPack repository to your build file 29 | 30 | allprojects { 31 | repositories { 32 | ... 33 | maven { url "https://jitpack.io" } 34 | } 35 | } 36 | 37 | Step 2. Add the dependency 38 | 39 | dependencies { 40 | compile 'com.github.Hitomis:CircleMenu:v1.1.0' 41 | } 42 | 43 | ### Maven 44 | 45 | Step 1. Add the JitPack repository to your build file 46 | 47 | 48 | 49 | jitpack.io 50 | https://jitpack.io 51 | 52 | 53 | 54 | Step 2. Add the dependency 55 | 56 | 57 | com.github.Hitomis 58 | CircleMenu 59 | v1.1.0 60 | 61 | 62 | # Usage 63 | 64 | 布局文件中: 65 | 66 | 70 | 71 | Activity 中: 72 | 73 | circleMenu = (CircleMenu) findViewById(R.id.circle_menu); 74 | 75 | circleMenu.setMainMenu(Color.parseColor("#CDCDCD"), R.mipmap.icon_menu, R.mipmap.icon_cancel) 76 | .addSubMenu(Color.parseColor("#258CFF"), R.mipmap.icon_home) 77 | .addSubMenu(Color.parseColor("#30A400"), R.mipmap.icon_search) 78 | .addSubMenu(Color.parseColor("#FF4B32"), R.mipmap.icon_notify) 79 | .addSubMenu(Color.parseColor("#8A39FF"), R.mipmap.icon_setting) 80 | .addSubMenu(Color.parseColor("#FF6A00"), R.mipmap.icon_gps) 81 | .setOnMenuSelectedListener(new OnMenuSelectedListener() { 82 | 83 | @Override 84 | public void onMenuSelected(int index) {} 85 | 86 | }).setOnMenuStatusChangeListener(new OnMenuStatusChangeListener() { 87 | 88 | @Override 89 | public void onMenuOpened() {} 90 | 91 | @Override 92 | public void onMenuClosed() {} 93 | 94 | }); 95 | 96 | # Method 97 | 98 | | 方法 | 说明 | 99 | | :--: | :--: | 100 | | setMainMenu | 设置主按钮(打开/关闭)的背景色,以及打开/关闭的图标。图标支持 Resource、Bitmap、Drawable 形式 | 101 | | addSubMenu | 添加一个子菜单项,包括子菜单的背景色以及图标 。图标支持 Resource、Bitmap、Drawable 形式| 102 | | openMenu | 打开菜单 | 103 | | closeMenu | 关闭菜单 | 104 | | isOpened | 菜单是否打开,返回 boolean 值 | 105 | | setOnMenuSelectedListener | 设置选中子菜单项的监听器,回调方法会传递当前点击子菜单项的下标值,从 0 开始计算 | 106 | | setOnMenuStatusChangeListener | 设置 CircleMenu 行为状态监听器,onMenuOpened 为菜单打开后的回调方法,onMenuClosed 为菜单关闭后的回调方法 | 107 | 108 | 109 | # Thanks 110 | 111 | 原型设计来源于 [dribbble](https://dribbble.com/shots/2534780-Circle-Menu-Swift-Open-Source)
112 | IOS 版本 [Ramotion](https://github.com/Ramotion/circle-menu) 113 | 114 | # Licence 115 | 116 | Copyright 2016 Hitomis, Inc. 117 | 118 | Licensed under the Apache License, Version 2.0 (the "License"); 119 | you may not use this file except in compliance with the License. 120 | You may obtain a copy of the License at 121 | 122 | http://www.apache.org/licenses/LICENSE-2.0 123 | 124 | Unless required by applicable law or agreed to in writing, software 125 | distributed under the License is distributed on an "AS IS" BASIS, 126 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 127 | See the License for the specific language governing permissions and 128 | limitations under the License. 129 | 130 | 131 | 132 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /cmlibrary/src/main/java/com/hitomi/cmlibrary/CircleMenu.java: -------------------------------------------------------------------------------- 1 | package com.hitomi.cmlibrary; 2 | 3 | import android.animation.Animator; 4 | import android.animation.AnimatorListenerAdapter; 5 | import android.animation.AnimatorSet; 6 | import android.animation.ValueAnimator; 7 | import android.content.Context; 8 | import android.graphics.Bitmap; 9 | import android.graphics.Canvas; 10 | import android.graphics.Color; 11 | import android.graphics.Paint; 12 | import android.graphics.Path; 13 | import android.graphics.PathMeasure; 14 | import android.graphics.RadialGradient; 15 | import android.graphics.RectF; 16 | import android.graphics.Shader; 17 | import android.graphics.drawable.BitmapDrawable; 18 | import android.graphics.drawable.Drawable; 19 | import android.graphics.drawable.GradientDrawable; 20 | import android.support.v4.graphics.ColorUtils; 21 | import android.util.AttributeSet; 22 | import android.view.MotionEvent; 23 | import android.view.View; 24 | import android.view.animation.AccelerateDecelerateInterpolator; 25 | import android.view.animation.AnticipateInterpolator; 26 | import android.view.animation.LinearInterpolator; 27 | import android.view.animation.OvershootInterpolator; 28 | 29 | import java.util.ArrayList; 30 | import java.util.List; 31 | 32 | /** 33 | * Created by hitomi on 2016/9/28. 34 | *

35 | * github : https://github.com/Hitomis
36 | *

37 | * email : 196425254@qq.com 38 | */ 39 | public class CircleMenu extends View { 40 | 41 | private static final int STATUS_MENU_OPEN = 1; 42 | 43 | private static final int STATUS_MENU_OPENED = 1 << 1; 44 | 45 | private static final int STATUS_MENU_CLOSE = 1 << 2; 46 | 47 | private static final int STATUS_MENU_CLOSE_CLEAR = 1 << 3; 48 | 49 | private static final int STATUS_MENU_CLOSED = 1 << 4; 50 | 51 | private static final int STATUS_MENU_CANCEL = 1 << 5; 52 | 53 | private static final int MAX_SUBMENU_NUM = 8; 54 | 55 | private final int shadowRadius = 5; 56 | 57 | private int partSize; 58 | 59 | private int iconSize; 60 | 61 | private float circleMenuRadius; 62 | 63 | private int itemNum; 64 | 65 | private float itemMenuRadius; 66 | 67 | private float fraction, rFraction; 68 | 69 | private float pathLength; 70 | 71 | private int mainMenuColor; 72 | 73 | private Drawable openMenuIcon, closeMenuIcon; 74 | 75 | private List subMenuColorList; 76 | 77 | private List subMenuDrawableList; 78 | 79 | private List menuRectFList; 80 | 81 | private int centerX, centerY; 82 | 83 | private int clickIndex; 84 | 85 | private int rotateAngle; 86 | 87 | private int itemIconSize; 88 | 89 | private int pressedColor; 90 | 91 | private int status; 92 | 93 | private boolean pressed; 94 | 95 | private Paint oPaint, cPaint, sPaint; 96 | 97 | private PathMeasure pathMeasure; 98 | 99 | private Path path, dstPath; 100 | 101 | private OnMenuSelectedListener onMenuSelectedListener; 102 | 103 | private OnMenuStatusChangeListener onMenuStatusChangeListener; 104 | 105 | public CircleMenu(Context context) { 106 | this(context, null); 107 | } 108 | 109 | public CircleMenu(Context context, AttributeSet attrs) { 110 | this(context, attrs, 0); 111 | } 112 | 113 | public CircleMenu(Context context, AttributeSet attrs, int defStyleAttr) { 114 | super(context, attrs, defStyleAttr); 115 | status = STATUS_MENU_CLOSED; 116 | init(); 117 | } 118 | 119 | private void init() { 120 | initTool(); 121 | 122 | mainMenuColor = Color.parseColor("#CDCDCD"); 123 | 124 | openMenuIcon = new GradientDrawable(); 125 | closeMenuIcon = new GradientDrawable(); 126 | 127 | subMenuColorList = new ArrayList<>(); 128 | subMenuDrawableList = new ArrayList<>(); 129 | menuRectFList = new ArrayList<>(); 130 | } 131 | 132 | private void initTool() { 133 | oPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 134 | oPaint.setStyle(Paint.Style.FILL_AND_STROKE); 135 | 136 | cPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 137 | cPaint.setStyle(Paint.Style.STROKE); 138 | cPaint.setStrokeCap(Paint.Cap.ROUND); 139 | 140 | sPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 141 | sPaint.setStyle(Paint.Style.FILL); 142 | 143 | path = new Path(); 144 | dstPath = new Path(); 145 | pathMeasure = new PathMeasure(); 146 | } 147 | 148 | @Override 149 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 150 | int widthMode = MeasureSpec.getMode(widthMeasureSpec); 151 | int heightMode = MeasureSpec.getMode(heightMeasureSpec); 152 | 153 | int width = MeasureSpec.getSize(widthMeasureSpec); 154 | int height = MeasureSpec.getSize(heightMeasureSpec); 155 | 156 | int measureWidthSize = width, measureHeightSize = height; 157 | 158 | if (widthMode == MeasureSpec.AT_MOST) { 159 | measureWidthSize = dip2px(20) * 10; 160 | } 161 | 162 | if (heightMode == MeasureSpec.AT_MOST) { 163 | measureHeightSize = dip2px(20) * 10; 164 | } 165 | setMeasuredDimension(measureWidthSize, measureHeightSize); 166 | } 167 | 168 | @Override 169 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 170 | super.onSizeChanged(w, h, oldw, oldh); 171 | 172 | int minSize = Math.min(getMeasuredWidth(), getMeasuredHeight()); 173 | 174 | partSize = minSize / 10; 175 | iconSize = partSize * 4 / 5; 176 | circleMenuRadius = partSize * 3; 177 | 178 | centerX = getMeasuredWidth() / 2; 179 | centerY = getMeasuredHeight() / 2; 180 | resetMainDrawableBounds(); 181 | 182 | path.addCircle(centerX, centerY, circleMenuRadius, Path.Direction.CW); 183 | pathMeasure.setPath(path, true); 184 | pathLength = pathMeasure.getLength(); 185 | 186 | RectF mainMenuRectF = new RectF(centerX - partSize, centerY - partSize, centerX + partSize, centerY + partSize); 187 | menuRectFList.add(mainMenuRectF); 188 | } 189 | 190 | @Override 191 | protected void onDraw(Canvas canvas) { 192 | switch (status) { 193 | case STATUS_MENU_CLOSED: 194 | drawMainMenu(canvas); 195 | break; 196 | case STATUS_MENU_OPEN: 197 | drawMainMenu(canvas); 198 | drawSubMenu(canvas); 199 | break; 200 | case STATUS_MENU_OPENED: 201 | drawMainMenu(canvas); 202 | drawSubMenu(canvas); 203 | break; 204 | case STATUS_MENU_CLOSE: 205 | drawMainMenu(canvas); 206 | drawSubMenu(canvas); 207 | drawCircleMenu(canvas); 208 | break; 209 | case STATUS_MENU_CLOSE_CLEAR: 210 | drawMainMenu(canvas); 211 | drawCircleMenu(canvas); 212 | break; 213 | case STATUS_MENU_CANCEL: 214 | drawMainMenu(canvas); 215 | drawSubMenu(canvas); 216 | break; 217 | } 218 | } 219 | 220 | /** 221 | * 绘制周围子菜单环绕的圆环路径 222 | * 223 | * @param canvas 224 | */ 225 | private void drawCircleMenu(Canvas canvas) { 226 | if (status == STATUS_MENU_CLOSE) { 227 | drawCirclePath(canvas); 228 | drawCircleIcon(canvas); 229 | } else { 230 | cPaint.setStrokeWidth(partSize * 2 + partSize * .5f * fraction); 231 | cPaint.setColor(calcAlphaColor(getClickMenuColor(), true)); 232 | canvas.drawCircle(centerX, centerY, circleMenuRadius + partSize * .5f * fraction, cPaint); 233 | } 234 | } 235 | 236 | private int getClickMenuColor() { 237 | return clickIndex == 0 ? mainMenuColor : subMenuColorList.get(clickIndex - 1); 238 | } 239 | 240 | /** 241 | * 绘制子菜单转动时的图标 242 | * 243 | * @param canvas 244 | */ 245 | private void drawCircleIcon(Canvas canvas) { 246 | canvas.save(); 247 | Drawable selDrawable = subMenuDrawableList.get(clickIndex - 1); 248 | if (selDrawable == null) return; 249 | int startAngle = (clickIndex - 1) * (360 / itemNum); 250 | int endAngle = 360 + startAngle; 251 | int itemX = (int) (centerX + Math.sin(Math.toRadians((endAngle - startAngle) * fraction + startAngle)) * circleMenuRadius); 252 | int itemY = (int) (centerY - Math.cos(Math.toRadians((endAngle - startAngle) * fraction + startAngle)) * circleMenuRadius); 253 | canvas.rotate(360 * fraction, itemX, itemY); 254 | selDrawable.setBounds(itemX - iconSize / 2, itemY - iconSize / 2, itemX + iconSize / 2, itemY + iconSize / 2); 255 | selDrawable.draw(canvas); 256 | canvas.restore(); 257 | } 258 | 259 | /** 260 | * 绘制子菜单项转动时的轨迹路径 261 | * 262 | * @param canvas 263 | */ 264 | private void drawCirclePath(Canvas canvas) { 265 | canvas.save(); 266 | canvas.rotate(rotateAngle, centerX, centerY); 267 | dstPath.reset(); 268 | dstPath.lineTo(0, 0); 269 | pathMeasure.getSegment(0, pathLength * fraction, dstPath, true); 270 | cPaint.setStrokeWidth(partSize * 2); 271 | cPaint.setColor(getClickMenuColor()); 272 | canvas.drawPath(dstPath, cPaint); 273 | canvas.restore(); 274 | } 275 | 276 | /** 277 | * 绘制周围子菜单项按钮 278 | * 279 | * @param canvas 280 | */ 281 | private void drawSubMenu(Canvas canvas) { 282 | int itemX, itemY, angle; 283 | final float offsetRadius = 1.5f; 284 | RectF menuRectF; 285 | for (int i = 0; i < itemNum; i++) { 286 | angle = i * (360 / itemNum); 287 | if (status == STATUS_MENU_OPEN) { 288 | itemX = (int) (centerX + Math.sin(Math.toRadians(angle)) * (circleMenuRadius - (1 - fraction) * partSize * offsetRadius)); 289 | itemY = (int) (centerY - Math.cos(Math.toRadians(angle)) * (circleMenuRadius - (1 - fraction) * partSize * offsetRadius)); 290 | oPaint.setColor(calcAlphaColor(subMenuColorList.get(i), false)); 291 | sPaint.setColor(calcAlphaColor(subMenuColorList.get(i), false)); 292 | } else if (status == STATUS_MENU_CANCEL) { 293 | itemX = (int) (centerX + Math.sin(Math.toRadians(angle)) * (circleMenuRadius - fraction * partSize * offsetRadius)); 294 | itemY = (int) (centerY - Math.cos(Math.toRadians(angle)) * (circleMenuRadius - fraction * partSize * offsetRadius)); 295 | oPaint.setColor(calcAlphaColor(subMenuColorList.get(i), true)); 296 | sPaint.setColor(calcAlphaColor(subMenuColorList.get(i), true)); 297 | } else { 298 | itemX = (int) (centerX + Math.sin(Math.toRadians(angle)) * circleMenuRadius); 299 | itemY = (int) (centerY - Math.cos(Math.toRadians(angle)) * circleMenuRadius); 300 | oPaint.setColor(subMenuColorList.get(i)); 301 | sPaint.setColor(subMenuColorList.get(i)); 302 | } 303 | if (pressed && clickIndex - 1 == i) { 304 | oPaint.setColor(pressedColor); 305 | } 306 | drawMenuShadow(canvas, itemX, itemY, itemMenuRadius); 307 | canvas.drawCircle(itemX, itemY, itemMenuRadius, oPaint); 308 | drawSubMenuIcon(canvas, itemX, itemY, i); 309 | menuRectF = new RectF(itemX - partSize, itemY - partSize, itemX + partSize, itemY + partSize); 310 | if (menuRectFList.size() - 1 > i) { 311 | menuRectFList.remove(i + 1); 312 | } 313 | menuRectFList.add(i + 1, menuRectF); 314 | } 315 | } 316 | 317 | /** 318 | * 绘制子菜单项图标 319 | * 320 | * @param canvas 321 | * @param centerX 322 | * @param centerY 323 | * @param index 324 | */ 325 | private void drawSubMenuIcon(Canvas canvas, int centerX, int centerY, int index) { 326 | int diff; 327 | if (status == STATUS_MENU_OPEN || status == STATUS_MENU_CANCEL) { 328 | diff = itemIconSize / 2; 329 | } else { 330 | diff = iconSize / 2; 331 | } 332 | resetBoundsAndDrawIcon(canvas, subMenuDrawableList.get(index), centerX, centerY, diff); 333 | } 334 | 335 | private void resetBoundsAndDrawIcon(Canvas canvas, Drawable drawable, int centerX, int centerY, int diff) { 336 | if (drawable == null) return; 337 | drawable.setBounds(centerX - diff, centerY - diff, centerX + diff, centerY + diff); 338 | drawable.draw(canvas); 339 | } 340 | 341 | /** 342 | * 绘制中间的菜单开关按钮 343 | * 344 | * @param canvas 345 | */ 346 | private void drawMainMenu(Canvas canvas) { 347 | float centerMenuRadius, realFraction; 348 | if (status == STATUS_MENU_CLOSE) { 349 | // 中心主菜单按钮以两倍速度缩小 350 | realFraction = (1 - fraction * 2) == 0 ? 0 : (1 - fraction * 2); 351 | centerMenuRadius = partSize * realFraction; 352 | } else if (status == STATUS_MENU_CLOSE_CLEAR) { 353 | // 中心主菜单按钮以四倍速度扩大 354 | realFraction = fraction * 4 >= 1 ? 1 : fraction * 4; 355 | centerMenuRadius = partSize * realFraction; 356 | } else if (status == STATUS_MENU_CLOSED || status == STATUS_MENU_CANCEL) { 357 | centerMenuRadius = partSize; 358 | } else { 359 | centerMenuRadius = partSize; 360 | } 361 | if (status == STATUS_MENU_OPEN || status == STATUS_MENU_OPENED || status == STATUS_MENU_CLOSE) { 362 | oPaint.setColor(calcPressedEffectColor(0, .5f)); 363 | } else if (pressed && clickIndex == 0) { 364 | oPaint.setColor(pressedColor); 365 | } else { 366 | oPaint.setColor(mainMenuColor); 367 | sPaint.setColor(mainMenuColor); 368 | } 369 | drawMenuShadow(canvas, centerX, centerY, centerMenuRadius); 370 | canvas.drawCircle(centerX, centerY, centerMenuRadius, oPaint); 371 | drawMainMenuIcon(canvas); 372 | } 373 | 374 | private void drawMainMenuIcon(Canvas canvas) { 375 | canvas.save(); 376 | switch (status) { 377 | case STATUS_MENU_CLOSED: 378 | if (openMenuIcon != null) 379 | openMenuIcon.draw(canvas); 380 | break; 381 | case STATUS_MENU_OPEN: 382 | canvas.rotate(45 * (fraction - 1), centerX, centerY); 383 | resetBoundsAndDrawIcon(canvas, closeMenuIcon, centerX, centerY, iconSize / 2); 384 | break; 385 | case STATUS_MENU_OPENED: 386 | resetBoundsAndDrawIcon(canvas, closeMenuIcon, centerX, centerY, iconSize / 2); 387 | break; 388 | case STATUS_MENU_CLOSE: 389 | resetBoundsAndDrawIcon(canvas, closeMenuIcon, centerX, centerY, itemIconSize / 2); 390 | break; 391 | case STATUS_MENU_CLOSE_CLEAR: 392 | canvas.rotate(90 * (rFraction - 1), centerX, centerY); 393 | resetBoundsAndDrawIcon(canvas, openMenuIcon, centerX, centerY, itemIconSize / 2); 394 | break; 395 | case STATUS_MENU_CANCEL: 396 | canvas.rotate(-45 * fraction, centerX, centerY); 397 | if (closeMenuIcon != null) 398 | closeMenuIcon.draw(canvas); 399 | break; 400 | } 401 | canvas.restore(); 402 | } 403 | 404 | /** 405 | * 绘制菜单按钮阴影 406 | * 407 | * @param canvas 408 | * @param centerX 409 | * @param centerY 410 | */ 411 | private void drawMenuShadow(Canvas canvas, int centerX, int centerY, float radius) { 412 | if (radius + shadowRadius > 0) { 413 | sPaint.setShader(new RadialGradient(centerX, centerY, radius + shadowRadius, 414 | Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP)); 415 | canvas.drawCircle(centerX, centerY, radius + shadowRadius, sPaint); 416 | } 417 | } 418 | 419 | @Override 420 | public boolean onTouchEvent(MotionEvent event) { 421 | if (status == STATUS_MENU_CLOSE || status == STATUS_MENU_CLOSE_CLEAR) return true; 422 | int index = clickWhichRectF(event.getX(), event.getY()); 423 | switch (event.getAction()) { 424 | case MotionEvent.ACTION_DOWN: 425 | pressed = true; 426 | if (index != -1) { 427 | clickIndex = index; 428 | updatePressEffect(index, pressed); 429 | } 430 | break; 431 | case MotionEvent.ACTION_MOVE: 432 | if (index == -1) { 433 | pressed = false; 434 | invalidate(); 435 | } 436 | break; 437 | case MotionEvent.ACTION_UP: 438 | pressed = false; 439 | if (index != -1) { 440 | clickIndex = index; 441 | updatePressEffect(index, pressed); 442 | } 443 | if (index == 0) { // 点击的是中间的按钮 444 | if (status == STATUS_MENU_CLOSED) { 445 | status = STATUS_MENU_OPEN; 446 | startOpenMenuAnima(); 447 | } else if (status == STATUS_MENU_OPENED) { 448 | status = STATUS_MENU_CANCEL; 449 | startCancelMenuAnima(); 450 | } 451 | } else { // 点击的是周围子菜单项按钮 452 | if (status == STATUS_MENU_OPENED && index != -1) { 453 | status = STATUS_MENU_CLOSE; 454 | if (onMenuSelectedListener != null) 455 | onMenuSelectedListener.onMenuSelected(index - 1); 456 | rotateAngle = clickIndex * (360 / itemNum) - (360 / itemNum) - 90; 457 | startCloseMeunAnima(); 458 | } 459 | } 460 | break; 461 | } 462 | return true; 463 | } 464 | 465 | /** 466 | * 更新按钮的状态 467 | * 468 | * @param menuIndex 469 | * @param press 470 | */ 471 | private void updatePressEffect(int menuIndex, boolean press) { 472 | if (press) { 473 | pressedColor = calcPressedEffectColor(menuIndex, .15f); 474 | } 475 | invalidate(); 476 | } 477 | 478 | /** 479 | * 获取按钮被按下的颜色 480 | * 481 | * @param menuIndex 482 | * @param depth 取值范围为[0, 1].值越大,颜色越深 483 | * @return 484 | */ 485 | private int calcPressedEffectColor(int menuIndex, float depth) { 486 | int color = menuIndex == 0 ? mainMenuColor : subMenuColorList.get(menuIndex - 1); 487 | float[] hsv = new float[3]; 488 | Color.colorToHSV(color, hsv); 489 | hsv[2] *= (1.f - depth); 490 | return Color.HSVToColor(hsv); 491 | } 492 | 493 | /** 494 | * 用于完成在 View 中的圆环逐渐扩散消失的动画效果
495 | *

496 | * 根据 fraction 调整 color 的 Alpha 值 497 | * 498 | * @param color 被调整 Alpha 值的颜色 499 | * @param reverse true : 由不透明到透明的顺序调整,否则就逆序 500 | * @return 501 | */ 502 | private int calcAlphaColor(int color, boolean reverse) { 503 | int alpha; 504 | if (reverse) { // 由不透明到透明 505 | alpha = (int) (255 * (1.f - fraction)); 506 | } else { // 由透明到不透明 507 | alpha = (int) (255 * fraction); 508 | } 509 | if (alpha >= 255) alpha = 255; 510 | if (alpha <= 0) alpha = 0; 511 | return ColorUtils.setAlphaComponent(color, alpha); 512 | } 513 | 514 | /** 515 | * 启动打开菜单动画 516 | */ 517 | private void startOpenMenuAnima() { 518 | ValueAnimator openAnima = ValueAnimator.ofFloat(1.f, 100.f); 519 | openAnima.setDuration(500); 520 | openAnima.setInterpolator(new OvershootInterpolator()); 521 | openAnima.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 522 | @Override 523 | public void onAnimationUpdate(ValueAnimator valueAnimator) { 524 | fraction = valueAnimator.getAnimatedFraction(); 525 | itemMenuRadius = fraction * partSize; 526 | itemIconSize = (int) (fraction * iconSize); 527 | invalidate(); 528 | } 529 | }); 530 | openAnima.addListener(new AnimatorListenerAdapter() { 531 | @Override 532 | public void onAnimationEnd(Animator animation) { 533 | status = STATUS_MENU_OPENED; 534 | if (onMenuStatusChangeListener != null) 535 | onMenuStatusChangeListener.onMenuOpened(); 536 | } 537 | }); 538 | openAnima.start(); 539 | } 540 | 541 | /** 542 | * 启动取消动画 543 | */ 544 | private void startCancelMenuAnima() { 545 | ValueAnimator cancelAnima = ValueAnimator.ofFloat(1.f, 100.f); 546 | cancelAnima.setDuration(500); 547 | cancelAnima.setInterpolator(new AnticipateInterpolator()); 548 | cancelAnima.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 549 | @Override 550 | public void onAnimationUpdate(ValueAnimator valueAnimator) { 551 | fraction = valueAnimator.getAnimatedFraction(); 552 | itemMenuRadius = (1 - fraction) * partSize; 553 | itemIconSize = (int) ((1 - fraction) * iconSize); 554 | invalidate(); 555 | } 556 | }); 557 | cancelAnima.addListener(new AnimatorListenerAdapter() { 558 | @Override 559 | public void onAnimationEnd(Animator animation) { 560 | status = STATUS_MENU_CLOSED; 561 | if (onMenuStatusChangeListener != null) 562 | onMenuStatusChangeListener.onMenuClosed(); 563 | } 564 | }); 565 | cancelAnima.start(); 566 | } 567 | 568 | /** 569 | * 开启关闭菜单动画
570 | *

关闭菜单动画分为三部分

571 | * 572 | *
  • 选中菜单项转动一周
  • 573 | *
  • 环状轨迹扩散消失
  • 574 | *
  • 主菜单按钮旋转
  • 575 | *
    576 | */ 577 | private void startCloseMeunAnima() { 578 | // 选中菜单项转动一周动画驱动 579 | ValueAnimator aroundAnima = ValueAnimator.ofFloat(1.f, 100.f); 580 | aroundAnima.setDuration(600); 581 | aroundAnima.setInterpolator(new AccelerateDecelerateInterpolator()); 582 | aroundAnima.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 583 | @Override 584 | public void onAnimationUpdate(ValueAnimator valueAnimator) { 585 | fraction = valueAnimator.getAnimatedFraction(); 586 | // 中心主菜单图标以两倍速度缩小 587 | float animaFraction = fraction * 2 >= 1 ? 1 : fraction * 2; 588 | itemIconSize = (int) ((1 - animaFraction) * iconSize); 589 | invalidate(); 590 | } 591 | }); 592 | aroundAnima.addListener(new AnimatorListenerAdapter() { 593 | @Override 594 | public void onAnimationEnd(Animator animation) { 595 | status = STATUS_MENU_CLOSE_CLEAR; 596 | } 597 | }); 598 | 599 | // 环状轨迹扩散消失动画驱动 600 | ValueAnimator spreadAnima = ValueAnimator.ofFloat(1.f, 100.f); 601 | spreadAnima.setInterpolator(new LinearInterpolator()); 602 | spreadAnima.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 603 | @Override 604 | public void onAnimationUpdate(ValueAnimator valueAnimator) { 605 | fraction = valueAnimator.getAnimatedFraction(); 606 | } 607 | }); 608 | 609 | // 主菜单转动动画驱动 610 | ValueAnimator rotateAnima = ValueAnimator.ofFloat(1.f, 100.f); 611 | rotateAnima.setInterpolator(new OvershootInterpolator()); 612 | rotateAnima.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 613 | @Override 614 | public void onAnimationUpdate(ValueAnimator valueAnimator) { 615 | rFraction = valueAnimator.getAnimatedFraction(); 616 | itemIconSize = (int) (rFraction * iconSize); 617 | invalidate(); 618 | } 619 | }); 620 | 621 | AnimatorSet closeAnimaSet = new AnimatorSet(); 622 | closeAnimaSet.setDuration(500); 623 | closeAnimaSet.play(spreadAnima).with(rotateAnima); 624 | closeAnimaSet.addListener(new AnimatorListenerAdapter() { 625 | @Override 626 | public void onAnimationEnd(Animator animation) { 627 | status = STATUS_MENU_CLOSED; 628 | if (onMenuStatusChangeListener != null) 629 | onMenuStatusChangeListener.onMenuClosed(); 630 | } 631 | }); 632 | 633 | AnimatorSet animatorSet = new AnimatorSet(); 634 | animatorSet.play(aroundAnima).before(closeAnimaSet); 635 | animatorSet.start(); 636 | } 637 | 638 | /** 639 | * 获取当前点击的是哪一个菜单按钮
    640 | * 中心菜单下标为0,周围菜单从正上方顺时针计数1~5 641 | * 642 | * @param x 643 | * @param y 644 | * @return 645 | */ 646 | private int clickWhichRectF(float x, float y) { 647 | int which = -1; 648 | for (RectF rectF : menuRectFList) { 649 | if (rectF.contains(x, y)) { 650 | which = menuRectFList.indexOf(rectF); 651 | break; 652 | } 653 | } 654 | return which; 655 | } 656 | 657 | private Drawable convertDrawable(int iconRes) { 658 | return getResources().getDrawable(iconRes); 659 | } 660 | 661 | private Drawable convertBitmap(Bitmap bitmap) { 662 | return new BitmapDrawable(getResources(), bitmap); 663 | } 664 | 665 | private void resetMainDrawableBounds() { 666 | openMenuIcon.setBounds(centerX - iconSize / 2, centerY - iconSize / 2, 667 | centerX + iconSize / 2, centerY + iconSize / 2); 668 | closeMenuIcon.setBounds(centerX - iconSize / 2, centerY - iconSize / 2, 669 | centerX + iconSize / 2, centerY + iconSize / 2); 670 | } 671 | 672 | /** 673 | * 设置主菜单的背景色,以及打开/关闭的图标 674 | * 675 | * @param mainMenuColor 主菜单背景色 676 | * @param openMenuRes 菜单打开图标,Resource 格式 677 | * @param closeMenuRes 菜单关闭图标,Resource 格式 678 | * @return 679 | */ 680 | public CircleMenu setMainMenu(int mainMenuColor, int openMenuRes, int closeMenuRes) { 681 | openMenuIcon = convertDrawable(openMenuRes); 682 | closeMenuIcon = convertDrawable(closeMenuRes); 683 | this.mainMenuColor = mainMenuColor; 684 | return this; 685 | } 686 | 687 | /** 688 | * 设置主菜单的背景色,以及打开/关闭的图标 689 | * 690 | * @param mainMenuColor 主菜单背景色 691 | * @param openMenuBitmap 菜单打开图标,Bitmap 格式 692 | * @param closeMenuBitmap 菜单关闭图标,Bitmap 格式 693 | * @return 694 | */ 695 | public CircleMenu setMainMenu(int mainMenuColor, Bitmap openMenuBitmap, Bitmap closeMenuBitmap) { 696 | openMenuIcon = convertBitmap(openMenuBitmap); 697 | closeMenuIcon = convertBitmap(closeMenuBitmap); 698 | this.mainMenuColor = mainMenuColor; 699 | return this; 700 | } 701 | 702 | /** 703 | * 设置主菜单的背景色,以及打开/关闭的图标 704 | * 705 | * @param mainMenuColor 主菜单背景色 706 | * @param openMenuDrawable 菜单打开图标,Drawable 格式 707 | * @param closeMenuDrawable 菜单关闭图标,Drawable 格式 708 | * @return 709 | */ 710 | public CircleMenu setMainMenu(int mainMenuColor, Drawable openMenuDrawable, Drawable closeMenuDrawable) { 711 | openMenuIcon = openMenuDrawable; 712 | closeMenuIcon = closeMenuDrawable; 713 | this.mainMenuColor = mainMenuColor; 714 | return this; 715 | } 716 | 717 | /** 718 | * 添加一个子菜单项,包括子菜单的背景色以及图标 719 | * 720 | * @param menuColor 子菜单的背景色 721 | * @param menuRes 子菜单图标,Resource 格式 722 | * @return 723 | */ 724 | public CircleMenu addSubMenu(int menuColor, int menuRes) { 725 | if (subMenuColorList.size() < MAX_SUBMENU_NUM && subMenuDrawableList.size() < MAX_SUBMENU_NUM) { 726 | subMenuColorList.add(menuColor); 727 | subMenuDrawableList.add(convertDrawable(menuRes)); 728 | itemNum = Math.min(subMenuColorList.size(), subMenuDrawableList.size()); 729 | } 730 | return this; 731 | } 732 | 733 | /** 734 | * 添加一个子菜单项,包括子菜单的背景色以及图标 735 | * 736 | * @param menuColor 子菜单的背景色 737 | * @param menuBitmap 子菜单图标,Bitmap 格式 738 | * @return 739 | */ 740 | public CircleMenu addSubMenu(int menuColor, Bitmap menuBitmap) { 741 | if (subMenuColorList.size() < MAX_SUBMENU_NUM && subMenuDrawableList.size() < MAX_SUBMENU_NUM) { 742 | subMenuColorList.add(menuColor); 743 | subMenuDrawableList.add(convertBitmap(menuBitmap)); 744 | itemNum = Math.min(subMenuColorList.size(), subMenuDrawableList.size()); 745 | } 746 | return this; 747 | } 748 | 749 | /** 750 | * 添加一个子菜单项,包括子菜单的背景色以及图标 751 | * 752 | * @param menuColor 子菜单的背景色 753 | * @param menuDrawable 子菜单图标,Drawable 格式 754 | * @return 755 | */ 756 | public CircleMenu addSubMenu(int menuColor, Drawable menuDrawable) { 757 | if (subMenuColorList.size() < MAX_SUBMENU_NUM && subMenuDrawableList.size() < MAX_SUBMENU_NUM) { 758 | subMenuColorList.add(menuColor); 759 | subMenuDrawableList.add(menuDrawable); 760 | itemNum = Math.min(subMenuColorList.size(), subMenuDrawableList.size()); 761 | } 762 | return this; 763 | } 764 | 765 | /** 766 | * 打开菜单 767 | * Open the CircleMenu 768 | */ 769 | public void openMenu() { 770 | if (status == STATUS_MENU_CLOSED) { 771 | status = STATUS_MENU_OPEN; 772 | startOpenMenuAnima(); 773 | } 774 | } 775 | 776 | /** 777 | * 关闭菜单 778 | * Close the CircleMenu 779 | */ 780 | public void closeMenu() { 781 | if (status == STATUS_MENU_OPENED) { 782 | status = STATUS_MENU_CANCEL; 783 | startCancelMenuAnima(); 784 | } 785 | } 786 | 787 | /** 788 | * 菜单是否关闭 789 | * Returns whether the menu is alread open 790 | * 791 | * @return 792 | */ 793 | public boolean isOpened() { 794 | return status == STATUS_MENU_OPENED; 795 | } 796 | 797 | public CircleMenu setOnMenuSelectedListener(OnMenuSelectedListener listener) { 798 | this.onMenuSelectedListener = listener; 799 | return this; 800 | } 801 | 802 | public CircleMenu setOnMenuStatusChangeListener(OnMenuStatusChangeListener listener) { 803 | this.onMenuStatusChangeListener = listener; 804 | return this; 805 | } 806 | 807 | private int dip2px(float dpValue) { 808 | final float scale = getContext().getResources().getDisplayMetrics().density; 809 | return (int) (dpValue * scale + 0.5f); 810 | } 811 | } 812 | --------------------------------------------------------------------------------