├── .gitignore ├── .idea ├── .name ├── compiler.xml ├── copyright │ └── profiles_settings.xml ├── encodings.xml ├── gradle.xml ├── misc.xml ├── modules.xml ├── runConfigurations.xml └── vcs.xml ├── LICENSE ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── jacpy │ │ └── sb │ │ └── MainActivity.java │ └── res │ ├── layout │ └── activity_main.xml │ ├── mipmap-hdpi │ └── ic_launcher.png │ ├── mipmap-mdpi │ └── ic_launcher.png │ ├── mipmap-xhdpi │ └── ic_launcher.png │ ├── mipmap-xxhdpi │ └── ic_launcher.png │ ├── mipmap-xxxhdpi │ └── ic_launcher.png │ ├── values-w820dp │ └── dimens.xml │ └── values │ ├── colors.xml │ ├── dimens.xml │ ├── strings.xml │ └── styles.xml ├── build.gradle ├── bus-line ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ └── main │ ├── AndroidManifest.xml │ ├── java │ └── com │ │ └── jacpy │ │ └── busline │ │ └── widget │ │ ├── BusLineView.java │ │ └── model │ │ └── BusLineItem.java │ └── res │ ├── drawable-xhdpi │ ├── ic_bus.png │ ├── ic_bus_small.png │ ├── ic_current_bus_station.png │ ├── ic_current_bus_station_location.png │ └── ic_railway.png │ └── values │ └── strings.xml ├── captures └── bus_line.gif ├── gradle.properties ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Built application files 2 | *.apk 3 | *.ap_ 4 | 5 | # Files for the ART/Dalvik VM 6 | *.dex 7 | 8 | # Java class files 9 | *.class 10 | 11 | # Generated files 12 | bin/ 13 | gen/ 14 | out/ 15 | 16 | # Gradle files 17 | .gradle/ 18 | build/ 19 | 20 | # Local configuration file (sdk path, etc) 21 | local.properties 22 | 23 | # Proguard folder generated by Eclipse 24 | proguard/ 25 | 26 | # Log Files 27 | *.log 28 | 29 | # Android Studio Navigation editor temp files 30 | .navigation/ 31 | 32 | # Android Studio captures folder 33 | #captures/ 34 | 35 | # Intellij 36 | *.iml 37 | .idea/workspace.xml 38 | 39 | # Keystore files 40 | *.jks 41 | 42 | /.idea/libraries 43 | .DS_Store 44 | -------------------------------------------------------------------------------- /.idea/.name: -------------------------------------------------------------------------------- 1 | BusLine -------------------------------------------------------------------------------- /.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 | 23 | -------------------------------------------------------------------------------- /.idea/copyright/profiles_settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | -------------------------------------------------------------------------------- /.idea/encodings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | -------------------------------------------------------------------------------- /.idea/gradle.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 25 | 26 | -------------------------------------------------------------------------------- /.idea/misc.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 12 | 13 | 14 | 26 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | Class structureJava 46 | 47 | 48 | Code maturity issuesJava 49 | 50 | 51 | Java 52 | 53 | 54 | Java language level migration aidsJava 55 | 56 | 57 | Javadoc issuesJava 58 | 59 | 60 | Performance issuesJava 61 | 62 | 63 | TestNG 64 | 65 | 66 | Threading issuesJava 67 | 68 | 69 | 70 | 71 | Android 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 93 | 94 | 95 | 96 | 97 | 98 | 103 | 104 | 105 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /.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 | -------------------------------------------------------------------------------- /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 | # BusLine 2 | 仿《车来了》公交线路自定义View,根据线路数据绘制View并控制滑动手势。 3 | 4 | ### 效果 5 | 先看最终运行效果: 6 | 7 | ![效果图](./captures/bus_line.gif) 8 | 9 | ### 使用 10 | 11 | * XML布局: 12 | 13 | ```xml 14 | 15 | 25 | 26 | 32 | 33 | 34 | 35 | ``` 36 | * 在代码中使用: 37 | 38 | ```java 39 | BusLineView view = (BusLineView) findViewById(R.id.bus_line); 40 | view.setBusLineData(initData()); // 设置数据 41 | view.setOnBusStationClickListener(this); // 设置公交站点的点击事件 42 | ``` 43 | 44 | * 性能 45 | 在2013年的机型MOTO G XT1032机子上运行,同时刷新96个公交站点数据,onDraw()方法耗时平均7ms左右。 46 | 47 | ### 实现原理 48 | 49 | 站点信息使用android系统中的绘图部分API,位置是通过大小及顺序计算位置坐标。手势滑动使用的是android系统中的Touch事件控制及Scroller滚动实现。 50 | 51 | 52 | ### 关于 53 | 54 | [jacpy](http://www.jacpy.com) 55 | 56 | ### License 57 | 本项目遵守Apache 2.0 license。 58 | 59 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | applicationId "com.jacpy.busline.demo" 9 | minSdkVersion 15 10 | targetSdkVersion 23 11 | versionCode 1 12 | versionName "1.0" 13 | } 14 | buildTypes { 15 | release { 16 | minifyEnabled false 17 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 18 | } 19 | } 20 | } 21 | 22 | dependencies { 23 | compile fileTree(dir: 'libs', include: ['*.jar']) 24 | compile 'com.android.support:appcompat-v7:23.0.1' 25 | compile project(':bus-line') 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:\programs\android-sdk-windows-studio/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/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /app/src/main/java/com/jacpy/sb/MainActivity.java: -------------------------------------------------------------------------------- 1 | package com.jacpy.sb; 2 | 3 | import android.os.Bundle; 4 | import android.support.v7.app.AppCompatActivity; 5 | import android.util.Log; 6 | import android.view.View; 7 | 8 | import com.jacpy.busline.widget.BusLineView; 9 | import com.jacpy.busline.widget.model.BusLineItem; 10 | 11 | import java.util.ArrayList; 12 | import java.util.List; 13 | 14 | public class MainActivity extends AppCompatActivity implements BusLineView.OnBusStationClickListener { 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | setContentView(R.layout.activity_main); 20 | 21 | BusLineView view = (BusLineView) findViewById(R.id.bus_line); 22 | view.setBusLineData(initData()); // 设置数据 23 | view.setOnBusStationClickListener(this); // 设置公交站点的点击事件 24 | } 25 | 26 | @Override 27 | public void onBusStationClick(View view, BusLineItem item) { 28 | if (BuildConfig.DEBUG) { 29 | Log.d("may", "name: " + item.name); 30 | } 31 | } 32 | 33 | private List initData() { 34 | ArrayList list = new ArrayList<>(); 35 | BusLineItem item = new BusLineItem(); 36 | item.name = "紫薇阁"; 37 | item.trafficState = initRoadState(); 38 | list.add(item); 39 | 40 | item = new BusLineItem(); 41 | item.name = "景蜜村"; 42 | item.busPosition = 0; 43 | item.trafficState = initRoadState(); 44 | list.add(item); 45 | 46 | item = new BusLineItem(); 47 | item.name = "福田外语学校东"; 48 | item.trafficState = initRoadState(); 49 | item.isRailwayStation = true; 50 | item.isCurrentPosition = true; 51 | list.add(item); 52 | 53 | item = new BusLineItem(); 54 | item.name = "景田沃尔玛"; 55 | item.trafficState = initRoadState(); 56 | item.isRailwayStation = true; 57 | item.busPosition = 30; 58 | list.add(item); 59 | 60 | item = new BusLineItem(); 61 | item.name = "特发小区"; 62 | item.trafficState = initRoadState(); 63 | list.add(item); 64 | 65 | item = new BusLineItem(); 66 | item.name = "枫丹雅苑"; 67 | item.isRailwayStation = false; 68 | list.add(item); 69 | 70 | item = new BusLineItem(); 71 | item.name = "市委党校"; 72 | item.trafficState = initRoadState(); 73 | list.add(item); 74 | 75 | item = new BusLineItem(); 76 | item.name = "香蜜红荔立交"; 77 | item.busPosition = 50; 78 | list.add(item); 79 | 80 | item = new BusLineItem(); 81 | item.name = "景蜜村"; 82 | item.trafficState = initRoadState(); 83 | list.add(item); 84 | 85 | item = new BusLineItem(); 86 | item.name = "天安数码城"; 87 | item.trafficState = initRoadState(); 88 | list.add(item); 89 | 90 | item = new BusLineItem(); 91 | item.name = "上沙村"; 92 | list.add(item); 93 | 94 | item = new BusLineItem(); 95 | item.name = "沙嘴路口"; 96 | item.trafficState = initRoadState(); 97 | list.add(item); 98 | 99 | item = new BusLineItem(); 100 | item.name = "新洲中学"; 101 | item.busPosition = 80; 102 | list.add(item); 103 | 104 | item = new BusLineItem(); 105 | item.name = "新洲三街"; 106 | item.trafficState = initRoadState(); 107 | list.add(item); 108 | 109 | item = new BusLineItem(); 110 | item.name = "新洲村"; 111 | list.add(item); 112 | 113 | item = new BusLineItem(); 114 | item.name = "众孚小学"; 115 | item.trafficState = initRoadState(); 116 | list.add(item); 117 | 118 | item = new BusLineItem(); 119 | item.name = "石厦学校"; 120 | list.add(item); 121 | 122 | item = new BusLineItem(); 123 | item.name = "福田区委"; 124 | item.trafficState = initRoadState(); 125 | item.isRailwayStation = true; 126 | list.add(item); 127 | 128 | item = new BusLineItem(); 129 | item.name = "皇岗村"; 130 | item.busPosition = 50; 131 | item.isRailwayStation = true; 132 | list.add(item); 133 | 134 | item = new BusLineItem(); 135 | item.name = "福田保健院"; 136 | item.trafficState = initRoadState(); 137 | item.isRailwayStation = true; 138 | list.add(item); 139 | 140 | item = new BusLineItem(); 141 | item.name = "福民地铁站"; 142 | item.isRailwayStation = true; 143 | list.add(item); 144 | 145 | item = new BusLineItem(); 146 | item.name = "福强路口"; 147 | item.trafficState = initRoadState(); 148 | item.busPosition = 80; 149 | list.add(item); 150 | 151 | item = new BusLineItem(); 152 | item.name = "福田口岸总站"; 153 | item.isRailwayStation = true; 154 | list.add(item); 155 | 156 | return list; 157 | } 158 | 159 | private BusLineItem.RoadState[] initRoadState() { 160 | BusLineItem.RoadState[] rss = new BusLineItem.RoadState[5]; 161 | BusLineItem.RoadState rs = new BusLineItem.RoadState(); 162 | rs.idx = 0; 163 | rs.start = 0; 164 | rs.ratio = 0.1f; 165 | rs.state = BusLineItem.RoadState.ROAD_BUSY; 166 | rss[0] = rs; 167 | 168 | rs = new BusLineItem.RoadState(); 169 | rs.idx = 1; 170 | rs.start = 0.1f; 171 | rs.ratio = 0.2f; 172 | rs.state = BusLineItem.RoadState.ROAD_NORMAL; 173 | rss[1] = rs; 174 | 175 | rs = new BusLineItem.RoadState(); 176 | rs.idx = 2; 177 | rs.start = 0.3f; 178 | rs.ratio = 0.2f; 179 | rs.state = BusLineItem.RoadState.ROAD_BLOCK; 180 | rss[2] = rs; 181 | 182 | rs = new BusLineItem.RoadState(); 183 | rs.idx = 3; 184 | rs.start = 0.5f; 185 | rs.ratio = 0.3f; 186 | rs.state = BusLineItem.RoadState.ROAD_BUSY; 187 | rss[3] = rs; 188 | 189 | rs = new BusLineItem.RoadState(); 190 | rs.idx = 4; 191 | rs.start = 0.8f; 192 | rs.ratio = 0.2f; 193 | rs.state = BusLineItem.RoadState.ROAD_BLOCK; 194 | rss[4] = rs; 195 | return rss; 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_main.xml: -------------------------------------------------------------------------------- 1 | 2 | 12 | 13 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/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 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | BusLineDemo 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /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.1.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 | -------------------------------------------------------------------------------- /bus-line/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /bus-line/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.library' 2 | 3 | android { 4 | compileSdkVersion 23 5 | buildToolsVersion "23.0.2" 6 | 7 | defaultConfig { 8 | minSdkVersion 15 9 | targetSdkVersion 24 10 | versionCode 1 11 | versionName "1.0" 12 | } 13 | buildTypes { 14 | release { 15 | minifyEnabled false 16 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 17 | } 18 | } 19 | } 20 | 21 | dependencies { 22 | compile fileTree(dir: 'libs', include: ['*.jar']) 23 | } 24 | -------------------------------------------------------------------------------- /bus-line/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Users/mayliang/programs/android-sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /bus-line/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /bus-line/src/main/java/com/jacpy/busline/widget/BusLineView.java: -------------------------------------------------------------------------------- 1 | package com.jacpy.busline.widget; 2 | 3 | import android.content.Context; 4 | import android.graphics.Canvas; 5 | import android.graphics.Color; 6 | import android.graphics.Paint; 7 | import android.graphics.Rect; 8 | import android.graphics.RectF; 9 | import android.graphics.drawable.BitmapDrawable; 10 | import android.graphics.drawable.ColorDrawable; 11 | import android.graphics.drawable.Drawable; 12 | import android.util.AttributeSet; 13 | import android.util.Log; 14 | import android.view.MotionEvent; 15 | import android.view.VelocityTracker; 16 | import android.view.View; 17 | import android.view.ViewConfiguration; 18 | import android.widget.OverScroller; 19 | 20 | import com.jacpy.busline.BuildConfig; 21 | import com.jacpy.busline.R; 22 | import com.jacpy.busline.widget.model.BusLineItem; 23 | 24 | import java.util.ArrayList; 25 | import java.util.List; 26 | 27 | /** 28 | * Created by jacpy.may@gmail.com on 2016/8/24 10:52. 29 | */ 30 | public class BusLineView extends View { 31 | 32 | private List mBusLines = new ArrayList<>(); 33 | private int mBusStationWidth; 34 | private Drawable mBus, mBusSmall, mCurrentPosition, mCurrentPositionLocation, mRailway; 35 | private int mMargin1X, mMargin2P; 36 | private float mStrokeWidth; 37 | private int mColorPass, mColorUnreach, mSelectedColor, mColorBlock, mColorBusy, mColorNormal; 38 | 39 | private Rect mTxtRect; 40 | private RectF mBusRect, mLocationRectF; 41 | 42 | private Paint mStationCirclePaint, mTxtPaint; 43 | 44 | private float mNumTxtSize, mStationNameTxtSize, mSelectedTxtSize; 45 | private int mHeight; 46 | 47 | private OnBusStationClickListener mOnBusStationClickListener; 48 | 49 | private OverScroller mScroller; 50 | private VelocityTracker mVelocityTracker; 51 | private boolean mIsScroll; 52 | private int mTouchSlop, mMinimumFlingVelocity; 53 | 54 | public BusLineView(Context context) { 55 | super(context); 56 | init(context); 57 | } 58 | 59 | public BusLineView(Context context, AttributeSet attrs) { 60 | super(context, attrs); 61 | init(context); 62 | } 63 | 64 | public BusLineView(Context context, AttributeSet attrs, int defStyleAttr) { 65 | super(context, attrs, defStyleAttr); 66 | init(context); 67 | } 68 | 69 | private void init(Context context) { 70 | mScroller = new OverScroller(context); 71 | ViewConfiguration viewConfiguration = ViewConfiguration.get(context); 72 | mTouchSlop = viewConfiguration.getScaledTouchSlop(); 73 | mMinimumFlingVelocity = viewConfiguration.getScaledMinimumFlingVelocity() * 5; 74 | 75 | mBus = getResources().getDrawable(R.drawable.ic_bus); 76 | mBusSmall = getResources().getDrawable(R.drawable.ic_bus_small); 77 | mCurrentPosition = getResources().getDrawable(R.drawable.ic_current_bus_station); 78 | mCurrentPositionLocation = getResources().getDrawable(R.drawable.ic_current_bus_station_location); 79 | mRailway = getResources().getDrawable(R.drawable.ic_railway); 80 | 81 | mBusStationWidth = mBus.getIntrinsicWidth() << 1; 82 | float density = getResources().getDisplayMetrics().density; 83 | mMargin1X = (int) (10 * density); 84 | mMargin2P = (int) (2 * density); 85 | 86 | mStationCirclePaint = new Paint(); 87 | mStrokeWidth = 3 * density; 88 | mStationCirclePaint.setStrokeWidth(mStrokeWidth); 89 | mStationCirclePaint.setStyle(Paint.Style.STROKE); 90 | mStationCirclePaint.setAntiAlias(true); 91 | mTxtPaint = new Paint(); 92 | mTxtPaint.setAntiAlias(true); 93 | 94 | mColorPass = Color.parseColor("#1FB562"); 95 | mColorUnreach = Color.parseColor("#C0C0C0"); 96 | mSelectedColor = Color.parseColor("#FE771D"); 97 | 98 | mColorBlock = Color.parseColor("#F41C0D"); 99 | mColorBusy = Color.parseColor("#FFAC00"); 100 | mColorNormal = Color.parseColor("#00BD00"); 101 | 102 | mTxtRect = new Rect(); 103 | mBusRect = new RectF(); 104 | mLocationRectF = new RectF(); 105 | float scaleDensity = getResources().getDisplayMetrics().scaledDensity; 106 | mNumTxtSize = 14 * scaleDensity; 107 | mStationNameTxtSize = 18 * scaleDensity; 108 | mSelectedTxtSize = 20 * scaleDensity; 109 | } 110 | 111 | /** 112 | * 设置公交车数据 113 | * @param list 公交车列表数据 114 | */ 115 | public void setBusLineData(List list) { 116 | mBusLines.clear(); 117 | if (list == null) { 118 | return; 119 | } 120 | 121 | mBusLines.addAll(list); 122 | mHeight = computeHeight(); 123 | } 124 | 125 | private int computeHeight() { 126 | int num = 0; 127 | BusLineItem bli = null; 128 | for (BusLineItem item : mBusLines) { 129 | int length = item.name.length(); 130 | if (length > num) { 131 | num = length; 132 | bli = item; 133 | } 134 | } 135 | 136 | int height = (int) (250 * getResources().getDisplayMetrics().density); 137 | if (bli != null) { 138 | Rect r = new Rect(); 139 | int y = 0; 140 | Paint p = new Paint(); 141 | p.setTextSize(mSelectedTxtSize); 142 | for (int k = 0, length = bli.name.length(); k < length; ++k) { 143 | String s = String.valueOf(bli.name.charAt(k)); 144 | measureText(s, r, p); 145 | y += r.height() + mMargin2P; 146 | } 147 | 148 | p.setTextSize(mNumTxtSize); 149 | measureText("1", r, p); 150 | y += mMargin1X + mBus.getIntrinsicHeight() + mMargin1X + mCurrentPosition.getIntrinsicHeight() + mMargin1X + r.height() + mMargin2P + mRailway.getIntrinsicHeight() + mMargin1X + mMargin1X; 151 | height = y; 152 | } 153 | 154 | return height; 155 | } 156 | 157 | /** 158 | * 设置公交站显示的宽度,至少是公交车图标的两倍宽度。 159 | * @param width 宽度值 160 | */ 161 | public void setBusStationWidth(int width) { 162 | if (width > mBusStationWidth) { 163 | mBusStationWidth = width; 164 | } 165 | } 166 | 167 | /** 168 | * 设置公交图标 169 | * @param d Drawable 170 | */ 171 | public void setBusDrawable(Drawable d) { 172 | mBus = d; 173 | mBusStationWidth = d.getIntrinsicWidth() << 1; 174 | } 175 | 176 | /** 177 | * 设置小公交图标 178 | * @param d Drawable 179 | */ 180 | public void setBusSmallDrawable(Drawable d) { 181 | mBusSmall = d; 182 | } 183 | 184 | /** 185 | * 设置当前位置显示的图标 186 | * @param d Drawable 187 | */ 188 | public void setCurrentPositionDrawable(Drawable d) { 189 | mCurrentPosition = d; 190 | } 191 | 192 | /** 193 | * 设置定位的图标 194 | * @param d Drawable 195 | */ 196 | public void setCurrentLocationDrawable(Drawable d) { 197 | mCurrentPositionLocation = d; 198 | } 199 | 200 | /** 201 | * 设置有地铁标识的公交站图标 202 | * @param d Drawable 203 | */ 204 | public void setMetroStationDrawable(Drawable d) { 205 | mRailway = d; 206 | } 207 | 208 | /** 209 | * 设置不交通状况的颜色 210 | * @param normal 正常畅通的颜色 211 | * @param busy 交通繁忙的颜色 212 | * @param block 交通拥堵的颜色 213 | */ 214 | public void setTrafficColor(int normal, int busy, int block) { 215 | mColorNormal = normal; 216 | mColorBusy = busy; 217 | mColorBlock = block; 218 | } 219 | 220 | /** 221 | * 设置路线的颜色 222 | * @param pass 到当前站点的颜色 223 | * @param unreach 当前站点以后和颜色 224 | * @param selected 选中公交站时的颜色 225 | */ 226 | public void setBusLineColor(int pass, int unreach, int selected) { 227 | mColorPass = pass; 228 | mColorUnreach = unreach; 229 | mSelectedColor = selected; 230 | } 231 | 232 | /** 233 | * 设置公交站被点击的监听器 234 | * @param l listener 235 | */ 236 | public void setOnBusStationClickListener(OnBusStationClickListener l) { 237 | mOnBusStationClickListener = l; 238 | } 239 | 240 | @Override 241 | public void computeScroll() { 242 | super.computeScroll(); 243 | if (mScroller.computeScrollOffset()) { 244 | scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); 245 | invalidate(); 246 | } 247 | } 248 | 249 | private float mDownX, mDownY; 250 | 251 | @Override 252 | public boolean dispatchTouchEvent(MotionEvent event) { 253 | switch (event.getAction()) { 254 | case MotionEvent.ACTION_DOWN: 255 | if (!mScroller.isFinished()) { 256 | if (BuildConfig.DEBUG) { 257 | Log.d("may", "is not finished."); 258 | } 259 | return super.dispatchTouchEvent(event); 260 | } 261 | 262 | mDownX = event.getX(); 263 | mDownY = event.getY(); 264 | break; 265 | case MotionEvent.ACTION_MOVE: 266 | float x = event.getX(); 267 | float y = event.getY(); 268 | float dx = x - mDownX; 269 | float dy = y - mDownY; 270 | if (BuildConfig.DEBUG) { 271 | Log.d("may", "dx: " + dx + ", dy: " + dy + ", slop: " + mTouchSlop); 272 | } 273 | 274 | if (!mIsScroll && Math.abs(dx) > Math.abs(dy) && Math.abs(dx) > mTouchSlop) { 275 | mIsScroll = true; 276 | } 277 | 278 | break; 279 | case MotionEvent.ACTION_UP: 280 | break; 281 | } 282 | 283 | return super.dispatchTouchEvent(event); 284 | } 285 | 286 | @Override 287 | public boolean onTouchEvent(MotionEvent event) { 288 | if (BuildConfig.DEBUG) { 289 | Log.d("may", "onTouchEvent, action: " + event.getAction() + ", scroll: " + mIsScroll); 290 | } 291 | 292 | addVelocityTracker(event); 293 | switch (event.getAction()) { 294 | case MotionEvent.ACTION_DOWN: 295 | if (!mIsScroll && !mScroller.isFinished()) { 296 | // fling后点击屏幕停止 297 | mScroller.abortAnimation(); 298 | } 299 | 300 | mDownX = event.getX(); 301 | return true; 302 | case MotionEvent.ACTION_MOVE: 303 | if (!mIsScroll) { 304 | return super.onTouchEvent(event); 305 | } 306 | 307 | if (!mScroller.isFinished()) { 308 | mScroller.abortAnimation(); 309 | } 310 | 311 | float x = event.getX(); 312 | scrollBy((int) (mDownX - x), 0); 313 | mDownX = x; 314 | break; 315 | case MotionEvent.ACTION_UP: 316 | case MotionEvent.ACTION_CANCEL: 317 | float velocityX = getScrollVelocity(); 318 | if (BuildConfig.DEBUG) { 319 | Log.d("may", "vx: " + velocityX + ", minVx: " + mMinimumFlingVelocity); 320 | } 321 | 322 | Rect r = new Rect(); 323 | getLocalVisibleRect(r); 324 | if (Math.abs(velocityX) <= mMinimumFlingVelocity) { 325 | int sx = getScrollX(); 326 | int dx = sx + r.width() - getWidth(); 327 | if (sx < 0) { 328 | mScroller.startScroll(sx, 0, -sx, 0, -sx * 2); 329 | invalidate(); 330 | } else if (dx > 0) { 331 | mScroller.startScroll(sx, 0, -dx, 0, dx * 2); 332 | invalidate(); 333 | } else if (!mIsScroll) { 334 | onTapUp(event, r.width()); 335 | } 336 | 337 | mIsScroll = false; 338 | break; 339 | } 340 | 341 | if (!mScroller.isFinished()) { 342 | mScroller.abortAnimation(); 343 | } 344 | 345 | mScroller.fling(getScrollX(), 0, (int) -velocityX, 0, 346 | 0, getWidth() - r.width(), 0, 0, r.width() / 2, 0); 347 | invalidate(); 348 | 349 | mIsScroll = false; 350 | recycleVelocityTracker(); 351 | break; 352 | } 353 | 354 | return super.onTouchEvent(event); 355 | } 356 | 357 | private void onTapUp(MotionEvent event, int visibleWidth) { 358 | float x = event.getX(); 359 | float y = event.getY(); 360 | BusLineItem bli = null; 361 | if (BuildConfig.DEBUG) { 362 | Log.d("may", "x: " + x + ", y: " + y + ", sx: " + getScrollX() + ", cx: " + mScroller.getCurrX() + ", fx: " + mScroller.getFinalX() + ", startx: " + mScroller.getStartX()); 363 | } 364 | 365 | for (BusLineItem item : mBusLines) { 366 | if (item.location != null && item.location.contains(x + getScrollX(), y + getScrollY()) && !item.isCurrentPosition) { 367 | bli = item; 368 | break; 369 | } 370 | } 371 | 372 | if (bli != null) { 373 | for (BusLineItem item : mBusLines) { 374 | item.isCurrentPosition = false; 375 | } 376 | 377 | bli.isCurrentPosition = true; 378 | BusLineItem firstItem = mBusLines.get(0); 379 | int pos = visibleWidth - mBusStationWidth; 380 | int dx = (int) (pos - bli.location.centerX() + getScrollX()); 381 | if (BuildConfig.DEBUG) { 382 | Log.d("may", "dx: " + dx + ", compare: " + (getScrollX() - firstItem.location.left > 0)); 383 | } 384 | 385 | if (getScrollX() - dx < 0) { 386 | // 第一个边界处理 387 | dx = getScrollX(); 388 | if (BuildConfig.DEBUG) { 389 | Log.d("may", "intercept first"); 390 | } 391 | } else if (getScrollX() + visibleWidth - dx > getWidth()) { 392 | // 最后一个边界处理 393 | dx = getScrollX() + visibleWidth - getWidth(); 394 | if (BuildConfig.DEBUG) { 395 | Log.d("may", "intercept last"); 396 | } 397 | } 398 | 399 | if (BuildConfig.DEBUG) { 400 | Log.d("may", "scroll dx: " + dx + ", cx: " + bli.location.centerX() + ", sx: " + mScroller.getCurrX() + ", sx2: " + getScrollX()); 401 | } 402 | 403 | mScroller.startScroll(getScrollX(), 0, -dx, 0, Math.abs(dx) * 2); 404 | invalidate(); 405 | 406 | if (mOnBusStationClickListener != null) { 407 | mOnBusStationClickListener.onBusStationClick(BusLineView.this, bli); 408 | } 409 | } 410 | } 411 | 412 | private void addVelocityTracker(MotionEvent event) { 413 | if (mVelocityTracker == null) { 414 | mVelocityTracker = VelocityTracker.obtain(); 415 | } 416 | 417 | mVelocityTracker.addMovement(event); 418 | } 419 | 420 | private void recycleVelocityTracker() { 421 | if (mVelocityTracker != null) { 422 | mVelocityTracker.recycle(); 423 | mVelocityTracker = null; 424 | } 425 | } 426 | 427 | private float getScrollVelocity() { 428 | mVelocityTracker.computeCurrentVelocity(1000); 429 | return mVelocityTracker.getXVelocity(); 430 | } 431 | 432 | @Override 433 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { 434 | super.onMeasure(widthMeasureSpec, heightMeasureSpec); 435 | setMeasuredDimension(mBusStationWidth * (mBusLines.size()) + getPaddingLeft() + getPaddingRight(), 436 | mHeight + getPaddingTop() + getPaddingBottom()); 437 | } 438 | 439 | @Override 440 | protected void onDraw(Canvas canvas) { 441 | super.onDraw(canvas); 442 | if (mBusLines.size() == 0) { 443 | return; 444 | } 445 | 446 | long start = 0; 447 | if (BuildConfig.DEBUG) { 448 | start = System.currentTimeMillis(); 449 | } 450 | 451 | int margin1X = mMargin1X; 452 | float startX = getPaddingLeft(); 453 | float startY = getPaddingTop(); 454 | 455 | int busStationWidth = mBusStationWidth; 456 | 457 | int margin2X = margin1X << 1; 458 | int busHeight = mBus.getIntrinsicHeight(); // 公交图片的高度 459 | 460 | int currentPosWidth = mCurrentPosition.getIntrinsicWidth(); 461 | int halfCurrentPosWidth = currentPosWidth >> 1; 462 | int locationHeight = mCurrentPosition.getIntrinsicHeight(); 463 | int halfLocationHeight = locationHeight >> 1; 464 | 465 | float lineStartY = startY + busHeight + margin2X + (locationHeight >> 1); 466 | 467 | int halfBusStationWidth = busStationWidth >> 1; 468 | startX += halfBusStationWidth; 469 | 470 | int currentPositionIdx = 0; 471 | for (int i = 0, size = mBusLines.size(); i < size; ++i) { 472 | if (mBusLines.get(i).isCurrentPosition) { 473 | currentPositionIdx = i; 474 | break; 475 | } 476 | } 477 | 478 | // 整个线路 479 | mStationCirclePaint.setColor(mColorPass); 480 | canvas.drawLine(startX, lineStartY, busStationWidth * mBusLines.size() - halfBusStationWidth, lineStartY, mStationCirclePaint); 481 | 482 | // 未到达的线路 483 | mStationCirclePaint.setColor(mColorUnreach); 484 | canvas.drawLine(startX + busStationWidth * currentPositionIdx, lineStartY, busStationWidth * mBusLines.size() - halfBusStationWidth, lineStartY, mStationCirclePaint); 485 | 486 | int halfBusWidth = mBus.getIntrinsicWidth() >> 1; 487 | 488 | BusLineItem hasBusNearCurrentPos = null; 489 | // 找最近的一个公交车 490 | for (int i = 0, size = mBusLines.size(); i < size; ++i) { 491 | BusLineItem item = mBusLines.get(i); 492 | if (item.isCurrentPosition) { 493 | if (item.busPosition == 0) { 494 | hasBusNearCurrentPos = item; 495 | } 496 | 497 | break; 498 | } 499 | 500 | if (item.busPosition >= 0) { 501 | hasBusNearCurrentPos = item; 502 | } 503 | } 504 | 505 | for (int i = 0, size = mBusLines.size(); i < size; ++i) { 506 | BusLineItem item = mBusLines.get(i); 507 | if (item.location == null) { 508 | item.location = new RectF(); 509 | } 510 | 511 | float pos = startX + i * busStationWidth; // 公交站点的起始位置 512 | // 路况 513 | if (item.trafficState != null) { 514 | for (int k = 0, length = item.trafficState.length; k < length; ++k) { 515 | BusLineItem.RoadState rs = item.trafficState[k]; 516 | float sx = busStationWidth * rs.start; 517 | switch (rs.state) { 518 | case BusLineItem.RoadState.ROAD_BLOCK: 519 | mStationCirclePaint.setColor(mColorBlock); 520 | break; 521 | case BusLineItem.RoadState.ROAD_BUSY: 522 | mStationCirclePaint.setColor(mColorBusy); 523 | break; 524 | case BusLineItem.RoadState.ROAD_NORMAL: 525 | mStationCirclePaint.setColor(mColorNormal); 526 | break; 527 | default: 528 | } 529 | 530 | canvas.drawLine(pos + sx, lineStartY, pos + sx + busStationWidth * rs.ratio, lineStartY, mStationCirclePaint); 531 | } 532 | } 533 | 534 | int offsetY = margin1X; 535 | if (item.busPosition >= 0) { 536 | // 公交车位置 537 | float busPos = (float) item.busPosition / BusLineItem.BUS_STATION_LENGTH * busStationWidth; 538 | mBusRect.top = offsetY; 539 | mBusRect.left = pos + busPos; 540 | mBusRect.bottom = offsetY + busHeight; 541 | mBusRect.right = mBusRect.left + mBus.getIntrinsicWidth(); 542 | if (hasBusNearCurrentPos == item) { 543 | // 当前最近的一个公交车 544 | canvas.drawBitmap(((BitmapDrawable) mBus).getBitmap(), pos - halfBusWidth + busPos, offsetY, null); 545 | } else { 546 | canvas.drawBitmap(((BitmapDrawable) mBusSmall).getBitmap(), pos - (mBusSmall.getIntrinsicWidth() >> 1) + busPos, offsetY + mBus.getIntrinsicHeight() - mBusSmall.getIntrinsicHeight(), null); 547 | } 548 | } 549 | 550 | offsetY += margin1X + busHeight; 551 | item.location.top = offsetY; 552 | if (item.isCurrentPosition) { 553 | // 当前位置 554 | mLocationRectF.top = offsetY - mCurrentPositionLocation.getIntrinsicHeight() - mMargin2P; 555 | mLocationRectF.left = pos - (mCurrentPositionLocation.getIntrinsicWidth() >> 1); 556 | mLocationRectF.bottom = offsetY - mMargin2P; 557 | mLocationRectF.right = pos + (mCurrentPositionLocation.getIntrinsicWidth() >> 1); 558 | if (item.busPosition > 0 && !hit(mBusRect, mLocationRectF)) { 559 | // 公交车和显示当前位置的图标相冲突 560 | canvas.drawBitmap(((BitmapDrawable) mCurrentPositionLocation).getBitmap(), mLocationRectF.left, mLocationRectF.top, null); 561 | } 562 | 563 | canvas.drawBitmap(((BitmapDrawable) mCurrentPosition).getBitmap(), pos - halfCurrentPosWidth, offsetY, null); 564 | } else { 565 | mStationCirclePaint.setColor(mColorPass); 566 | canvas.drawCircle(pos, offsetY + halfLocationHeight, halfCurrentPosWidth - mStrokeWidth, mStationCirclePaint); 567 | mStationCirclePaint.setStyle(Paint.Style.FILL); 568 | if (getBackground() instanceof ColorDrawable) { 569 | mStationCirclePaint.setColor(((ColorDrawable) getBackground()).getColor()); 570 | } else { 571 | mStationCirclePaint.setColor(Color.WHITE); 572 | } 573 | 574 | canvas.drawCircle(pos, offsetY + halfLocationHeight, halfCurrentPosWidth - mStrokeWidth - (mMargin2P >> 1), mStationCirclePaint); 575 | mStationCirclePaint.setStyle(Paint.Style.STROKE); 576 | } 577 | 578 | offsetY += mCurrentPosition.getIntrinsicHeight() + margin2X; 579 | 580 | // 文字 581 | String s = String.valueOf(i + 1); 582 | mTxtPaint.setColor(Color.GRAY); 583 | mTxtPaint.setTextSize(mNumTxtSize); 584 | measureText(s, mTxtRect, mTxtPaint); 585 | canvas.drawText(s, pos - (mTxtRect.width() >> 1), offsetY, mTxtPaint); 586 | 587 | // 名称 588 | mTxtPaint.setColor(item.isCurrentPosition ? mSelectedColor : Color.BLACK); 589 | mTxtPaint.setTextSize(item.isCurrentPosition ? mSelectedTxtSize : mStationNameTxtSize); 590 | offsetY += mTxtRect.height() + margin1X; 591 | int oy = offsetY; 592 | for (int k = 0, length = item.name.length(); k < length; ++k) { 593 | s = String.valueOf(item.name.charAt(k)); 594 | measureText(s, mTxtRect, mTxtPaint); 595 | canvas.drawText(s, pos - (mTxtRect.width() >> 1), oy, mTxtPaint); 596 | oy += mTxtRect.height() + mMargin2P; 597 | } 598 | 599 | // int max = Math.max(mTxtRect.width(), currentPosWidth); 600 | item.location.left = pos + halfBusStationWidth - busStationWidth; 601 | item.location.right = item.location.left + busStationWidth; 602 | 603 | if (item.isRailwayStation) { 604 | // 地铁站 605 | canvas.drawBitmap(((BitmapDrawable) mRailway).getBitmap(), pos - (mRailway.getIntrinsicWidth() >> 1), oy - mTxtRect.height() + mMargin2P, null); 606 | } 607 | 608 | item.location.bottom = oy + mMargin2P; 609 | // mTxtPaint.setStyle(Paint.Style.STROKE); 610 | // mTxtPaint.setStrokeWidth(1); 611 | // canvas.drawRect(item.location, mTxtPaint); 612 | } 613 | 614 | if (BuildConfig.DEBUG) { 615 | Log.d("may", ("draw used time: " + (System.currentTimeMillis() - start)) + "ms"); 616 | } 617 | } 618 | 619 | private void measureText(String s, Rect r, Paint p) { 620 | p.getTextBounds(s, 0, s.length(), r); 621 | } 622 | 623 | private boolean hit(RectF src, RectF dst) { 624 | return src.contains(dst) 625 | || src.contains(dst.left, dst.top) 626 | || src.contains(dst.left, dst.bottom) 627 | || src.contains(dst.right, dst.bottom) 628 | || src.contains(dst.right, dst.top) 629 | || dst.contains(src); 630 | } 631 | 632 | public interface OnBusStationClickListener { 633 | void onBusStationClick(View view, BusLineItem item); 634 | } 635 | } 636 | -------------------------------------------------------------------------------- /bus-line/src/main/java/com/jacpy/busline/widget/model/BusLineItem.java: -------------------------------------------------------------------------------- 1 | package com.jacpy.busline.widget.model; 2 | 3 | import android.graphics.RectF; 4 | 5 | /** 6 | * Created by jacpy.may@gmail.com on 2016/8/24 10:56. 7 | */ 8 | public class BusLineItem { 9 | 10 | public static final int BUS_STATION_LENGTH = 100; 11 | 12 | public static final int BUS_NONE = -1; 13 | 14 | /** 公交站名称 */ 15 | public String name; 16 | 17 | /** 用户是否是在当前站的位置 */ 18 | public boolean isCurrentPosition; 19 | 20 | /** 相对于当前让的位置,从-1 ~ 100(不包含100),-1表示当前站点之内的位置没有公交车,0表示公交车正在当前站点位置 */ 21 | public int busPosition = BusLineItem.BUS_NONE; 22 | 23 | /** 是否是地铁站附近 */ 24 | public boolean isRailwayStation; 25 | 26 | /** 在View中的位置 */ 27 | public RectF location; 28 | 29 | /** 交通路况 */ 30 | public RoadState[] trafficState; 31 | 32 | /** 33 | * 路况 34 | */ 35 | public static class RoadState { 36 | 37 | /** 畅通 */ 38 | public static final int ROAD_NORMAL = 0; 39 | 40 | /** 忙路 */ 41 | public static final int ROAD_BUSY = 1; 42 | 43 | /** 拥堵 */ 44 | public static final int ROAD_BLOCK = 2; 45 | 46 | /** 位置下标 */ 47 | public int idx; 48 | 49 | /** 起始位置占比 */ 50 | public float start; 51 | 52 | /** 占比 */ 53 | public float ratio; 54 | 55 | /** 状态 */ 56 | public int state; 57 | } 58 | 59 | } 60 | -------------------------------------------------------------------------------- /bus-line/src/main/res/drawable-xhdpi/ic_bus.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/bus-line/src/main/res/drawable-xhdpi/ic_bus.png -------------------------------------------------------------------------------- /bus-line/src/main/res/drawable-xhdpi/ic_bus_small.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/bus-line/src/main/res/drawable-xhdpi/ic_bus_small.png -------------------------------------------------------------------------------- /bus-line/src/main/res/drawable-xhdpi/ic_current_bus_station.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/bus-line/src/main/res/drawable-xhdpi/ic_current_bus_station.png -------------------------------------------------------------------------------- /bus-line/src/main/res/drawable-xhdpi/ic_current_bus_station_location.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/bus-line/src/main/res/drawable-xhdpi/ic_current_bus_station_location.png -------------------------------------------------------------------------------- /bus-line/src/main/res/drawable-xhdpi/ic_railway.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/bus-line/src/main/res/drawable-xhdpi/ic_railway.png -------------------------------------------------------------------------------- /bus-line/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | bus-line 3 | 4 | -------------------------------------------------------------------------------- /captures/bus_line.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/captures/bus_line.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 | # 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 -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/jacpy/BusLine/fb26e78e194a4fd30fd389941bd130f1603c6068/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Wed Aug 24 10:49:23 CST 2016 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-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 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app', ':bus-line' 2 | --------------------------------------------------------------------------------