├── .gitignore ├── .idea ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── dalong │ │ └── androidrulerview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── dalong │ │ │ └── androidrulerview │ │ │ └── MainActivity.java │ └── res │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ ├── icon_arrow.png │ │ └── icon_arrow_ver.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── dalong │ └── androidrulerview │ └── ExampleUnitTest.java ├── build.gradle ├── gif └── ruler.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── rulerview ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── dalong │ │ └── rulerview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── dalong │ │ │ └── rulerview │ │ │ ├── RulerView.java │ │ │ └── RulerViewScroller.java │ └── res │ │ ├── drawable-xxhdpi │ │ └── ruler_mid_arraw.png │ │ └── values │ │ ├── attrs.xml │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── dalong │ └── rulerview │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/workspace.xml 5 | /.idea/libraries 6 | .DS_Store 7 | /build 8 | /captures 9 | .externalNativeBuild 10 | -------------------------------------------------------------------------------- /.idea/compiler.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 19 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 46 | 47 | 48 | 49 | 50 | 1.8 51 | 52 | 57 | 58 | 59 | 60 | 61 | 62 | -------------------------------------------------------------------------------- /.idea/modules.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # AndroidRulerView 2 | android自定义尺子 3 | #效果 4 | ![image](https://github.com/dalong982242260/AndroidRulerView/blob/master/gif/ruler.gif?raw=true) -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.1" 6 | defaultConfig { 7 | applicationId "com.dalong.androidrulerview" 8 | minSdkVersion 14 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(include: ['*.jar'], dir: 'libs') 24 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 25 | exclude group: 'com.android.support', module: 'support-annotations' 26 | }) 27 | compile 'com.android.support:appcompat-v7:25.1.0' 28 | testCompile 'junit:junit:4.12' 29 | compile project(':rulerview') 30 | } 31 | -------------------------------------------------------------------------------- /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 /android/tools/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 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/dalong/androidrulerview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.dalong.androidrulerview; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.dalong.androidrulerview", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/dalong/androidrulerview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.dalong.androidrulerview; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.util.Log; 6 | import android.view.View; 7 | import android.widget.EditText; 8 | 9 | import com.dalong.rulerview.RulerView; 10 | 11 | public class MainActivity extends AppCompatActivity { 12 | 13 | private RulerView ruler,ruler2,ruler3; 14 | private EditText edit,edit2,edit3; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | 21 | ruler=(RulerView)findViewById(R.id.ruler); 22 | edit=(EditText)findViewById(R.id.edit); 23 | ruler2=(RulerView)findViewById(R.id.ruler2); 24 | edit2=(EditText)findViewById(R.id.edit2); 25 | ruler3=(RulerView)findViewById(R.id.ruler3); 26 | edit3=(EditText)findViewById(R.id.edit3); 27 | ruler.setVisibility(View.GONE); 28 | ruler3.setVisibility(View.GONE); 29 | edit.setVisibility(View.GONE); 30 | edit3.setVisibility(View.GONE); 31 | ruler.setScrollingListener(new RulerView.OnRulerViewScrollListener() { 32 | @Override 33 | public void onChanged(RulerView rulerView, String oldValue, String newValue) { 34 | edit.setText(String.valueOf(newValue)); 35 | } 36 | 37 | @Override 38 | public void onScrollingStarted(RulerView rulerView) { 39 | 40 | } 41 | 42 | @Override 43 | public void onScrollingFinished(RulerView rulerView) { 44 | 45 | } 46 | }); 47 | ruler2.setScrollingListener(new RulerView.OnRulerViewScrollListener() { 48 | @Override 49 | public void onChanged(RulerView rulerView, String oldValue, String newValue) { 50 | edit2.setText(String.valueOf(newValue)); 51 | } 52 | 53 | @Override 54 | public void onScrollingStarted(RulerView rulerView) { 55 | 56 | } 57 | 58 | @Override 59 | public void onScrollingFinished(RulerView rulerView) { 60 | 61 | } 62 | }); 63 | ruler3.setScrollingListener(new RulerView.OnRulerViewScrollListener() { 64 | @Override 65 | public void onChanged(RulerView rulerView, String oldValue, String newValue) { 66 | edit3.setText(String.valueOf(newValue)); 67 | } 68 | 69 | @Override 70 | public void onScrollingStarted(RulerView rulerView) { 71 | 72 | } 73 | 74 | @Override 75 | public void onScrollingFinished(RulerView rulerView) { 76 | 77 | } 78 | }); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 11 | 12 | 20 | 21 | 47 | 48 | 49 | 57 | 58 | 59 | 85 | 86 | 87 | 95 | 96 | 97 | 123 | 124 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yixiaolunhui/AndroidRulerView/b8c23e2c3a5a1a1cb7c5c967256687e4ef637cd1/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yixiaolunhui/AndroidRulerView/b8c23e2c3a5a1a1cb7c5c967256687e4ef637cd1/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yixiaolunhui/AndroidRulerView/b8c23e2c3a5a1a1cb7c5c967256687e4ef637cd1/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/icon_arrow.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yixiaolunhui/AndroidRulerView/b8c23e2c3a5a1a1cb7c5c967256687e4ef637cd1/app/src/main/res/mipmap-xhdpi/icon_arrow.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/icon_arrow_ver.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yixiaolunhui/AndroidRulerView/b8c23e2c3a5a1a1cb7c5c967256687e4ef637cd1/app/src/main/res/mipmap-xhdpi/icon_arrow_ver.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yixiaolunhui/AndroidRulerView/b8c23e2c3a5a1a1cb7c5c967256687e4ef637cd1/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yixiaolunhui/AndroidRulerView/b8c23e2c3a5a1a1cb7c5c967256687e4ef637cd1/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | #666666 7 | 8 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | AndroidRulerView 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/com/dalong/androidrulerview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.dalong.androidrulerview; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.2.3' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } 24 | -------------------------------------------------------------------------------- /gif/ruler.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yixiaolunhui/AndroidRulerView/b8c23e2c3a5a1a1cb7c5c967256687e4ef637cd1/gif/ruler.gif -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | org.gradle.jvmargs=-Xmx1536m 13 | 14 | # When configured, Gradle will run in incubating parallel mode. 15 | # This option should only be used with decoupled projects. More details, visit 16 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 17 | # org.gradle.parallel=true 18 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yixiaolunhui/AndroidRulerView/b8c23e2c3a5a1a1cb7c5c967256687e4ef637cd1/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Mon Dec 28 10:00:20 PST 2015 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip 7 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /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 | -------------------------------------------------------------------------------- /rulerview/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /rulerview/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 25 5 | buildToolsVersion "25.0.1" 6 | 7 | defaultConfig { 8 | minSdkVersion 14 9 | targetSdkVersion 25 10 | versionCode 1 11 | versionName "1.0" 12 | 13 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 14 | 15 | } 16 | buildTypes { 17 | release { 18 | minifyEnabled false 19 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 20 | } 21 | } 22 | } 23 | 24 | dependencies { 25 | compile fileTree(dir: 'libs', include: ['*.jar']) 26 | androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { 27 | exclude group: 'com.android.support', module: 'support-annotations' 28 | }) 29 | compile 'com.android.support:appcompat-v7:25.1.0' 30 | testCompile 'junit:junit:4.12' 31 | } 32 | -------------------------------------------------------------------------------- /rulerview/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 /android/tools/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 | -------------------------------------------------------------------------------- /rulerview/src/androidTest/java/com/dalong/rulerview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.dalong.rulerview; 2 | 3 | import android.content.Context; 4 | import android.support.test.InstrumentationRegistry; 5 | import android.support.test.runner.AndroidJUnit4; 6 | 7 | import org.junit.Test; 8 | import org.junit.runner.RunWith; 9 | 10 | import static org.junit.Assert.*; 11 | 12 | /** 13 | * Instrumentation test, which will execute on an Android device. 14 | * 15 | * @see Testing documentation 16 | */ 17 | @RunWith(AndroidJUnit4.class) 18 | public class ExampleInstrumentedTest { 19 | @Test 20 | public void useAppContext() throws Exception { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.dalong.rulerview.test", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /rulerview/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 5 | 6 | 7 | 8 | 9 | -------------------------------------------------------------------------------- /rulerview/src/main/java/com/dalong/rulerview/RulerView.java: -------------------------------------------------------------------------------- 1 | package com.dalong.rulerview; 2 | 3 | import android.content.Context; 4 | import android.content.res.TypedArray; 5 | import android.graphics.Bitmap; 6 | import android.graphics.BitmapFactory; 7 | import android.graphics.Canvas; 8 | import android.graphics.Color; 9 | import android.graphics.Paint; 10 | import android.text.Layout; 11 | import android.text.TextPaint; 12 | import android.util.AttributeSet; 13 | import android.view.MotionEvent; 14 | import android.view.View; 15 | 16 | /** 17 | * 刻度尺 18 | * Created by dalong on 2016/12/15. 19 | */ 20 | 21 | public class RulerView extends View { 22 | private static final String TAG="RulerView"; 23 | // 默认刻度模式 24 | public static final int MOD_TYPE_SCALE = 5; 25 | //刻度基数 每个刻度代表多少 默认为1 26 | public int mScaleBase=1; 27 | //最大刻度的颜色 28 | public int mMaxScaleColor; 29 | //中间刻度的颜色 30 | public int mMidScaleColor; 31 | //最小刻度的颜色 32 | public int mMinScaleColor; 33 | //底部线的颜色 34 | public int mBottomLineColor; 35 | //最大刻度的宽度 36 | public float mMaxScaleWidth; 37 | //中间刻度的宽度 38 | public float mMidScaleWidth; 39 | //最小刻度的宽度 40 | public float mMinScaleWidth; 41 | //底线的宽度 42 | public float mBottomLineWidth; 43 | //最大刻度的高度占控件的高度比例 44 | public float mMaxScaleHeightRatio; 45 | //中间刻度的高度占控件的高度比例 46 | public float mMidScaleHeightRatio; 47 | //最小刻度的高度占控件的高度比例 48 | public float mMinScaleHeightRatio; 49 | //是否显示刻度值 50 | public boolean isShowScaleValue; 51 | //是否刻度渐变 52 | public boolean isScaleGradient; 53 | //刻度值颜色 54 | public int mScaleValueColor; 55 | //刻度值文字大小 56 | public float mScaleValueSize; 57 | //当前值 58 | public int mCurrentValue; 59 | //最大值 60 | public int mMaxValue; 61 | //最小值 62 | public int mMinValue; 63 | //中间图片 64 | private Bitmap mMiddleImg; 65 | //刻度线画笔 66 | private Paint mScalePaint; 67 | // 刻度值画笔 68 | private TextPaint mScaleValuePaint; 69 | //中间图片画笔 70 | private Paint mMiddleImgPaint; 71 | private float mTpDesiredWidth; 72 | //最大刻度高度 73 | private int mMaxScaleHeight; 74 | //中间刻度高度 75 | private int mMidScaleHeight; 76 | //最小刻度高度 77 | private int mMinScaleHeight; 78 | // 滚动偏移量 79 | private int scrollingOffset; 80 | //间隔S 81 | private int mScaleSpace=20; 82 | // 滚动器 83 | private RulerViewScroller scroller; 84 | // 是否执行滚动 85 | private boolean isScrollingPerformed; 86 | 87 | public RulerView(Context context) { 88 | this(context,null); 89 | } 90 | 91 | public RulerView(Context context, AttributeSet attrs) { 92 | this(context, attrs,0); 93 | } 94 | 95 | public RulerView(Context context, AttributeSet attrs, int defStyleAttr) { 96 | super(context, attrs, defStyleAttr); 97 | 98 | TypedArray typedArray=context.obtainStyledAttributes(attrs,R.styleable.RulerView); 99 | 100 | mMaxScaleColor=typedArray.getColor(R.styleable.RulerView_mMaxScaleColor, Color.BLACK); 101 | mMidScaleColor=typedArray.getColor(R.styleable.RulerView_mMidScaleColor, Color.BLACK); 102 | mMinScaleColor=typedArray.getColor(R.styleable.RulerView_mMinScaleColor, Color.BLACK); 103 | mMaxScaleColor=typedArray.getColor(R.styleable.RulerView_mMaxScaleColor, Color.BLACK); 104 | mScaleValueColor=typedArray.getColor(R.styleable.RulerView_mScaleValueColor, Color.BLACK); 105 | mBottomLineColor=typedArray.getColor(R.styleable.RulerView_mBottomLineColor, Color.BLACK); 106 | 107 | mMaxScaleWidth = typedArray.getDimensionPixelSize(R.styleable.RulerView_mMaxScaleWidth, 15); 108 | mMidScaleWidth = typedArray.getDimensionPixelSize(R.styleable.RulerView_mMidScaleWidth, 12); 109 | mMinScaleWidth = typedArray.getDimensionPixelSize(R.styleable.RulerView_mMinScaleWidth, 10); 110 | mBottomLineWidth = typedArray.getDimensionPixelSize(R.styleable.RulerView_mBottomLineWidth, 15); 111 | mScaleValueSize = typedArray.getDimensionPixelSize(R.styleable.RulerView_mScaleValueSize, 12); 112 | mScaleSpace = typedArray.getDimensionPixelSize(R.styleable.RulerView_mScaleSpace, 20); 113 | 114 | mMaxScaleHeightRatio = typedArray.getFloat(R.styleable.RulerView_mMaxScaleHeightRatio, 0.3f); 115 | mMidScaleHeightRatio = typedArray.getFloat(R.styleable.RulerView_mMidScaleHeightRatio, 0.2f); 116 | mMinScaleHeightRatio = typedArray.getFloat(R.styleable.RulerView_mMinScaleHeightRatio, 0.1f); 117 | 118 | isShowScaleValue = typedArray.getBoolean(R.styleable.RulerView_isShowScaleValue, true); 119 | isScaleGradient = typedArray.getBoolean(R.styleable.RulerView_isScaleGradient, true); 120 | 121 | mMaxValue = typedArray.getInteger(R.styleable.RulerView_mMaxValue, 100); 122 | mMinValue = typedArray.getInteger(R.styleable.RulerView_mMinValue, 0); 123 | mScaleBase = typedArray.getInteger(R.styleable.RulerView_mScaleBase, 1); 124 | mCurrentValue = typedArray.getInteger(R.styleable.RulerView_mCurrentValue, 0); 125 | setCurrentValue(mCurrentValue); 126 | 127 | mMiddleImg = BitmapFactory.decodeResource(getResources(), 128 | typedArray.getResourceId(R.styleable.RulerView_mMiddleImg,R.drawable.ruler_mid_arraw)); 129 | typedArray.recycle(); 130 | 131 | mScalePaint=new Paint(Paint.ANTI_ALIAS_FLAG); 132 | mScalePaint.setStyle(Paint.Style.STROKE); 133 | mScalePaint.setAntiAlias(true); 134 | 135 | mScaleValuePaint=new TextPaint(Paint.ANTI_ALIAS_FLAG); 136 | mScaleValuePaint.setColor(mScaleValueColor); 137 | mScaleValuePaint.setTextSize(mScaleValueSize); 138 | mScaleValuePaint.setTextAlign(Paint.Align.CENTER); 139 | mTpDesiredWidth = Layout.getDesiredWidth("0", mScaleValuePaint); 140 | 141 | mMiddleImgPaint=new Paint(Paint.ANTI_ALIAS_FLAG); 142 | mMiddleImgPaint.setStyle(Paint.Style.STROKE); 143 | mMiddleImgPaint.setAntiAlias(true); 144 | 145 | scroller=new RulerViewScroller(context,scrollingListener); 146 | } 147 | 148 | @Override 149 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 150 | setMeasuredDimension(measureWidthSize(widthMeasureSpec),measureHeightSize(heightMeasureSpec)); 151 | } 152 | 153 | private int measureHeightSize(int heightMeasureSpec) { 154 | int result; 155 | int mode=MeasureSpec.getMode(heightMeasureSpec); 156 | int size=MeasureSpec.getSize(heightMeasureSpec); 157 | if(mode==MeasureSpec.EXACTLY){ 158 | result=size; 159 | }else{ 160 | result=(int) (mMiddleImg.getHeight() + getPaddingTop() + getPaddingBottom() + 2 * mScaleValuePaint.getTextSize()); 161 | if(mode==MeasureSpec.AT_MOST){ 162 | result=Math.min(result,size); 163 | } 164 | } 165 | return result; 166 | } 167 | 168 | private int measureWidthSize(int widthMeasureSpec) { 169 | int result; 170 | int mode=MeasureSpec.getMode(widthMeasureSpec); 171 | int size=MeasureSpec.getSize(widthMeasureSpec); 172 | if(mode==MeasureSpec.EXACTLY){ 173 | result=size; 174 | }else{ 175 | result=400; 176 | if(mode==MeasureSpec.AT_MOST){ 177 | result=Math.min(result,size); 178 | } 179 | } 180 | return result; 181 | } 182 | 183 | @Override 184 | protected void onSizeChanged(int w, int h, int oldw, int oldh) { 185 | super.onSizeChanged(w, h, oldw, oldh); 186 | if (w == 0 || h == 0) 187 | return; 188 | /** 189 | * 在这里根据控件高度设置三中刻度线的高度 190 | */ 191 | int mHeight = h - getPaddingTop() - getPaddingBottom(); 192 | mMaxScaleHeight = (int) (mHeight*mMaxScaleHeightRatio); 193 | mMidScaleHeight = (int) (mHeight*mMidScaleHeightRatio); 194 | mMinScaleHeight = (int) (mHeight*mMinScaleHeightRatio); 195 | } 196 | 197 | /** 198 | * 绘制图像 199 | * @param canvas 200 | */ 201 | @Override 202 | protected void onDraw(Canvas canvas) { 203 | super.onDraw(canvas); 204 | int mDrawWidth=getMeasuredWidth()-getPaddingLeft()-getPaddingRight(); 205 | int mDrawHeight=getMeasuredHeight()-getPaddingTop()-getPaddingBottom(); 206 | 207 | //绘制刻度线 208 | drawScaleLine(canvas,mDrawWidth,mDrawHeight); 209 | //绘制中间图片 210 | drawMiddleImg(canvas,mDrawWidth,mDrawHeight); 211 | } 212 | 213 | /** 214 | * 绘制中间图片 215 | * @param canvas 216 | * @param mDrawWidth 217 | * @param mDrawHeight 218 | */ 219 | private void drawMiddleImg(Canvas canvas, int mDrawWidth, int mDrawHeight) { 220 | int left = (mDrawWidth - mMiddleImg.getWidth()) / 2; 221 | int top = (int) (mScaleValuePaint.getTextSize() / 2); 222 | canvas.drawBitmap(mMiddleImg, left, top, mMiddleImgPaint); 223 | } 224 | 225 | /** 226 | * 绘制刻度线 227 | * @param canvas 228 | * @param mDrawWidth 229 | * @param mDrawHeight 230 | */ 231 | private void drawScaleLine(Canvas canvas, int mDrawWidth, int mDrawHeight) { 232 | int scaleNum= (int) (Math.ceil(mDrawWidth/2f/mScaleSpace))+2; 233 | int distanceX = scrollingOffset; 234 | int currValue = mCurrentValue; 235 | drawScaleLine(canvas,scaleNum,distanceX,currValue,mDrawWidth,mDrawHeight); 236 | } 237 | 238 | /** 239 | * 绘制刻度线 240 | * @param canvas 241 | * @param scaleNum 242 | * @param distanceX 243 | * @param currValue 244 | * @param mDrawWidth 245 | * @param mDrawHeight 246 | */ 247 | private void drawScaleLine(Canvas canvas, int scaleNum, int distanceX, int currValue, int mDrawWidth, int mDrawHeight) { 248 | int dy = (int) (mDrawHeight - mTpDesiredWidth - mScaleValuePaint.getTextSize()) - getPaddingBottom(); 249 | int value; 250 | float xPosition; 251 | for (int i=0;i=(mMinValue/mScaleBase)&&value<=(mMaxValue/mScaleBase)){ 256 | drawScaleLine(canvas, value, xPosition, dy, scaleNum, i, mDrawHeight); 257 | } 258 | //绘制右面线 259 | if(value<(mMaxValue/mScaleBase)&&value>=(mMinValue/mScaleBase)) 260 | drawBottomLine(canvas,getAlpha(scaleNum, i),xPosition-mMaxScaleWidth/2, dy, xPosition+mScaleSpace+mMaxScaleWidth/2, dy); 261 | 262 | //左面 263 | xPosition=mDrawWidth/2f-i*mScaleSpace+distanceX; 264 | value=currValue-i; 265 | if(xPosition>getPaddingLeft() && value>=(mMinValue/mScaleBase)&&value<=(mMaxValue/mScaleBase)){ 266 | drawScaleLine( canvas, value, xPosition, dy, scaleNum, i, mDrawHeight); 267 | } 268 | //绘制左面线 269 | if(value>=(mMinValue/mScaleBase) && value<(mMaxValue/mScaleBase)) 270 | drawBottomLine(canvas,getAlpha(scaleNum, i),xPosition-mMaxScaleWidth/2, dy, xPosition+mScaleSpace+mMaxScaleWidth/2, dy); 271 | } 272 | } 273 | 274 | /** 275 | * 绘制底部线 276 | * @param canvas 277 | * @param alpha 278 | * @param sx 279 | * @param sy 280 | * @param ex 281 | * @param ey 282 | */ 283 | private void drawBottomLine(Canvas canvas,int alpha,float sx,float sy,float ex,float ey){ 284 | mScalePaint.setColor(mBottomLineColor); 285 | mScalePaint.setStrokeWidth(mBottomLineWidth); 286 | mScalePaint.setAlpha(alpha); 287 | canvas.drawLine(sx, sy, ex, ey, mScalePaint); 288 | } 289 | /** 290 | * 绘制刻度尺 左 右 291 | * @param canvas 292 | * @param value 293 | * @param xPosition 294 | * @param dy 295 | * @param scaleNum 296 | * @param i 297 | * @param mDrawHeight 298 | */ 299 | public void drawScaleLine(Canvas canvas,int value, float xPosition,int dy,int scaleNum,int i,int mDrawHeight){ 300 | if (value % MOD_TYPE_SCALE == 0) { 301 | if(value % (MOD_TYPE_SCALE*2)==0){//大刻度 302 | drawScaleLine(canvas,mMaxScaleWidth,mMaxScaleColor,getAlpha(scaleNum, i), 303 | xPosition,dy,xPosition,dy - mMaxScaleHeight); 304 | if (isShowScaleValue) { 305 | mScaleValuePaint.setAlpha(getAlpha(scaleNum, i)); 306 | canvas.drawText(String.valueOf(value*mScaleBase), xPosition, mDrawHeight - mTpDesiredWidth, mScaleValuePaint); 307 | } 308 | }else{//中刻度 309 | drawScaleLine(canvas,mMidScaleWidth,mMidScaleColor,getAlpha(scaleNum, i), 310 | xPosition,dy,xPosition,dy-mMidScaleHeight); 311 | } 312 | }else{// 小刻度 313 | drawScaleLine(canvas,mMinScaleWidth,mMinScaleColor,getAlpha(scaleNum, i), 314 | xPosition,dy,xPosition,dy-mMinScaleHeight); 315 | } 316 | 317 | } 318 | /** 319 | * 绘制刻度尺刻度 320 | * @param canvas 321 | * @param strokeWidth 322 | * @param scaleColor 323 | * @param alpha 324 | * @param sx 325 | * @param sy 326 | * @param ex 327 | * @param ey 328 | */ 329 | private void drawScaleLine(Canvas canvas,float strokeWidth,int scaleColor,int alpha,float sx,float sy,float ex,float ey){ 330 | mScalePaint.setStrokeWidth(strokeWidth); 331 | mScalePaint.setColor(scaleColor); 332 | mScalePaint.setAlpha(alpha); 333 | canvas.drawLine(sx, sy, ex, ey, mScalePaint); 334 | } 335 | 336 | 337 | 338 | /** 339 | * 获取透明度 通过当前index占总共数量的count的比例来设置透明度 340 | * @param halfCount 341 | * @param index 342 | * @return 343 | */ 344 | private int getAlpha(int halfCount, int index) { 345 | if (isScaleGradient) { 346 | int MAX_ALPHA_VALUE = 255; 347 | return MAX_ALPHA_VALUE / halfCount * (halfCount - index); 348 | } else { 349 | return 255; 350 | } 351 | } 352 | 353 | 354 | /** 355 | * 设置最小值 356 | * 357 | * @param mMinValue 358 | */ 359 | public void setMinValue(int mMinValue) { 360 | if (mMinValue / mScaleBase < 1) { 361 | mMinValue = 0; 362 | } 363 | this.mMinValue = mMinValue; 364 | setCurrentValue(mCurrentValue); 365 | invalidate(); 366 | } 367 | 368 | /** 369 | * 设置最大值 370 | * @param mMaxValue 371 | */ 372 | public void setMaxValue(int mMaxValue) { 373 | if (mMaxValue > 0) { 374 | this.mMaxValue = mMaxValue; 375 | setCurrentValue(mCurrentValue); 376 | } 377 | invalidate(); 378 | } 379 | 380 | 381 | /** 382 | * 获取当前值 383 | * 384 | * @return 385 | */ 386 | public int getCurrentValue() { 387 | return Math.min(Math.max(0, mCurrentValue*mScaleBase), mMaxValue); 388 | } 389 | 390 | /** 391 | * 设置当前值 392 | * @param currentValue 393 | */ 394 | public void setCurrentValue(int currentValue) { 395 | if (currentValue < 0) { 396 | currentValue = 0; 397 | } 398 | if(currentValue<=mMinValue){ 399 | currentValue=mMinValue; 400 | } 401 | if(currentValue>=mMaxValue){ 402 | currentValue=mMaxValue; 403 | } 404 | this.mCurrentValue=currentValue/mScaleBase; 405 | invalidate(); 406 | } 407 | /***********************************************以上是绘制部分**********************************************/ 408 | 409 | 410 | /** 411 | * 滚动回调接口 412 | */ 413 | RulerViewScroller.ScrollingListener scrollingListener = new RulerViewScroller.ScrollingListener() { 414 | 415 | /** 416 | * 滚动开始 417 | */ 418 | @Override 419 | public void onStarted() { 420 | isScrollingPerformed = true; 421 | //滚动开始 422 | if (null != onWheelListener) { 423 | onWheelListener.onScrollingStarted(RulerView.this); 424 | } 425 | } 426 | 427 | /** 428 | * 滚动中 429 | * @param distance 滚动的距离 430 | */ 431 | @Override 432 | public void onScroll(int distance) { 433 | doScroll(distance); 434 | } 435 | 436 | /** 437 | * 滚动结束 438 | */ 439 | @Override 440 | public void onFinished() { 441 | if (outOfRange()) { 442 | return; 443 | } 444 | if (isScrollingPerformed) { 445 | //滚动结束 446 | if (null != onWheelListener) { 447 | onWheelListener.onScrollingFinished(RulerView.this); 448 | } 449 | isScrollingPerformed = false; 450 | } 451 | scrollingOffset = 0; 452 | invalidate(); 453 | } 454 | 455 | /** 456 | * 验证滚动是否在正确位置 457 | */ 458 | @Override 459 | public void onJustify() { 460 | if (outOfRange()) { 461 | return; 462 | } 463 | if (Math.abs(scrollingOffset) > RulerViewScroller.MIN_DELTA_FOR_SCROLLING) { 464 | if (scrollingOffset < -mScaleSpace / 2) { 465 | scroller.scroll(mScaleSpace + scrollingOffset, 0); 466 | } else if (scrollingOffset > mScaleSpace / 2) { 467 | scroller.scroll(scrollingOffset - mScaleSpace, 0); 468 | } else { 469 | scroller.scroll(scrollingOffset, 0); 470 | } 471 | } 472 | } 473 | }; 474 | 475 | 476 | 477 | /** 478 | * 超出左右范围 479 | * @return 480 | */ 481 | private boolean outOfRange() { 482 | //这个是越界后需要回滚的大小值 483 | int outRange = 0; 484 | if (mCurrentValue < mMinValue/mScaleBase) { 485 | outRange = (mCurrentValue - mMinValue/mScaleBase) * mScaleSpace; 486 | } else if (mCurrentValue > mMaxValue/mScaleBase) { 487 | outRange = (mCurrentValue - mMaxValue/mScaleBase) * mScaleSpace; 488 | } 489 | if (0 != outRange) { 490 | scrollingOffset = 0; 491 | scroller.scroll(-outRange, 100); 492 | return true; 493 | } 494 | return false; 495 | } 496 | 497 | /** 498 | * 滚动中回调最新值 499 | * @param delta 500 | */ 501 | private void doScroll(int delta) { 502 | scrollingOffset += delta; 503 | int offsetCount = scrollingOffset / mScaleSpace; 504 | if (0 != offsetCount) { 505 | // 显示在范围内 506 | int oldValueIndex = Math.min(Math.max(mMinValue, mCurrentValue*mScaleBase), mMaxValue); 507 | mCurrentValue -= offsetCount; 508 | scrollingOffset -= offsetCount * mScaleSpace; 509 | if (null != onWheelListener) { 510 | //回调通知最新的值 511 | int valueIndex = Math.min(Math.max(mMinValue, mCurrentValue*mScaleBase), mMaxValue); 512 | onWheelListener.onChanged(this, oldValueIndex + "",valueIndex+""); 513 | } 514 | } 515 | invalidate(); 516 | } 517 | 518 | private float mDownFocusX; 519 | private float mDownFocusY; 520 | private boolean isDisallowIntercept; 521 | @Override 522 | public boolean onTouchEvent(MotionEvent event) { 523 | if (!isEnabled()) { 524 | return true; 525 | } 526 | switch (event.getAction()) { 527 | case MotionEvent.ACTION_DOWN: 528 | mDownFocusX = event.getX(); 529 | mDownFocusY = event.getY(); 530 | break; 531 | case MotionEvent.ACTION_MOVE: 532 | if (!isDisallowIntercept && Math.abs(event.getY() - mDownFocusY) < Math.abs(event.getX() - mDownFocusX)) { 533 | isDisallowIntercept = true; 534 | if (getParent() != null) { 535 | getParent().requestDisallowInterceptTouchEvent(true); 536 | } 537 | } 538 | break; 539 | case MotionEvent.ACTION_UP: 540 | case MotionEvent.ACTION_CANCEL: 541 | if (getParent() != null) { 542 | getParent().requestDisallowInterceptTouchEvent(false); 543 | } 544 | isDisallowIntercept = false; 545 | break; 546 | } 547 | return scroller.onTouchEvent(event); 548 | } 549 | 550 | private OnRulerViewScrollListener onWheelListener; 551 | 552 | /** 553 | * 添加滚动回调 554 | * @param listener the listener 555 | */ 556 | public void setScrollingListener(OnRulerViewScrollListener listener) { 557 | onWheelListener = listener; 558 | } 559 | 560 | 561 | public interface OnRulerViewScrollListener { 562 | /** 563 | * 当更改选择的时候回调方法 564 | * @param rulerView 状态更改的view 565 | * @param oldValue 当前item的旧值 566 | * @param newValue 当前item的新值 567 | */ 568 | void onChanged(RulerView rulerView, T oldValue, T newValue); 569 | 570 | /** 571 | * 滚动启动时调用的回调方法 572 | * @param rulerView 573 | */ 574 | void onScrollingStarted(RulerView rulerView); 575 | 576 | /** 577 | * 滚动结束时调用的回调方法 578 | * @param rulerView 579 | */ 580 | void onScrollingFinished(RulerView rulerView); 581 | } 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | } 606 | -------------------------------------------------------------------------------- /rulerview/src/main/java/com/dalong/rulerview/RulerViewScroller.java: -------------------------------------------------------------------------------- 1 | package com.dalong.rulerview; 2 | 3 | import android.content.Context; 4 | import android.os.Handler; 5 | import android.os.Message; 6 | import android.view.GestureDetector; 7 | import android.view.MotionEvent; 8 | import android.widget.Scroller; 9 | 10 | /** 11 | * Created by dalong on 2016/12/15. 12 | */ 13 | 14 | public class RulerViewScroller { 15 | 16 | //滚动的时间 17 | public static final int SCROLLING_DURATION = 400; 18 | 19 | //用于滚动的最小增量 20 | public static final int MIN_DELTA_FOR_SCROLLING = 1; 21 | 22 | //Listener 23 | private ScrollingListener listener; 24 | 25 | //上下文 26 | private Context context; 27 | 28 | // Scrolling 29 | private GestureDetector gestureDetector; 30 | private Scroller scroller; 31 | private int lastScrollX; 32 | private float lastTouchedX; 33 | private boolean isScrollingPerformed; 34 | 35 | private final int MESSAGE_SCROLL = 0; 36 | private final int MESSAGE_JUSTIFY = 1; 37 | 38 | 39 | public RulerViewScroller(Context context, ScrollingListener listener) { 40 | this.listener = listener; 41 | this.context = context; 42 | gestureDetector = new GestureDetector(context, gestureListener); 43 | gestureDetector.setIsLongpressEnabled(false); 44 | scroller = new Scroller(context); 45 | scroller.setFriction(0.05f); 46 | 47 | } 48 | 49 | 50 | /** 51 | * 手势监听 52 | */ 53 | private GestureDetector.SimpleOnGestureListener gestureListener = new GestureDetector.SimpleOnGestureListener() { 54 | public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 55 | return true; 56 | } 57 | 58 | public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 59 | lastScrollX = 0; 60 | scroller.fling(0, lastScrollX, (int) -velocityX, 0, -0x7FFFFFFF, 0x7FFFFFFF, 0, 0); 61 | setNextMessage(MESSAGE_SCROLL); 62 | return true; 63 | } 64 | }; 65 | 66 | 67 | /** 68 | * 手势处理 69 | * @param event 70 | * @return 71 | */ 72 | public boolean onTouchEvent(MotionEvent event) { 73 | switch (event.getAction()) { 74 | case MotionEvent.ACTION_DOWN: 75 | lastTouchedX = event.getX(); 76 | scroller.forceFinished(true); 77 | clearMessages(); 78 | break; 79 | 80 | case MotionEvent.ACTION_MOVE: 81 | int distanceX = (int) (event.getX() - lastTouchedX); 82 | if (distanceX != 0) { 83 | startScrolling(); 84 | listener.onScroll(distanceX); 85 | lastTouchedX = event.getX(); 86 | } 87 | break; 88 | } 89 | //当手指离开控件时 90 | if (!gestureDetector.onTouchEvent(event) && event.getAction() == MotionEvent.ACTION_UP) { 91 | justify(); 92 | } 93 | 94 | return true; 95 | } 96 | /** 97 | * 发送下一步消息,清楚之前的消息 98 | * @param message 99 | */ 100 | private void setNextMessage(int message) { 101 | clearMessages(); 102 | animationHandler.sendEmptyMessage(message); 103 | } 104 | 105 | /** 106 | * 清楚所有的what的消息列表 107 | */ 108 | private void clearMessages() { 109 | animationHandler.removeMessages(MESSAGE_SCROLL); 110 | animationHandler.removeMessages(MESSAGE_JUSTIFY); 111 | } 112 | 113 | 114 | /** 115 | * 滚动 116 | * @param distance 距离 117 | * @param time 时间 118 | */ 119 | public void scroll(int distance, int time) { 120 | scroller.forceFinished(true); 121 | lastScrollX = 0; 122 | scroller.startScroll(0, 0, distance, 0, time != 0 ? time : SCROLLING_DURATION); 123 | setNextMessage(MESSAGE_SCROLL); 124 | startScrolling(); 125 | } 126 | 127 | /** 128 | * 动画处理handler 129 | */ 130 | private Handler animationHandler = new Handler(new Handler.Callback() { 131 | @Override 132 | public boolean handleMessage(Message msg) { 133 | scroller.computeScrollOffset(); 134 | int currX = scroller.getCurrX(); 135 | int delta = lastScrollX - currX; 136 | lastScrollX = currX; 137 | if (delta != 0) { 138 | listener.onScroll(delta); 139 | } 140 | // 滚动是不是完成时,涉及到最终Y,所以手动完成 141 | if (Math.abs(currX - scroller.getFinalX()) < MIN_DELTA_FOR_SCROLLING) { 142 | lastScrollX = scroller.getFinalX(); 143 | scroller.forceFinished(true); 144 | } 145 | if (!scroller.isFinished()) { 146 | animationHandler.sendEmptyMessage(msg.what); 147 | } else if (msg.what == MESSAGE_SCROLL) { 148 | justify(); 149 | } else { 150 | finishScrolling(); 151 | } 152 | return true; 153 | } 154 | }); 155 | 156 | 157 | /** 158 | * 滚动停止时待校验 159 | */ 160 | private void justify() { 161 | listener.onJustify(); 162 | setNextMessage(MESSAGE_JUSTIFY); 163 | } 164 | 165 | /** 166 | * 开始滚动 167 | */ 168 | private void startScrolling() { 169 | if (!isScrollingPerformed) { 170 | isScrollingPerformed = true; 171 | listener.onStarted(); 172 | } 173 | } 174 | 175 | /** 176 | * 滚动结束 177 | */ 178 | void finishScrolling() { 179 | if (isScrollingPerformed) { 180 | listener.onFinished(); 181 | isScrollingPerformed = false; 182 | } 183 | } 184 | 185 | /** 186 | * 滚动监听器接口 187 | */ 188 | public interface ScrollingListener { 189 | /** 190 | * 正在滚动中回调 191 | * @param distance 滚动的距离 192 | */ 193 | void onScroll(int distance); 194 | 195 | /** 196 | * 启动滚动时调用的回调函数 197 | */ 198 | void onStarted(); 199 | 200 | /** 201 | * 校验完成后 执行完毕后回调 202 | */ 203 | void onFinished(); 204 | 205 | /** 206 | * 滚动停止时待校验 207 | */ 208 | void onJustify(); 209 | } 210 | } 211 | -------------------------------------------------------------------------------- /rulerview/src/main/res/drawable-xxhdpi/ruler_mid_arraw.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/yixiaolunhui/AndroidRulerView/b8c23e2c3a5a1a1cb7c5c967256687e4ef637cd1/rulerview/src/main/res/drawable-xxhdpi/ruler_mid_arraw.png -------------------------------------------------------------------------------- /rulerview/src/main/res/values/attrs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | -------------------------------------------------------------------------------- /rulerview/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | rulerView 3 | 4 | -------------------------------------------------------------------------------- /rulerview/src/test/java/com/dalong/rulerview/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package com.dalong.rulerview; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * Example local unit test, which will execute on the development machine (host). 9 | * 10 | * @see Testing documentation 11 | */ 12 | public class ExampleUnitTest { 13 | @Test 14 | public void addition_isCorrect() throws Exception { 15 | assertEquals(4, 2 + 2); 16 | } 17 | } -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':rulerview' 2 | --------------------------------------------------------------------------------