├── .idea ├── .name ├── copyright │ └── profiles_settings.xml ├── scopes │ └── scope_settings.xml ├── encodings.xml ├── vcs.xml ├── modules.xml ├── misc.xml ├── gradle.xml └── compiler.xml ├── app ├── .gitignore ├── libs │ └── nineoldandroids-2.4.0.jar ├── src │ ├── main │ │ ├── res │ │ │ ├── drawable-hdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-mdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-xhdpi │ │ │ │ └── ic_launcher.png │ │ │ ├── drawable-xxhdpi │ │ │ │ ├── ic_launcher.png │ │ │ │ └── ic_action_new.png │ │ │ ├── values-v21 │ │ │ │ └── styles.xml │ │ │ ├── values │ │ │ │ ├── styles.xml │ │ │ │ ├── dimens.xml │ │ │ │ └── strings.xml │ │ │ ├── menu │ │ │ │ └── menu_main.xml │ │ │ ├── values-w820dp │ │ │ │ └── dimens.xml │ │ │ └── layout │ │ │ │ └── activity_main.xml │ │ ├── AndroidManifest.xml │ │ └── java │ │ │ └── cn │ │ │ └── zmn │ │ │ └── materialbutton │ │ │ ├── RaisedButton.java │ │ │ ├── MainActivity.java │ │ │ ├── FloatingActionButton.java │ │ │ └── FlatButton.java │ └── androidTest │ │ └── java │ │ └── cn │ │ └── zmn │ │ └── materialbutton │ │ └── ApplicationTest.java ├── build.gradle ├── proguard-rules.pro └── app.iml ├── settings.gradle ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── MaterialButton.iml ├── gradle.properties ├── README.md ├── LICENSE ├── gradlew.bat └── gradlew /.idea/.name: -------------------------------------------------------------------------------- 1 | MaterialButton -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhongbaitu/MaterialButton/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /app/libs/nineoldandroids-2.4.0.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhongbaitu/MaterialButton/HEAD/app/libs/nineoldandroids-2.4.0.jar -------------------------------------------------------------------------------- /app/src/main/res/drawable-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhongbaitu/MaterialButton/HEAD/app/src/main/res/drawable-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhongbaitu/MaterialButton/HEAD/app/src/main/res/drawable-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhongbaitu/MaterialButton/HEAD/app/src/main/res/drawable-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhongbaitu/MaterialButton/HEAD/app/src/main/res/drawable-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/drawable-xxhdpi/ic_action_new.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zhongbaitu/MaterialButton/HEAD/app/src/main/res/drawable-xxhdpi/ic_action_new.png -------------------------------------------------------------------------------- /.idea/scopes/scope_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | -------------------------------------------------------------------------------- /app/src/main/res/values-v21/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | MaterialButton 5 | Hello world! 6 | Settings 7 | 8 | 9 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Apr 10 15:27:10 PDT 2013 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.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/menu/menu_main.xml: -------------------------------------------------------------------------------- 1 | 4 | 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /app/src/androidTest/java/cn/zmn/materialbutton/ApplicationTest.java: -------------------------------------------------------------------------------- 1 | package cn.zmn.materialbutton; 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 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | <<<<<<< HEAD 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | ======= 9 | # Built application files 10 | *.apk 11 | *.ap_ 12 | 13 | # Files for the Dalvik VM 14 | *.dex 15 | 16 | # Java class files 17 | *.class 18 | 19 | # Generated files 20 | bin/ 21 | gen/ 22 | 23 | # Gradle files 24 | .gradle/ 25 | build/ 26 | 27 | # Local configuration file (sdk path, etc) 28 | local.properties 29 | 30 | # Proguard folder generated by Eclipse 31 | proguard/ 32 | 33 | # Log Files 34 | *.log 35 | 36 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 7 | 8 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 21 5 | buildToolsVersion "20.0.0" 6 | 7 | defaultConfig { 8 | applicationId "cn.zmn.materialbutton" 9 | minSdkVersion 8 10 | targetSdkVersion 21 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | runProguard false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:appcompat-v7:21.0.0' 25 | compile files('libs/nineoldandroids-2.4.0.jar') 26 | } 27 | -------------------------------------------------------------------------------- /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 D:\software\adt\adt-bundle-windows-x86_64-20140702\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 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /MaterialButton.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /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 -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ##效果: 2 | --- 3 | ![](http://zhongsir.qiniudn.com/2014-11-22/materialButton.gif) 4 | 5 | ##使用: 6 | ----------- 7 | 一共做了三个Button,分别为:`Flat Button` 、 `Raised Button` 、 `Floating Action Button` 8 | 9 | 动画基于`nineoldandroids` 10 | 11 | 其中 `Floating Action Button` 提供两个点击事件,一个为普通的点击事件,一个为带有双向回调函数的点击事件。 12 | 13 | 使用代码见 [Demo](https://github.com/zhongbaitu/MaterialButton/blob/master/app/src/main/java/cn/zmn/materialbutton/MainActivity.java)。 14 | 15 | ##License 16 | 17 | ----------- 18 | 19 | ``` 20 | Copyright 2014 ZhongBaiTu 21 | 22 | Licensed under the Apache License, Version 2.0 (the "License"); 23 | you may not use this file except in compliance with the License. 24 | You may obtain a copy of the License at 25 | 26 | http://www.apache.org/licenses/LICENSE-2.0 27 | 28 | Unless required by applicable law or agreed to in writing, software 29 | distributed under the License is distributed on an "AS IS" BASIS, 30 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 31 | See the License for the specific language governing permissions and 32 | limitations under the License. 33 | ``` 34 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 MaiNengZhong 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /app/src/main/java/cn/zmn/materialbutton/RaisedButton.java: -------------------------------------------------------------------------------- 1 | package cn.zmn.materialbutton; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.util.AttributeSet; 6 | 7 | public class RaisedButton extends FlatButton { 8 | 9 | private final int NORMAL_STATUS = 0; 10 | private final int DISABLE_STATUS = 1; 11 | private final int FOCUS_STATUS = 2; 12 | private final int PRESSED_STATUS = 3; 13 | 14 | public RaisedButton(Context context, AttributeSet attrs, int defStyle) { 15 | super(context, attrs, defStyle); 16 | // TODO Auto-generated constructor stub 17 | } 18 | 19 | public RaisedButton(Context context, AttributeSet attrs) { 20 | super(context, attrs); 21 | // TODO Auto-generated constructor stub 22 | } 23 | 24 | public RaisedButton(Context context) { 25 | super(context); 26 | } 27 | 28 | @Override 29 | protected void onDraw(Canvas canvas) { 30 | bgPaint.setColor(backgroundColor); 31 | switch (status) { 32 | case FOCUS_STATUS: 33 | bgPaint.setAlpha((int) (bgAlpha)); 34 | canvas.drawRoundRect(bgRectF, ROUND_CORNET_RADIUS, ROUND_CORNET_RADIUS, bgPaint); 35 | focusPaint.setAlpha((int) (bgAlpha)); 36 | canvas.drawCircle(width / 2, height / 2, radius, focusPaint); 37 | break; 38 | 39 | case PRESSED_STATUS: 40 | bgPaint.setAlpha((int) (255)); 41 | canvas.drawRoundRect(bgRectF, ROUND_CORNET_RADIUS, ROUND_CORNET_RADIUS, bgPaint); 42 | focusPaint.setAlpha((int) (bgAlpha)); 43 | canvas.drawCircle(width / 2, height / 2, radius, focusPaint); 44 | break; 45 | 46 | default: 47 | bgPaint.setAlpha((int) (bgAlpha)); 48 | canvas.drawRoundRect(bgRectF, ROUND_CORNET_RADIUS, ROUND_CORNET_RADIUS, bgPaint); 49 | break; 50 | } 51 | 52 | super.onDraw(canvas); 53 | } 54 | 55 | private void setRadius(float radius) { 56 | this.radius = radius; 57 | invalidate(); 58 | } 59 | 60 | private float getBgAlpha() { 61 | return bgAlpha; 62 | } 63 | 64 | private void setBgAlpha(float bgAlpha) { 65 | this.bgAlpha = bgAlpha; 66 | invalidate(); 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /app/src/main/java/cn/zmn/materialbutton/MainActivity.java: -------------------------------------------------------------------------------- 1 | package cn.zmn.materialbutton; 2 | 3 | 4 | import android.app.Activity; 5 | import android.os.Bundle; 6 | import android.util.Log; 7 | import android.view.Gravity; 8 | import android.view.View; 9 | import android.view.View.OnClickListener; 10 | import android.widget.LinearLayout; 11 | import android.widget.LinearLayout.LayoutParams; 12 | 13 | public class MainActivity extends Activity { 14 | 15 | private LinearLayout myLayout; 16 | private FlatButton flatButton; 17 | private FlatButton disFlatButton; 18 | private LayoutParams params; 19 | private RaisedButton raisedButton; 20 | private RaisedButton disRaisedButton; 21 | private FloatingActionButton floatButton; 22 | private FloatingActionButton floatButton2; 23 | 24 | @Override 25 | protected void onCreate(Bundle savedInstanceState) { 26 | super.onCreate(savedInstanceState); 27 | myLayout = new LinearLayout(this); 28 | myLayout.setOrientation(LinearLayout.VERTICAL); 29 | myLayout.setGravity(Gravity.CENTER); 30 | setContentView(myLayout); 31 | 32 | params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); 33 | params.setMargins(0,0,0,40); 34 | 35 | flatButton = new FlatButton(this); 36 | flatButton.setText("Flat Button"); 37 | flatButton.setBackgroundColor(0xffFFBB33); 38 | flatButton.setFocusColor(0xffFF8800); 39 | flatButton.setOnClickListener(new OnClickListener() { 40 | 41 | @Override 42 | public void onClick(View v) { 43 | Log.v("test", "click flatButton"); 44 | 45 | } 46 | }); 47 | myLayout.addView(flatButton,params); 48 | 49 | disFlatButton = new FlatButton(this); 50 | disFlatButton.setText("Disable Flat Button"); 51 | disFlatButton.setClickable(false); 52 | myLayout.addView(disFlatButton,params); 53 | 54 | raisedButton = new RaisedButton(this); 55 | raisedButton.setBackgroundColor(0xffAA66CC); 56 | raisedButton.setFocusColor(0xff9933CC); 57 | raisedButton.setText("Raised Button"); 58 | raisedButton.setOnClickListener(new OnClickListener() { 59 | 60 | @Override 61 | public void onClick(View v) { 62 | Log.v("test", "click raisedButton"); 63 | } 64 | }); 65 | myLayout.addView(raisedButton,params); 66 | 67 | disRaisedButton = new RaisedButton(this); 68 | disRaisedButton.setText("Disable Raised Button"); 69 | disRaisedButton.setBackgroundColor(0xffAA66CC); 70 | disRaisedButton.setClickable(false); 71 | myLayout.addView(disRaisedButton, params); 72 | 73 | floatButton = new FloatingActionButton(this); 74 | floatButton.setIcon(R.drawable.ic_action_new); 75 | floatButton.setBackgroundColor(0xff99CC00); 76 | floatButton.setFocusBgcolor(0xff669900); 77 | floatButton.setRotateRepeatReverse(315); 78 | floatButton.setRotateDuration(1000); 79 | floatButton.setOnTwoWayListener(new FloatingActionButton.OnTwoWayListener() { 80 | 81 | @Override 82 | public void onForward() { 83 | Log.v("test", "floatButton forward"); 84 | } 85 | 86 | @Override 87 | public void onBack() { 88 | Log.v("test", "floatButton onBack"); 89 | 90 | } 91 | }); 92 | myLayout.addView(floatButton, params); 93 | 94 | floatButton2 = new FloatingActionButton(this); 95 | floatButton2.setIcon(R.drawable.ic_action_new); 96 | floatButton2.setBackgroundColor(0xff33B5E5); 97 | floatButton2.setFocusBgcolor(0xff0099CC); 98 | floatButton2.setRotate(360); 99 | floatButton2.setRotateDuration(1000); 100 | floatButton2.setOnClickListener(new OnClickListener() { 101 | @Override 102 | public void onClick(View view) { 103 | Log.v("test", "click floatButton2"); 104 | } 105 | }); 106 | myLayout.addView(floatButton2, params); 107 | 108 | } 109 | 110 | } 111 | -------------------------------------------------------------------------------- /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 | # For Cygwin, ensure paths are in UNIX format before anything is touched. 46 | if $cygwin ; then 47 | [ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"` 48 | fi 49 | 50 | # Attempt to set APP_HOME 51 | # Resolve links: $0 may be a link 52 | PRG="$0" 53 | # Need this for relative symlinks. 54 | while [ -h "$PRG" ] ; do 55 | ls=`ls -ld "$PRG"` 56 | link=`expr "$ls" : '.*-> \(.*\)$'` 57 | if expr "$link" : '/.*' > /dev/null; then 58 | PRG="$link" 59 | else 60 | PRG=`dirname "$PRG"`"/$link" 61 | fi 62 | done 63 | SAVED="`pwd`" 64 | cd "`dirname \"$PRG\"`/" >&- 65 | APP_HOME="`pwd -P`" 66 | cd "$SAVED" >&- 67 | 68 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 69 | 70 | # Determine the Java command to use to start the JVM. 71 | if [ -n "$JAVA_HOME" ] ; then 72 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 73 | # IBM's JDK on AIX uses strange locations for the executables 74 | JAVACMD="$JAVA_HOME/jre/sh/java" 75 | else 76 | JAVACMD="$JAVA_HOME/bin/java" 77 | fi 78 | if [ ! -x "$JAVACMD" ] ; then 79 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 80 | 81 | Please set the JAVA_HOME variable in your environment to match the 82 | location of your Java installation." 83 | fi 84 | else 85 | JAVACMD="java" 86 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 87 | 88 | Please set the JAVA_HOME variable in your environment to match the 89 | location of your Java installation." 90 | fi 91 | 92 | # Increase the maximum file descriptors if we can. 93 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 94 | MAX_FD_LIMIT=`ulimit -H -n` 95 | if [ $? -eq 0 ] ; then 96 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 97 | MAX_FD="$MAX_FD_LIMIT" 98 | fi 99 | ulimit -n $MAX_FD 100 | if [ $? -ne 0 ] ; then 101 | warn "Could not set maximum file descriptor limit: $MAX_FD" 102 | fi 103 | else 104 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 105 | fi 106 | fi 107 | 108 | # For Darwin, add options to specify how the application appears in the dock 109 | if $darwin; then 110 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 111 | fi 112 | 113 | # For Cygwin, switch paths to Windows format before running java 114 | if $cygwin ; then 115 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 116 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 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 | -------------------------------------------------------------------------------- /app/src/main/java/cn/zmn/materialbutton/FloatingActionButton.java: -------------------------------------------------------------------------------- 1 | package cn.zmn.materialbutton; 2 | 3 | 4 | import android.content.Context; 5 | import android.graphics.Bitmap; 6 | import android.graphics.BitmapFactory; 7 | import android.graphics.Canvas; 8 | import android.graphics.Paint; 9 | import android.graphics.PaintFlagsDrawFilter; 10 | import android.util.AttributeSet; 11 | import android.view.MotionEvent; 12 | import android.widget.Button; 13 | 14 | import com.nineoldandroids.animation.ObjectAnimator; 15 | 16 | public class FloatingActionButton extends Button { 17 | 18 | private final int NORMAL_STATUS = 0; 19 | private final int DISABLE_STATUS = 1; 20 | private final int FOCUS_STATUS = 2; 21 | private final int PRESSED_STATUS = 3; 22 | private int status = NORMAL_STATUS; 23 | 24 | private Paint defaultPaint; 25 | private Paint focusPaint; 26 | 27 | private int width; 28 | private int height; 29 | 30 | private Bitmap icon; 31 | private int iconWidth; 32 | private int iconHeight; 33 | private int defaultBgColor = 0xff33B5E5; 34 | private int focusBgcolor = 0xff0099CC; 35 | 36 | private int degree; 37 | private int rotateDegree = 0; 38 | private static int NORMAL = 0; 39 | private static int REVERSE = 1; 40 | private int rotateMode = NORMAL; 41 | private int rotateTime = 1000; 42 | private PaintFlagsDrawFilter mDrawFilter; 43 | private ObjectAnimator objectAnimator; 44 | 45 | private OnTwoWayListener listener; 46 | 47 | public FloatingActionButton(Context context, AttributeSet attrs, 48 | int defStyle) { 49 | super(context, attrs, defStyle); 50 | init(); 51 | } 52 | 53 | public FloatingActionButton(Context context, AttributeSet attrs) { 54 | super(context, attrs); 55 | init(); 56 | } 57 | 58 | public FloatingActionButton(Context context) { 59 | super(context); 60 | init(); 61 | } 62 | 63 | private void init() { 64 | defaultPaint = new Paint(); 65 | defaultPaint.setAntiAlias(true); 66 | defaultPaint.setColor(defaultBgColor); 67 | 68 | focusPaint = new Paint(); 69 | focusPaint.setAntiAlias(true); 70 | focusPaint.setColor(focusBgcolor); 71 | 72 | icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_new); 73 | getIconParams(); 74 | 75 | mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); 76 | 77 | this.setBackgroundResource(0); 78 | } 79 | 80 | @Override 81 | public boolean dispatchTouchEvent(MotionEvent event) { 82 | 83 | if (status == DISABLE_STATUS) { 84 | return true; 85 | } 86 | switch (event.getAction()) { 87 | 88 | case MotionEvent.ACTION_DOWN: 89 | status = FOCUS_STATUS; 90 | 91 | break; 92 | 93 | case MotionEvent.ACTION_MOVE: 94 | float x = event.getX(); 95 | float y = event.getY(); 96 | if (x < 0 || x > width || y > height || y < 0) { 97 | status = NORMAL_STATUS; 98 | } 99 | break; 100 | 101 | case MotionEvent.ACTION_CANCEL: 102 | status = NORMAL_STATUS; 103 | break; 104 | 105 | case MotionEvent.ACTION_UP: 106 | if (status != FOCUS_STATUS) { 107 | return true; 108 | } 109 | status = PRESSED_STATUS; 110 | 111 | objectAnimator = ObjectAnimator.ofFloat(this, "rotation", rotateDegree); 112 | objectAnimator.setDuration(rotateTime); 113 | objectAnimator.start(); 114 | 115 | if (rotateMode == REVERSE && rotateDegree != 0) { 116 | rotateDegree -= degree; 117 | if (listener != null) { 118 | listener.onForward(); 119 | } 120 | } else { 121 | rotateDegree += degree; 122 | if (listener != null) { 123 | listener.onBack(); 124 | } 125 | } 126 | 127 | break; 128 | } 129 | invalidate(); 130 | return super.dispatchTouchEvent(event); 131 | } 132 | 133 | @Override 134 | protected void onDraw(Canvas canvas) { 135 | this.setBackgroundResource(0); 136 | canvas.setDrawFilter(mDrawFilter); 137 | switch (status) { 138 | case FOCUS_STATUS: 139 | canvas.drawCircle(width / 2, height / 2, height / 2, focusPaint); 140 | break; 141 | 142 | default: 143 | canvas.drawCircle(width / 2, height / 2, height / 2, defaultPaint); 144 | break; 145 | } 146 | 147 | canvas.drawBitmap(icon, width / 2 - iconWidth / 2, height / 2 148 | - iconHeight / 2, defaultPaint); 149 | super.onDraw(canvas); 150 | } 151 | 152 | public void setIcon(Bitmap icon) { 153 | this.icon = icon; 154 | getIconParams(); 155 | } 156 | 157 | public void setIcon(int resourceID) { 158 | this.icon = BitmapFactory.decodeResource(getResources(), resourceID); 159 | getIconParams(); 160 | } 161 | 162 | private void getIconParams() { 163 | iconWidth = icon.getWidth(); 164 | iconHeight = icon.getHeight(); 165 | } 166 | 167 | @Override 168 | public void setBackgroundColor(int color) { 169 | this.defaultBgColor = color; 170 | defaultPaint.setColor(defaultBgColor); 171 | } 172 | 173 | public void setFocusBgcolor(int focusBgcolor) { 174 | this.focusBgcolor = focusBgcolor; 175 | focusPaint.setColor(focusBgcolor); 176 | } 177 | 178 | public void setRotate(int rotateDegree) { 179 | this.rotateDegree = rotateDegree; 180 | this.degree = rotateDegree; 181 | this.rotateMode = NORMAL; 182 | } 183 | 184 | public void setRotateRepeatReverse(int rotateDegree) { 185 | this.rotateDegree = rotateDegree; 186 | this.degree = rotateDegree; 187 | this.rotateMode = REVERSE; 188 | } 189 | 190 | public void setRotateDuration(int t) { 191 | this.rotateTime = t; 192 | } 193 | 194 | @Override 195 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 196 | super.onLayout(changed, left, top, right, bottom); 197 | 198 | width = this.getWidth(); 199 | height = this.getHeight(); 200 | } 201 | 202 | public void setOnTwoWayListener(OnTwoWayListener listener) { 203 | this.listener = listener; 204 | } 205 | 206 | public interface OnTwoWayListener { 207 | public void onForward(); 208 | public void onBack(); 209 | } 210 | 211 | } 212 | -------------------------------------------------------------------------------- /app/src/main/java/cn/zmn/materialbutton/FlatButton.java: -------------------------------------------------------------------------------- 1 | package cn.zmn.materialbutton; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.Paint; 6 | import android.graphics.PaintFlagsDrawFilter; 7 | import android.graphics.RectF; 8 | import android.util.AttributeSet; 9 | import android.view.MotionEvent; 10 | import android.widget.Button; 11 | 12 | import com.nineoldandroids.animation.Animator; 13 | import com.nineoldandroids.animation.AnimatorSet; 14 | import com.nineoldandroids.animation.ObjectAnimator; 15 | 16 | public class FlatButton extends Button { 17 | 18 | private final int NORMAL_STATUS = 0; 19 | private final int DISABLE_STATUS = 1; 20 | private final int FOCUS_STATUS = 2; 21 | protected int status = NORMAL_STATUS; 22 | 23 | protected final int NORMAL_TEXTCOLOR = 0xff000000; 24 | protected final int DISABLE_TEXTCOLOR = 0xffCCCCCC; 25 | protected int textColor = NORMAL_TEXTCOLOR; 26 | protected int backgroundColor = 0xff999999; 27 | protected int focusColor = 0xff777777; 28 | 29 | protected Paint bgPaint; 30 | protected Paint focusPaint; 31 | 32 | protected int width, height; 33 | 34 | protected float radius = 10; 35 | protected float minRadius; 36 | protected float maxRadius; 37 | 38 | protected ObjectAnimator objectAnimator; 39 | protected AnimatorSet animatorSet; 40 | 41 | protected float bgAlpha = 255; 42 | 43 | protected RectF bgRectF; 44 | 45 | protected final int ROUND_CORNET_RADIUS = 4; 46 | 47 | private PaintFlagsDrawFilter mDrawFilter; 48 | 49 | public FlatButton(Context context, AttributeSet attrs, int defStyle) { 50 | super(context, attrs, defStyle); 51 | init(); 52 | } 53 | 54 | public FlatButton(Context context, AttributeSet attrs) { 55 | super(context, attrs); 56 | init(); 57 | } 58 | 59 | public FlatButton(Context context) { 60 | super(context); 61 | init(); 62 | } 63 | 64 | protected void init() { 65 | bgPaint = new Paint(); 66 | bgPaint.setAntiAlias(true); 67 | bgPaint.setColor(backgroundColor); 68 | 69 | focusPaint = new Paint(); 70 | focusPaint.setAntiAlias(true); 71 | focusPaint.setColor(focusColor); 72 | 73 | animatorSet = new AnimatorSet(); 74 | 75 | bgRectF = new RectF(); 76 | 77 | mDrawFilter = new PaintFlagsDrawFilter(0, Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG); 78 | 79 | this.setBackgroundResource(0); 80 | } 81 | 82 | @Override 83 | public boolean dispatchTouchEvent(MotionEvent event) { 84 | 85 | if (status == DISABLE_STATUS) { 86 | return true; 87 | } 88 | switch (event.getAction()) { 89 | 90 | case MotionEvent.ACTION_DOWN: 91 | status = FOCUS_STATUS; 92 | objectAnimator = ObjectAnimator.ofFloat(this, "radius", minRadius, maxRadius).setDuration(1000); 93 | objectAnimator.setRepeatMode(ObjectAnimator.REVERSE); 94 | objectAnimator.setRepeatCount(Integer.MAX_VALUE); 95 | objectAnimator.start(); 96 | break; 97 | 98 | case MotionEvent.ACTION_MOVE: 99 | float x = event.getX(); 100 | float y = event.getY(); 101 | if (x < 0 || x > width || y > height || y < 0) { 102 | status = NORMAL_STATUS; 103 | objectAnimator.cancel(); 104 | setRadius(0); 105 | } 106 | break; 107 | 108 | case MotionEvent.ACTION_CANCEL: 109 | status = NORMAL_STATUS; 110 | objectAnimator.cancel(); 111 | setRadius(0); 112 | break; 113 | 114 | case MotionEvent.ACTION_UP: 115 | if (status != FOCUS_STATUS) { 116 | return true; 117 | } 118 | objectAnimator.cancel(); 119 | animatorSet.playTogether( 120 | ObjectAnimator.ofFloat(this, "radius", radius, radius * 2), 121 | ObjectAnimator.ofFloat(this, "bgAlpha", 0)); 122 | animatorSet.setDuration(500); 123 | 124 | animatorSet.addListener(new Animator.AnimatorListener() { 125 | 126 | @Override 127 | public void onAnimationStart(Animator arg0) { 128 | // TODO Auto-generated method stub 129 | 130 | } 131 | 132 | @Override 133 | public void onAnimationRepeat(Animator arg0) { 134 | // TODO Auto-generated method stub 135 | 136 | } 137 | 138 | @Override 139 | public void onAnimationEnd(Animator arg0) { 140 | if(status != DISABLE_STATUS){ 141 | status = NORMAL_STATUS; 142 | } 143 | 144 | bgAlpha = 255; 145 | setRadius(0); 146 | } 147 | 148 | @Override 149 | public void onAnimationCancel(Animator arg0) { 150 | // TODO Auto-generated method stub 151 | 152 | } 153 | }); 154 | animatorSet.start(); 155 | break; 156 | } 157 | 158 | return super.dispatchTouchEvent(event); 159 | } 160 | 161 | @Override 162 | protected void onDraw(Canvas canvas) { 163 | this.setBackgroundResource(0); 164 | canvas.setDrawFilter(mDrawFilter); 165 | switch (status) { 166 | case NORMAL_STATUS: 167 | break; 168 | case DISABLE_STATUS: 169 | break; 170 | 171 | default: 172 | bgPaint.setAlpha((int) (bgAlpha)); 173 | canvas.drawRoundRect(bgRectF, ROUND_CORNET_RADIUS, ROUND_CORNET_RADIUS, bgPaint); 174 | focusPaint.setAlpha((int) (bgAlpha)); 175 | canvas.drawCircle(width / 2, height / 2, radius, focusPaint); 176 | break; 177 | } 178 | super.onDraw(canvas); 179 | } 180 | 181 | private void setRadius(float radius) { 182 | this.radius = radius; 183 | invalidate(); 184 | } 185 | 186 | private float getBgAlpha() { 187 | return bgAlpha; 188 | } 189 | 190 | private void setBgAlpha(float bgAlpha) { 191 | this.bgAlpha = bgAlpha; 192 | invalidate(); 193 | } 194 | 195 | @Override 196 | public void setClickable(boolean clickable) { 197 | if (clickable) { 198 | this.textColor = NORMAL_TEXTCOLOR; 199 | } else { 200 | status = DISABLE_STATUS; 201 | this.textColor = DISABLE_TEXTCOLOR; 202 | } 203 | this.setTextColor(textColor); 204 | invalidate(); 205 | } 206 | 207 | @Override 208 | public void setBackgroundColor(int color) { 209 | this.backgroundColor = color; 210 | bgPaint.setColor(backgroundColor); 211 | } 212 | 213 | public void setFocusColor(int focusColor) { 214 | this.focusColor = focusColor; 215 | focusPaint.setColor(focusColor); 216 | } 217 | 218 | @Override 219 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) { 220 | super.onLayout(changed, left, top, right, bottom); 221 | 222 | width = this.getWidth(); 223 | height = this.getHeight(); 224 | if (this.getWidth() > this.getHeight()) { 225 | minRadius = width * 3 / 8; 226 | } else { 227 | minRadius = height * 3 / 8; 228 | } 229 | maxRadius = minRadius + 10; 230 | 231 | bgRectF.left = 0; 232 | bgRectF.top = 0; 233 | bgRectF.right = width; 234 | bgRectF.bottom = height; 235 | } 236 | } 237 | -------------------------------------------------------------------------------- /app/app.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 8 | 9 | 10 | 11 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | --------------------------------------------------------------------------------