├── .gitignore ├── .idea ├── caches │ └── build_file_checksums.ser ├── codeStyles │ └── Project.xml ├── gradle.xml ├── inspectionProfiles │ └── Project_Default.xml ├── misc.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zjun │ │ └── demo │ │ └── ruleview │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zjun │ │ │ └── demo │ │ │ └── ruleview │ │ │ └── MainActivity.java │ └── res │ │ ├── drawable-v24 │ │ └── ic_launcher_foreground.xml │ │ ├── drawable │ │ ├── ic_launcher_background.xml │ │ └── shape_time_rule_view_bg.xml │ │ ├── layout │ │ └── activity_main.xml │ │ ├── mipmap-anydpi-v26 │ │ ├── ic_launcher.xml │ │ └── ic_launcher_round.xml │ │ ├── mipmap-hdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-mdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ ├── mipmap-xxxhdpi │ │ ├── ic_launcher.png │ │ └── ic_launcher_round.png │ │ └── values │ │ ├── colors.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── com │ └── zjun │ └── demo │ └── ruleview │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat ├── img ├── all.gif ├── money.gif ├── ruleView.gif └── time.gif ├── rule-view ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── androidTest │ └── java │ │ └── com │ │ └── zjun │ │ └── widget │ │ └── ExampleInstrumentedTest.java │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── com │ │ │ └── zjun │ │ │ └── widget │ │ │ ├── MoneySelectRuleView.java │ │ │ ├── RuleView.java │ │ │ └── TimeRuleView.java │ └── res │ │ └── values │ │ ├── attrs.xml │ │ └── strings.xml │ └── test │ └── java │ └── com │ └── zjun │ └── widget │ └── ExampleUnitTest.java └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/libraries 5 | /.idea/modules.xml 6 | /.idea/workspace.xml 7 | .DS_Store 8 | /build 9 | /captures 10 | .externalNativeBuild 11 | -------------------------------------------------------------------------------- /.idea/caches/build_file_checksums.ser: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/zjun615/RulerView/62273961c51dca1cb1f6eca699e3acb1ee352f29/.idea/caches/build_file_checksums.ser -------------------------------------------------------------------------------- /.idea/codeStyles/Project.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 15 | 16 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 18 | 19 | -------------------------------------------------------------------------------- /.idea/inspectionProfiles/Project_Default.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 36 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 17 | 27 | 28 | 29 | 30 | 31 | 32 | 34 | -------------------------------------------------------------------------------- /.idea/runConfigurations.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 11 | 12 | -------------------------------------------------------------------------------- /.idea/vcs.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # RuleView 2 | [ ![Download](https://api.bintray.com/packages/zjun615/zjun/RuleView/images/download.svg) ](https://bintray.com/zjun615/zjun/RuleView/_latestVersion) 3 | 4 | 一系列卷尺控件,包含:基本卷尺控件(体重尺,或其它)、金额卷尺控件、时间卷尺控件 5 | 6 | demo中,分别用三个控件,高仿了薄荷健康的体重尺、团贷网的金额尺、海康威视下萤石云视频的时间尺 7 | 8 | ## 一、效果图 9 | > 全部效果: 10 | 11 | ![allGif](https://github.com/zjun615/RuleView/blob/master/img/all.gif) 12 | 13 | > RuleView: 14 | 15 | ![ruleGif](https://github.com/zjun615/RuleView/blob/master/img/ruleView.gif) 16 | 17 | > MoneySelectRuleView: 18 | 19 | ![moneyGif](https://github.com/zjun615/RuleView/blob/master/img/money.gif) 20 | 21 | > TimeRuleView: 22 | 23 | ![timeGif](https://github.com/zjun615/RuleView/blob/master/img/time.gif) 24 | 25 | 26 | ## 二、卷尺功能说明 27 | 28 | ### 1. RuleView 29 | 30 | 基本卷尺控件,主要功能: 31 | 32 | - 手动滑动 33 | - 惯性滑动 34 | - 停止后,自动定位到最近的刻度 35 | - 支持动态修改最小值、最大值、当前值 36 | 37 | demo中以体重尺为例,但不限于此 38 | 39 | ### 2. MoneySelectRuleView 40 | 金额选择尺,除了基本卷尺的功能外,额外的功能有: 41 | 42 | - 可设置“剩余金额” 43 | 44 | ### 3. TimeRuleView 45 | 时间尺,一天24h内的时间尺,最小刻度是1s 46 | 47 | - 支持惯性滑动 48 | - 支持缩放时间刻度 49 | - 支持多个时间段的显示 50 | 51 | ## 三、依赖 52 | > build.gradle in app, the repertory is JCenter: 53 | 54 | `implementation 'com.zjun:rule-view:0.0.1'` 55 | 56 | ## 四、使用 57 | 58 | ### 4.1 布局 59 | 60 | 都是最简单的使用,根据需要使用自定义属性即可,详细的自定义属性见五 61 | 62 | ```xml 63 | 67 | 68 | 71 | 72 | 75 | ``` 76 | 77 | ### 4.2 监听回调 78 | 79 | 主要用来监听当前数值的变化 80 | ```java 81 | ruleView.setOnValueChangedListener(new RuleView.OnValueChangedListener() { 82 | @Override 83 | public void onValueChanged(float value) { 84 | tvValue.setText(Float.toString(value)); 85 | } 86 | }); 87 | 88 | // 金额的超额判断,不在自定义View中判断,使用时自行判断,可参考demo 89 | moneySelectRuleView.setOnValueChangedListener(new MoneySelectRuleView.OnValueChangedListener() { 90 | @Override 91 | public void onValueChanged(int newValue) { 92 | tvMoney.setText(Integer.toString(newValue)); 93 | } 94 | }); 95 | 96 | timeRuleView.setOnTimeChangedListener(new TimeRuleView.OnTimeChangedListener() { 97 | @Override 98 | public void onTimeChanged(int newTimeValue) { 99 | tvTime.setText(TimeRuleView.formatTimeHHmmss(newTimeValue)); 100 | } 101 | }); 102 | ``` 103 | 104 | ## 五、属性说明 105 | 106 | ### 5.1 RuleView属性 107 | 108 | 属性名 | 说明 | 默认值 109 | :------ | :------ | :------ 110 | zjun_bgColor | 背景颜色 | #f5f8f5 111 | zjun_gradationColor | 刻度颜色 | Color.LTGRAY(#CCCCCC) 112 | gv_shortLineWidth | 短刻度线的宽度 | 1dp 113 | gv_shortGradationLen | 短刻度长度 | 16dp 114 | gv_longGradationLen | 长刻度长度 | gv_shortGradationLen * 2 115 | gv_longLineWidth | 长刻度线的宽度 | gv_shortLineWidth * 2 116 | zjun_textColor | 刻度值字体颜色 | Color.BLACK,#000000 117 | zjun_textSize | 刻度值字体大小 | 14sp 118 | zjun_indicatorLineColor | 中间指针线的颜色 | #48b975 119 | zjun_indicatorLineWidth | 中间指针线的宽度 | 3dp 120 | gv_indicatorLineLen | 中间指针线的长度 | 35dp 121 | gv_minValue | 最小值 | 0f 122 | gv_maxValue | 最大值 | 100f 123 | gv_currentValue | 当前值 | 50f 124 | gv_gradationUnit | 刻度间最小单位数值 | .1f 125 | gv_numberPerCount | 两数值间最小单位的个数 | 10 126 | gv_gradationGap | 最小单位的间距 | 10dp 127 | gv_gradationNumberGap | 数值与最长刻度的间距 | 8dp 128 | 129 | 130 | ### 5.2 MoneySelectRuleView属性 131 | 132 | 属性名 | 说明 | 默认值 133 | :------ | :------ | :------ 134 | zjun_bgColor | 背景色 | #F5F5F5 135 | zjun_gradationColor | 刻度颜色 | Color.LTGRAY(#CCCCCC) 136 | msrv_gradationHeight | 刻度的总高度,刻度基线以上的高度 | 40dp 137 | msrv_gradationShortLen | 短刻度的长度 | 6dp 138 | msrv_gradationLongLen | 长刻度的长度 | msrv_gradationShortLen * 2 139 | msrv_gradationShortWidth | 短刻度的宽度 | 1px 140 | msrv_gradationLongWidth | 短刻度的长度 | msrv_gradationShortWidth 141 | msrv_gradationValueGap | 刻度数值与长刻度的间距 | 8dp 142 | msrv_gradationTextSize | 刻度数值文字的大小 | 12sp 143 | zjun_textColor | 刻度数值文字的颜色 | Color.GRAY 144 | zjun_indicatorLineColor | 中间指针线的颜色 | #eb4c1c 145 | msrv_balanceTextSize | 余额文字的大小 | 10sp 146 | msrv_unitGap | 最小单位的间距 | 6dp 147 | msrv_balanceText | 余额文字内容 | "剩余额度" 148 | msrv_balanceGap | 余额文字与刻度基线的间距 | 4dp 149 | msrv_maxValue | 最大金额 | 50_000 150 | msrv_currentValue | 当前金额 | 0 151 | msrv_balanceValue | 剩余金额 | 0 152 | msrv_valueUnit | 每隔最小单位代表的金额值 | 100 153 | msrv_valuePerCount | 刻度数值间的最小单位数 | 10 154 | 155 | ### 5.3 TimeRuleView属性 156 | 157 | 属性名 | 说明 | 默认值 158 | :------ | :------ | :------ 159 | zjun_bgColor | 背景颜色 | #EEEEEE 160 | zjun_gradationColor | 刻度颜色 | Color.GRAY 161 | trv_partHeight | 时间段的高度 | 20dp 162 | trv_partColor | 时间的颜色 | #F58D24 163 | trv_gradationWidth | 刻度的宽度 | 1px 164 | trv_secondLen | 秒刻度的长度 | 3dp 165 | trv_minuteLen | 分刻度的长度 | 5dp 166 | trv_hourLen | 时刻度的长度 | 10dp 167 | trv_gradationTextColor | 刻度数值颜色 | Color.GRAY 168 | trv_gradationTextSize | 刻度数值字体大小 | 12sp 169 | trv_gradationTextGap | 刻度数值与时刻度的间距 | 2dp 170 | trv_currentTime | 当前时间 | 0(单位:秒,范围∈[0, 24*3600]) 171 | trv_indicatorTriangleSideLen | 中间指针头部三角形的边长 | 15dp 172 | zjun_indicatorLineWidth | 中间指针的宽度 | 1dp 173 | zjun_indicatorLineColor | 中间指针的颜色 | Color.RED 174 | 175 | ## 六、参考 176 | 177 | [1] [带你实现漂亮的滑动卷尺](https://mp.weixin.qq.com/s?__biz=MzIyNTY4NjU0OQ==&mid=2247485213&idx=1&sn=97fb5d0645f0bb0eb2c5c51db4454b59&chksm=e87aa267df0d2b71d53fa2267b12afae265df585246e8ce5e2edc32d9c57bad6fbd78ec0469c&mpshare=1&scene=23&srcid=0717uZbDuuzfAGiZKvVaft9k#rd) 178 | 179 | [2] [安卓自定义View进阶-缩放手势检测(ScaleGestureDecetor)](http://www.gcssloop.com/customview/scalegesturedetector) 180 | 181 | [3] [安卓自定义View进阶-MotionEvent详解](http://www.gcssloop.com/customview/motionevent) 182 | 183 | [4] [手把手教你发布自己的开源库到 Jcenter](https://mp.weixin.qq.com/s?__biz=MzIwMTAzMTMxMg==&mid=2649492998&idx=1&sn=015de305fa8cb125caf25d072165f6e8&chksm=8eec85f9b99b0cef30052e7333129ee1ffbba1600ebe5529b05c7d67a7690438ae7180acc804&mpshare=1&scene=23&srcid=0818WK605Hl5ctCkIBFtDP5q#rd) 184 | 185 | [5] RecyclerView 的 onTouchEvent(MotionEvent e) 源码 186 | 187 | **说明:** 188 | 189 | RuleView,针对参考[1]中 TapeView 的源码,进行了一些优化: 190 | 191 | 1) 左侧起始绘制刻度:如果当前绘制的刻度 currentCalibration 为0,即第一个刻度,则不绘制。滑动过程中,会发现最左侧的刻度,有时会突然消失。如果此刻度是带数值的长刻度,那数值也不会显示,界面突变会更明显。把控件宽度为 gapWidth 的整数倍,能立马看到这突变效果 192 | 193 | 优化:左侧起始刻度,往左多绘制2个刻度单位 194 | 195 | 2) 右侧结束绘制刻度:一直绘制到当前控件的10倍宽,或最大刻度数(totalCalibration = 最大刻度数 + 1,看的时候掉坑了,觉得最大刻度绘制不出来,原来是加了1) 196 | 197 | 优化:在最大刻度内,右侧结束刻度,与左侧起始刻度一样,多绘制2个刻度单位即可 198 | 199 | 多绘制的刻度单位个数,完全可以根据数值的最大宽度来决定。多了2个刻度单位,因为数值与刻度对称,就有了4个刻度。具体根据项目,多退少补 200 | 201 | ## 七、更新 202 | > *2018/8/22*: 203 | 204 | 经过网友提醒,才发现单词 Rule 写错了,尴尬了⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄。现更改 GitHub 项目名为 RulerView,其它不便修改,保持不变。 205 | 206 | ## 八、LICENSE 207 | ``` 208 | Copyright (c) 2018 Ralap 209 | 210 | Licensed under the Apache License, Version 2.0 (the "License"); 211 | you may not use this file except in compliance with the License. 212 | You may obtain a copy of the License at 213 | 214 | http://www.apache.org/licenses/LICENSE-2.0 215 | 216 | Unless required by applicable law or agreed to in writing, software 217 | distributed under the License is distributed on an "AS IS" BASIS, 218 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 219 | See the License for the specific language governing permissions and 220 | limitations under the License. 221 | ``` 222 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 27 5 | defaultConfig { 6 | applicationId "com.zjun.demo.ruleview" 7 | minSdkVersion 16 8 | targetSdkVersion 27 9 | versionCode 1 10 | versionName "1.0" 11 | testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | implementation fileTree(include: ['*.jar'], dir: 'libs') 23 | implementation 'com.android.support:appcompat-v7:27.1.1' 24 | implementation 'com.android.support.constraint:constraint-layout:1.1.2' 25 | implementation 'com.android.support:design:27.1.1' 26 | testImplementation 'junit:junit:4.12' 27 | androidTestImplementation 'com.android.support.test:runner:1.0.2' 28 | androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2' 29 | implementation project(':rule-view') 30 | } 31 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /app/src/androidTest/java/com/zjun/demo/ruleview/ExampleInstrumentedTest.java: -------------------------------------------------------------------------------- 1 | package com.zjun.demo.ruleview; 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 | * Instrumented 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() { 21 | // Context of the app under test. 22 | Context appContext = InstrumentationRegistry.getTargetContext(); 23 | 24 | assertEquals("com.zjun.demo.gradationview", appContext.getPackageName()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/com/zjun/demo/ruleview/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.zjun.demo.ruleview; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.IdRes; 5 | import android.support.design.widget.Snackbar; 6 | import android.support.v7.app.AppCompatActivity; 7 | import android.view.View; 8 | import android.widget.EditText; 9 | import android.widget.LinearLayout; 10 | import android.widget.TextView; 11 | 12 | import com.zjun.widget.MoneySelectRuleView; 13 | import com.zjun.widget.RuleView; 14 | import com.zjun.widget.TimeRuleView; 15 | 16 | import java.util.ArrayList; 17 | import java.util.List; 18 | import java.util.Random; 19 | 20 | public class MainActivity extends AppCompatActivity { 21 | 22 | private TextView tvValue; 23 | private RuleView gvRule; 24 | private MoneySelectRuleView msrvMoney; 25 | private TextView tvMoney; 26 | private EditText etMoney; 27 | private TimeRuleView trvTime; 28 | private TextView tvTime; 29 | 30 | private float moneyBalance; 31 | private boolean isMoneySloped; 32 | 33 | @Override 34 | protected void onCreate(Bundle savedInstanceState) { 35 | super.onCreate(savedInstanceState); 36 | setContentView(R.layout.activity_main); 37 | 38 | tvTime = findViewById(R.id.tv_time); 39 | trvTime = findViewById(R.id.trv_time); 40 | tvMoney = findViewById(R.id.tv_money); 41 | etMoney = findViewById(R.id.et_new_money); 42 | msrvMoney = findViewById(R.id.msrv_money); 43 | tvValue = findViewById(R.id.tv_value); 44 | gvRule = findViewById(R.id.gv_1); 45 | 46 | 47 | tvValue.setText(Float.toString(gvRule.getCurrentValue())); 48 | gvRule.setOnValueChangedListener(new RuleView.OnValueChangedListener() { 49 | @Override 50 | public void onValueChanged(float value) { 51 | tvValue.setText(Float.toString(value)); 52 | } 53 | }); 54 | 55 | tvMoney.setText(Integer.toString(msrvMoney.getValue())); 56 | moneyBalance = msrvMoney.getBalance(); 57 | msrvMoney.setOnValueChangedListener(new MoneySelectRuleView.OnValueChangedListener() { 58 | @Override 59 | public void onValueChanged(int newValue) { 60 | tvMoney.setText(Integer.toString(newValue)); 61 | if (newValue > moneyBalance) { 62 | if (!isMoneySloped) { 63 | isMoneySloped = true; 64 | Snackbar.make(msrvMoney, "超出额度", Snackbar.LENGTH_SHORT).show(); 65 | } 66 | } else { 67 | if (isMoneySloped) { 68 | isMoneySloped = false; 69 | } 70 | } 71 | } 72 | }); 73 | 74 | trvTime.setOnTimeChangedListener(new TimeRuleView.OnTimeChangedListener() { 75 | @Override 76 | public void onTimeChanged(int newTimeValue) { 77 | tvTime.setText(TimeRuleView.formatTimeHHmmss(newTimeValue)); 78 | } 79 | }); 80 | // 模拟时间段数据 81 | List list = new ArrayList<>(); 82 | for (int i = 0; i < 10; i++) { 83 | TimeRuleView.TimePart part = new TimeRuleView.TimePart(); 84 | part.startTime = i * 1000; 85 | part.endTime = part.startTime + new Random().nextInt(1000); 86 | list.add(part); 87 | } 88 | trvTime.setTimePartList(list); 89 | 90 | } 91 | 92 | 93 | public void onClick(View view) { 94 | switch (view.getId()) { 95 | case R.id.tv_rule_indicator: 96 | toggleSettingsShow(R.id.ll_rule_settings); 97 | break; 98 | case R.id.btn_50: 99 | gvRule.setCurrentValue(gvRule.getMinValue() == 11 ? 15 : 50); 100 | break; 101 | case R.id.btn_change: 102 | toggleValue(); 103 | break; 104 | case R.id.tv_money_indicator: 105 | toggleSettingsShow(R.id.ll_money_settings); 106 | break; 107 | case R.id.btn_set_money: 108 | float money = getMoney(); 109 | msrvMoney.setValue(money); 110 | break; 111 | case R.id.btn_set_balance: 112 | moneyBalance = getMoney(); 113 | msrvMoney.setBalance(moneyBalance); 114 | isMoneySloped = false; 115 | break; 116 | default: break; 117 | } 118 | } 119 | 120 | private void toggleSettingsShow(@IdRes int layoutId) { 121 | LinearLayout llSettings = findViewById(layoutId); 122 | llSettings.setVisibility(llSettings.getVisibility() == View.VISIBLE ? View.INVISIBLE : View.VISIBLE); 123 | } 124 | 125 | private void toggleValue() { 126 | if (gvRule.getMinValue() == 11) { 127 | gvRule.setValue(0, 100, 50, 0.1f, 10); 128 | } else { 129 | gvRule.setValue(11, 20, 15, 0.2f, 5); 130 | } 131 | } 132 | 133 | private float getMoney() { 134 | String moneyStr = etMoney.getText().toString(); 135 | if (moneyStr.isEmpty()) { 136 | moneyStr = "0"; 137 | } 138 | return Float.parseFloat(moneyStr); 139 | } 140 | 141 | } 142 | -------------------------------------------------------------------------------- /app/src/main/res/drawable-v24/ic_launcher_foreground.xml: -------------------------------------------------------------------------------- 1 | 7 | 12 | 13 | 19 | 22 | 25 | 26 | 27 | 28 | 34 | 35 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/ic_launcher_background.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 10 | 15 | 20 | 25 | 30 | 35 | 40 | 45 | 50 | 55 | 60 | 65 | 70 | 75 | 80 | 85 | 90 | 95 | 100 | 105 | 110 | 115 | 120 | 125 | 130 | 135 | 140 | 145 | 150 | 155 | 160 | 165 | 170 | 171 | -------------------------------------------------------------------------------- /app/src/main/res/drawable/shape_time_rule_view_bg.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 8 | 9 | 10 | 11 | 12 | 13 | 16 | 17 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 10 | 11 | 12 | 17 | 18 | 25 | 26 | 34 | 35 | 45 | 46 | 47 | 53 | 54 | 60 | 61 | 65 | 66 |