├── .gitignore
├── LICENSE
├── README.md
├── README_CH.md
├── app
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── dyzs
│ │ └── app
│ │ └── activity
│ │ └── MainActivity.java
│ └── res
│ ├── layout
│ └── activity_main.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
├── build.gradle
├── compassservant
├── .gitignore
├── build.gradle
├── proguard-rules.pro
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── dyzs
│ │ └── compassservant
│ │ ├── ColorUtil.java
│ │ ├── CompassServant.java
│ │ └── FontMatrixUtils.java
│ └── res
│ └── values
│ ├── attrs.xml
│ ├── colors.xml
│ └── strings.xml
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
└── video
├── compass_servant.gif
└── compass_servant.mp4
/.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 | .idea/tasks.xml
39 | .idea/gradle.xml
40 | .idea/dictionaries
41 | .idea/libraries
42 |
43 | # Keystore files
44 | *.jks
45 |
46 | # External native build folder generated in Android Studio 2.2 and later
47 | .externalNativeBuild
48 |
49 | # Google Services (e.g. APIs or Firebase)
50 | google-services.json
51 |
52 | # Freeline
53 | freeline.py
54 | freeline/
55 | freeline_project_description.json
56 |
57 | *.iml
58 | .gradle
59 | /local.properties
60 | .idea
61 | .DS_Store
62 | /build
63 | .externalNativeBuild
64 |
--------------------------------------------------------------------------------
/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 | CompassServant|[中文文档](https://github.com/dyzs/CompassServant/blob/master/README_CH.md)
2 | --------
3 | this is a practice view like compass or dashboard, you can set color gradient wish what you want.
4 |
5 | ScreenShot Video
6 | --------
7 |
8 | 
9 |
10 | Download
11 | --------
12 | maven
13 | ```xml
14 |
15 | com.dyzs.compassservant
16 | compassservant
17 | 1.0.0
18 | pom
19 |
20 | ```
21 |
22 | gradle
23 | ```xml
24 | compile 'com.dyzs.compassservant:compassservant:1.0.0'
25 | ```
26 |
27 | Usage
28 | --------
29 |
30 | xml sample
31 | ```xml
32 |
48 | ```
49 | and reset tick value and start animation
50 | ```java
51 | compass_servant.setPointerDecibel(iRandom);
52 | ```
53 | also add listener and start tension when you reset pointer
54 | ```java
55 | compass_servant.setServantListener(new CompassServant.ServantListener() {
56 | @Override
57 | public void startTension() {
58 | mLooper.sendEmptyMessage(MESSAGE);
59 | }
60 | });
61 | ```
62 |
63 | Attributes
64 | --------
65 |
66 | ## using sys attrs {android.R.attr.background, android.R.attr.padding}
67 | | **attr** ||
68 | |:---|:---|
69 | | android:background | The background color {android.R.attr.background}.
70 | | android:padding | The spacing between border and outer circle{android.R.attr.padding}.
71 | | app:cs_color_commander | {2~4},the number of your gradient colors in xml.
72 | | app:cs_color1 | The 1st color.
73 | | app:cs_color2 | The 2nd color.
74 | | app:cs_color3 | The 3rd color.
75 | | app:cs_color4 | The 4th color.
76 | | app:cs_galaxy_degree | The degree of your set in xml.
77 | | app:cs_outer_circle | Outer circle width.
78 | | app:cs_tick_mark_length | The compass tick length.
79 | | app:cs_decibel | Number of tick mark.
80 | | app:cs_text_size | The current tick mark text size.
81 |
82 | # License
83 |
84 | Copyright (C) 2018 Misaka Mikoto(dyzs)
85 |
86 | Licensed under the Apache License, Version 2.0 (the "License");
87 | you may not use this file except in compliance with the License.
88 | You may obtain a copy of the License at
89 |
90 | http://www.apache.org/licenses/LICENSE-2.0
91 |
92 | Unless required by applicable law or agreed to in writing, software
93 | distributed under the License is distributed on an "AS IS" BASIS,
94 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
95 | See the License for the specific language governing permissions and
96 | limitations under the License.
97 |
--------------------------------------------------------------------------------
/README_CH.md:
--------------------------------------------------------------------------------
1 | 自定义罗盘
2 | --------
3 | 这是一个仿魅族分贝仪的自定义控件,继承 view,可以叫它 Compass 或者 Dashboard,可以自己设置颜色渐变。
4 |
5 | gif 录屏
6 | --------
7 | 
8 |
9 | 配置 maven & gradle
10 | --------
11 | maven
12 | ```xml
13 |
14 | com.dyzs.compassservant
15 | compassservant
16 | 1.0.0
17 | pom
18 |
19 | ```
20 |
21 | gradle
22 | ```xml
23 | compile 'com.dyzs.compassservant:compassservant:1.0.0'
24 | ```
25 |
26 | 使用方式
27 | --------
28 |
29 | xml 方式
30 | ```xml
31 |
47 | ```
48 | 通过设置指针的刻度,开启绘制动画
49 | ```java
50 | compass_servant.setPointerDecibel(iRandom);
51 | ```
52 | 同时添加动画监听方法,该方法在当前指针动画结束时调用
53 | ```java
54 | compass_servant.setServantListener(new CompassServant.ServantListener() {
55 | @Override
56 | public void startTension() {
57 | mLooper.sendEmptyMessage(MESSAGE);
58 | }
59 | });
60 | ```
61 |
62 | 参数
63 | --------
64 | ## 覆盖了两个系统参数 {android.R.attr.background, android.R.attr.padding}
65 | | **attr** ||
66 | |:---|:---|
67 | | android:background | 背景颜色 {android.R.attr.background}.
68 | | android:padding | 罗盘指针的间隙{android.R.attr.padding}.
69 | | app:cs_color_commander | {2~4},在 xml 中设置控制显示的颜色渐变个数
70 | | app:cs_color1 | 第一个颜色.
71 | | app:cs_color2 |
72 | | app:cs_color3 |
73 | | app:cs_color4 |
74 | | app:cs_galaxy_degree | 整个罗盘的刻度的总角度,小于360
75 | | app:cs_outer_circle | 外圆环宽带
76 | | app:cs_tick_mark_length | 刻度长度
77 | | app:cs_decibel | 罗盘的总刻度
78 | | app:cs_text_size | 中间显示的刻度值的文字大小
79 |
80 | # License
81 |
82 | Copyright (C) 2018 Misaka Mikoto(dyzs)
83 |
84 | Licensed under the Apache License, Version 2.0 (the "License");
85 | you may not use this file except in compliance with the License.
86 | You may obtain a copy of the License at
87 |
88 | http://www.apache.org/licenses/LICENSE-2.0
89 |
90 | Unless required by applicable law or agreed to in writing, software
91 | distributed under the License is distributed on an "AS IS" BASIS,
92 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
93 | See the License for the specific language governing permissions and
94 | limitations under the License.
95 |
--------------------------------------------------------------------------------
/app/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/app/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion 26
5 | buildToolsVersion "26.0.2"
6 |
7 | defaultConfig {
8 | applicationId "com.dyzs.app.compassservant"
9 | minSdkVersion 21
10 | targetSdkVersion 26
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 | implementation fileTree(dir: 'libs', include: ['*.jar'])
24 | implementation 'com.android.support:appcompat-v7:26.0.2'//25.3.1'
25 | implementation 'com.android.support.constraint:constraint-layout:1.0.2'
26 | implementation project(':compassservant')
27 | }
28 |
--------------------------------------------------------------------------------
/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:\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 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/app/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
4 |
5 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
16 |
17 |
--------------------------------------------------------------------------------
/app/src/main/java/com/dyzs/app/activity/MainActivity.java:
--------------------------------------------------------------------------------
1 | package com.dyzs.app.activity;
2 |
3 | import android.os.Handler;
4 | import android.os.HandlerThread;
5 | import android.os.Message;
6 | import android.os.Process;
7 | import android.support.v7.app.AppCompatActivity;
8 | import android.os.Bundle;
9 |
10 | import com.dyzs.app.compassservant.R;
11 | import com.dyzs.compassservant.CompassServant;
12 |
13 | public class MainActivity extends AppCompatActivity implements CompassServant.ServantListener {
14 |
15 | private HandlerThread mHandlerThread;
16 | private String mHtName = "compass_servant";
17 | private Handler mLooper;
18 | private Handler mUIHandler;
19 | private static int MESSAGE = 0x110;
20 | CompassServant compass_servant;
21 | @Override
22 | protected void onCreate(Bundle savedInstanceState) {
23 | super.onCreate(savedInstanceState);
24 | setContentView(R.layout.activity_main);
25 | initHandlerThread();
26 | compass_servant = (CompassServant) findViewById(R.id.compass_servant);
27 | compass_servant.setServantListener(this);
28 | compass_servant.setPointerDecibel(118);
29 | }
30 |
31 |
32 | private void initHandlerThread() {
33 | mUIHandler = new Handler();
34 | mHandlerThread = new HandlerThread(mHtName, Process.THREAD_PRIORITY_DEFAULT);
35 | mHandlerThread.start();
36 | mLooper = new Handler(mHandlerThread.getLooper()) {
37 | @Override
38 | public void handleMessage(Message msg) {
39 | super.handleMessage(msg);
40 | if (msg.what == MESSAGE && i > 0) {
41 | doWithMainUI();
42 | i--;
43 | }
44 | }
45 | };
46 | }
47 | private int i = 1000;
48 | private void doWithMainUI() {
49 | try {
50 | mUIHandler.post(new Runnable() {
51 | @Override
52 | public void run() {
53 | Double d = Math.random() * 89;
54 | int iRandom = d.intValue() + 30;
55 | compass_servant.setPointerDecibel(iRandom);
56 | }
57 | });
58 | } catch (Exception e) {
59 |
60 | }
61 | }
62 |
63 | @Override
64 | public void onDestroy() {
65 | mHandlerThread.quit();
66 | super.onDestroy();
67 | }
68 |
69 | @Override
70 | public void startTension() {
71 | mLooper.sendEmptyMessage(MESSAGE);
72 | }
73 | }
74 |
--------------------------------------------------------------------------------
/app/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
2 |
7 |
8 |
15 |
16 |
33 |
34 |
35 |
36 |
37 |
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/app/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-hdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/app/src/main/res/mipmap-hdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/app/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-mdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/app/src/main/res/mipmap-mdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/app/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/app/src/main/res/mipmap-xhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/app/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/app/src/main/res/mipmap-xxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/app/src/main/res/mipmap-xxxhdpi/ic_launcher_round.png
--------------------------------------------------------------------------------
/app/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 |
8 |
--------------------------------------------------------------------------------
/app/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | CompassServant
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 | //maven {
6 | // url "https://plugins.gradle.org/m2/"
7 | //}
8 | jcenter()
9 | mavenCentral()
10 | google()
11 | }
12 | dependencies {
13 | classpath 'com.android.tools.build:gradle:3.0.1'//3.0.1'
14 | classpath 'com.github.dcendents:android-maven-gradle-plugin:2.0'
15 | classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'//7.3'
16 | // NOTE: Do not place your application dependencies here; they belong
17 | // in the individual module build.gradle files
18 | }
19 | }
20 |
21 | allprojects {
22 | repositories {
23 | jcenter()
24 | mavenCentral()
25 | google()
26 | maven {
27 | url 'https://bintray.com/dyzs/maven/'
28 | }
29 | }
30 | }
31 |
32 | task clean(type: Delete) {
33 | delete rootProject.buildDir
34 | }
35 |
--------------------------------------------------------------------------------
/compassservant/.gitignore:
--------------------------------------------------------------------------------
1 | /build
2 |
--------------------------------------------------------------------------------
/compassservant/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 | apply plugin: 'com.github.dcendents.android-maven'
3 | apply plugin: 'com.jfrog.bintray'
4 |
5 | version = "1.0.0"
6 | android {
7 | compileSdkVersion 26
8 | buildToolsVersion "26.0.2"
9 | resourcePrefix "CompassServant"
10 |
11 | defaultConfig {
12 | minSdkVersion 21
13 | targetSdkVersion 26
14 | versionCode 1
15 | versionName "1.0"
16 |
17 | }
18 | buildTypes {
19 | repositories {
20 | mavenCentral()
21 | google()
22 | }
23 | release {
24 | minifyEnabled false
25 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
26 | }
27 | }
28 | }
29 |
30 | dependencies {
31 | implementation fileTree(dir: 'libs', include: ['*.jar'])
32 | implementation 'com.android.support:appcompat-v7:26.0.2'
33 | implementation 'com.github.dcendents:android-maven-gradle-plugin:2.0'
34 | implementation 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7.3'
35 | }
36 | def siteUrl = 'https://github.com/dyzs/CompassServant' // 项目的主页
37 | def gitUrl = 'https://github.com/dyzs/CompassServant.git' // Git仓库的url
38 | group = "com.dyzs.compassservant" // Maven Group ID for the artifact,一般填你唯一的包名
39 |
40 | install {
41 | repositories.mavenInstaller {
42 | // This generates POM.xml with proper parameters
43 | pom {
44 | project {
45 | packaging 'aar'
46 | // Add your description here
47 | name 'Android Compass Servant' //项目描述
48 | url siteUrl
49 | // Set your license
50 | licenses {
51 | license {
52 | name 'The Apache Software License, Version 2.0'
53 | url 'http://www.apache.org/licenses/LICENSE-2.0.txt'
54 | }
55 | }
56 | developers {
57 | developer {
58 | id 'dyzs' //填写开发者基本信息
59 | name 'Akuma Lucifer'
60 | email 'chenbikun627370404@gmail.com'
61 | }
62 | }
63 | scm {
64 | connection gitUrl
65 | developerConnection gitUrl
66 | url siteUrl
67 | }
68 | }
69 | }
70 | }
71 | }
72 | task sourcesJar(type: Jar) {
73 | from android.sourceSets.main.java.srcDirs
74 | classifier = 'sources'
75 | }
76 | task javadoc(type: Javadoc) {
77 | options.encoding "UTF-8"
78 | options.charSet 'UTF-8'
79 | options.author true
80 | options.version true
81 | options.links "https://github.com/dyzs/CompassServant"
82 | failOnError false
83 | source = android.sourceSets.main.java.srcDirs
84 | classpath += project.files(android.getBootClasspath().join(File.pathSeparator))
85 | }
86 | task javadocJar(type: Jar, dependsOn: javadoc) {
87 | classifier = 'javadoc'
88 | from javadoc.destinationDir
89 | }
90 | artifacts {
91 | archives javadocJar
92 | archives sourcesJar
93 | }
94 | Properties properties = new Properties()
95 | properties.load(project.rootProject.file('local.properties').newDataInputStream())
96 | bintray {
97 | user = properties.getProperty("bintray.user")
98 | key = properties.getProperty("bintray.apikey")
99 | configurations = ['archives']
100 | pkg {
101 | // 发布到Bintray的那个仓库里,默认账户有四个库,我们这里上传到maven库,
102 | // 需要在自己的Bintray先创建一个maven,可以自定义命名
103 | repo = "maven"
104 | name = "CompassServant" //发布到Bintray上的项目名字
105 | websiteUrl = siteUrl
106 | vcsUrl = gitUrl
107 | licenses = ["Apache-2.0"]
108 | publish = true
109 | }
110 | }
111 |
--------------------------------------------------------------------------------
/compassservant/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 C:\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 |
19 | # Uncomment this to preserve the line number information for
20 | # debugging stack traces.
21 | #-keepattributes SourceFile,LineNumberTable
22 |
23 | # If you keep the line number information, uncomment this to
24 | # hide the original source file name.
25 | #-renamesourcefileattribute SourceFile
26 |
--------------------------------------------------------------------------------
/compassservant/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
7 |
8 |
9 |
10 |
11 |
--------------------------------------------------------------------------------
/compassservant/src/main/java/com/dyzs/compassservant/ColorUtil.java:
--------------------------------------------------------------------------------
1 | package com.dyzs.compassservant;
2 |
3 | import android.graphics.Color;
4 |
5 | import java.util.Random;
6 |
7 | public class ColorUtil {
8 | /**
9 | * 随机生成漂亮的颜色
10 | * @return
11 | */
12 | public static int randomColor() {
13 | Random random = new Random();
14 |
15 | int red = random.nextInt(150) + 50;
16 |
17 | int green = random.nextInt(150) + 50;
18 |
19 | int blue = random.nextInt(150) + 50;
20 |
21 | return Color.rgb(red, green, blue); // 根据rgb混合生成一种新的颜色
22 | }
23 |
24 | /**
25 | * 随机生成漂亮的颜色,带透明度的
26 | * @return
27 | */
28 | public static int randomColorArgb() {
29 | Random random = new Random();
30 |
31 | int alpha = random.nextInt(70) + 30;
32 |
33 | int red = random.nextInt(150) + 50;
34 |
35 | int green = random.nextInt(150) + 50;
36 |
37 | int blue = random.nextInt(150) + 50;
38 |
39 | return Color.argb(alpha, red, green, blue); // 根据argb混合生成一种新的颜色
40 | }
41 |
42 |
43 | /**
44 | * 颜色与上一个十六进制数ARGB,得到一个颜色加深的效果,效果从 0-F 深
45 | * @param color
46 | * @return
47 | */
48 | public static int getColorDeeply(int color) {
49 | // | 0xF0000000 & 0xFFF5F5F5
50 | return color & 0xFFDDDDDD;
51 | }
52 |
53 |
54 | /**
55 | * 颜色值取反
56 | * @param color
57 | * @return
58 | */
59 | public static int getColorReverse(int color) {
60 | // String string = String.format("#%x", color); // string reverse
61 | int red = 255 - Color.red(color);
62 | int green = 255 - Color.green(color);
63 | int blue = 255 - Color.blue(color);
64 | return Color.argb(255, red, green, blue);
65 | }
66 |
67 | /**
68 | * 获取两个颜色值之间渐变的某个点的颜色值
69 | * @param resSColor
70 | * @param resEColor
71 | * @param rangeColorRate
72 | * @return
73 | */
74 | public static int getCompositeColor(int resSColor, int resEColor, float rangeColorRate) {
75 | int sc = resSColor;
76 | int ec = resEColor;
77 | int rS = Color.red(sc);
78 | int gS = Color.green(sc);
79 | int bS = Color.blue(sc);
80 | int rE = Color.red(ec);
81 | int gE = Color.green(ec);
82 | int bE = Color.blue(ec);
83 | int r = (int) (rS + (rE - rS) * 1f * rangeColorRate);
84 | int g = (int) (gS + (gE - gS) * 1f * rangeColorRate);
85 | int b = (int) (bS + (bE - bS) * 1f * rangeColorRate);
86 | return Color.argb(255, r, g, b);
87 | }
88 |
89 | }
90 |
--------------------------------------------------------------------------------
/compassservant/src/main/java/com/dyzs/compassservant/CompassServant.java:
--------------------------------------------------------------------------------
1 | package com.dyzs.compassservant;
2 |
3 | import android.animation.Animator;
4 | import android.animation.ValueAnimator;
5 | import android.content.Context;
6 | import android.content.res.TypedArray;
7 | import android.graphics.Canvas;
8 | import android.graphics.Paint;
9 | import android.graphics.Path;
10 | import android.graphics.RectF;
11 | import android.graphics.SweepGradient;
12 | import android.support.annotation.Nullable;
13 | import android.support.v4.content.ContextCompat;
14 | import android.util.AttributeSet;
15 | import android.util.TypedValue;
16 | import android.view.MotionEvent;
17 | import android.view.View;
18 | import android.view.animation.DecelerateInterpolator;
19 |
20 | /**
21 | * @author dyzs
22 | * Created on 2018/1/9.
23 | * 自定义命名空间
24 | * xmlns:dyzs="http://schemas.android.com/apk/res/com.dyzs.common.ui.CompassServant"
25 | */
26 |
27 | public class CompassServant extends View{
28 | private static final String TAG = CompassServant.class.getSimpleName();
29 | private Context mCtx;// god of the universal
30 | private float mWidth, mHeight;
31 | private float mPadding;// 外圆相对于 view 的间距
32 | private float mSpacing;// 刻度与外圆, 刻度与内弧之间的间隔 the abyss between tick mark and outer circle
33 | private float[] mCircleCenter = new float[2];// view 的圆心点center of the universal
34 | private float mOuterCircleRadius;// 圆半径
35 | private float mPointerRadius;// 指针半径
36 | private float mTickRadius;// 刻度半径
37 | private float mInnerArcRadius;// 内弧半径
38 | private float mInnerArcDegree;// 内弧的度数
39 | private float mPerDegree;// 刻度值的平均度数
40 | private float mPointerDegree;// 当前指针刻度
41 | private RectF mPointerRectF, mTickRectF, mInnerArcRectF, mTextRectF;
42 | private float mCircleWidth;// 外圆的宽度 outer circle width
43 | private float mTickLength;// 刻度线的长度 tick mark pointer length
44 | private float mTickWidth;// 刻度线的宽度 tick mark pointer width
45 | private float mInnerArcWidth;// 内弧的宽度 color gradient width
46 | private float mPointerWidth;// 当前指针宽度 pointer width
47 | private Paint mOuterCirclePaint;// outer circle paint
48 | private Paint mTickPaint;// tick mark paint
49 | private Paint mInnerArcPaint;// color gradient paint
50 | private Paint mPointerPaint;// pointer paint
51 | private Paint mTextPaint, mTrianglePaint;
52 | private float mStartAngle;// 画布绘圆的开始角度
53 | private int mDecibel;// 总刻度数 tick mark total count
54 | private int[] mInnerArcGradientColors;// 内圆弧的渐变颜色值
55 | private float[] mGalaxyPositions;// could't be authorized
56 | private int mC1, mC2, mC3, mC4;// 内圆弧的颜色1,2,3,4
57 | private int mCCommander;// xml设置属性时,控制显示的颜色个数 command display colors, value limits[2~4]
58 | private int mBackgroundColor;// 默认获取背景颜色
59 | private float mLeapTextSize;// 当前显示刻度的文本的大小
60 | private static final int[] SYS_ATTRS = new int[]{
61 | android.R.attr.background,
62 | android.R.attr.padding
63 | };// got new skill
64 | @Deprecated
65 | private Path mTrianglePath;
66 |
67 | public CompassServant(Context context) {
68 | this(context, null);
69 | }
70 |
71 | public CompassServant(Context context, AttributeSet attrs) {
72 | this(context, attrs, -1);
73 | }
74 |
75 | public CompassServant(Context context, AttributeSet attrs, int defStyleAttr) {
76 | this(context, attrs, defStyleAttr, -1);
77 | }
78 |
79 | public CompassServant(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
80 | super(context, attrs, defStyleAttr, defStyleRes);
81 | init(context, attrs, defStyleAttr, defStyleRes);
82 | // startPointerAnim();
83 | }
84 |
85 | private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
86 | mCtx = context;
87 | /* get system attr */
88 | TypedArray ta = context.obtainStyledAttributes(attrs, SYS_ATTRS);
89 | mBackgroundColor = ta.getColor(0, ContextCompat.getColor(context, R.color.black));
90 | mPadding = ta.getDimension(1, dp2Px(10));
91 | ta.recycle();
92 | setBackgroundColor(mBackgroundColor);
93 |
94 | ta = context.obtainStyledAttributes(attrs, R.styleable.CompassServant, defStyleAttr, defStyleRes);
95 | mCCommander = ta.getInt(R.styleable.CompassServant_cs_color_commander, 3);
96 | mC1 = ta.getColor(R.styleable.CompassServant_cs_color1, ContextCompat.getColor(context, R.color.white));
97 | mC2 = ta.getColor(R.styleable.CompassServant_cs_color2, ContextCompat.getColor(context, R.color.oxygen_green));
98 | mC3 = ta.getColor(R.styleable.CompassServant_cs_color3, ContextCompat.getColor(context, R.color.cinnabar_red));
99 | mC4 = ta.getColor(R.styleable.CompassServant_cs_color4, ContextCompat.getColor(context, R.color.pale_blue));
100 | mDecibel = ta.getInteger(R.styleable.CompassServant_cs_decibel, 119);
101 | mTickLength = ta.getDimension(R.styleable.CompassServant_cs_tick_mark_length, 80f);
102 | mCircleWidth = ta.getDimension(R.styleable.CompassServant_cs_outer_circle, 20f);
103 | mInnerArcDegree = ta.getFloat(R.styleable.CompassServant_cs_galaxy_degree, 280f);
104 | mLeapTextSize = ta.getDimension(R.styleable.CompassServant_cs_text_size, 50f);
105 | ta.recycle();
106 |
107 | mSpacing = 15f;
108 | mInnerArcDegree = mInnerArcDegree % 361f;
109 | mPerDegree = mInnerArcDegree / mDecibel;
110 | mStartAngle = (360f - mInnerArcDegree) / 2 + 90f;
111 | mPointerDegree = 280f; // def degree value
112 | mInnerArcWidth = mCircleWidth * 1.5f;
113 | mTickWidth = (float) (mPerDegree / 4.5f * 2 * Math.PI);
114 | mPointerWidth = mTickWidth * 2;
115 |
116 | setInnerArcColors(calcInitColors());
117 |
118 | mOuterCirclePaint = new Paint();
119 | mOuterCirclePaint.setAntiAlias(true);
120 | mOuterCirclePaint.setStyle(Paint.Style.STROKE);
121 | mOuterCirclePaint.setStrokeWidth(mCircleWidth);
122 | mOuterCirclePaint.setColor(ContextCompat.getColor(context, R.color.tension_grey));
123 |
124 | mTickPaint = new Paint();
125 | mTickPaint.setAntiAlias(true);
126 | mTickPaint.setStyle(Paint.Style.STROKE);
127 | mTickPaint.setStrokeWidth(mTickWidth);
128 | mTickPaint.setColor(ContextCompat.getColor(context, R.color.tension_grey));
129 |
130 | mInnerArcPaint = new Paint();
131 | mInnerArcPaint.setAntiAlias(true);
132 | mInnerArcPaint.setStyle(Paint.Style.STROKE);
133 | mInnerArcPaint.setStrokeWidth(mInnerArcWidth);
134 | mInnerArcPaint.setColor(ContextCompat.getColor(context, R.color.girl_pink));
135 |
136 | mPointerPaint = new Paint();
137 | mPointerPaint.setAntiAlias(true);
138 | mPointerPaint.setStyle(Paint.Style.STROKE);
139 | mPointerPaint.setStrokeWidth(mPointerWidth);
140 | mPointerPaint.setColor(ContextCompat.getColor(context, R.color.alice_blue));
141 |
142 | mTextPaint = new Paint();
143 | mTextPaint.setAntiAlias(true);
144 | mTextPaint.setColor(mBackgroundColor);
145 | mTextPaint.setStrokeWidth(4f);
146 | mTextPaint.setTextSize(mLeapTextSize);
147 |
148 | mTrianglePaint = new Paint();
149 | mTrianglePaint.setAntiAlias(true);
150 | mTrianglePaint.setColor(ContextCompat.getColor(context, R.color.tension_grey));
151 | }
152 |
153 | private int[] calcInitColors() {
154 | mCCommander = mCCommander % 5;
155 | if (mCCommander < 2) {mCCommander = 2;}
156 | int[] retColors = new int[mCCommander], colors = new int[]{mC1, mC2, mC3, mC4};
157 | // System.arraycopy(colors, 0, retColors, 0, mCCommander);
158 | for (int i = 0; i < mCCommander; i++) {
159 | retColors[i] = colors[i];
160 | }
161 | return retColors;
162 | }
163 |
164 | @Override
165 | protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
166 | super.onMeasure(widthMeasureSpec, heightMeasureSpec);
167 | System.out.println("on measure");
168 | mWidth = getMeasuredWidth();mHeight = getMeasuredHeight();
169 | if (mWidth >= mHeight) {
170 | mOuterCircleRadius = mHeight / 2 - mPadding;
171 | } else {
172 | mOuterCircleRadius = mWidth / 2 - mPadding;
173 | }
174 | mCircleCenter[0] = mWidth / 2; mCircleCenter[1] = mHeight / 2;
175 |
176 | mPointerRadius = mOuterCircleRadius - mCircleWidth / 2;
177 | float l, t, r, b;
178 | l = mCircleCenter[0] - mPointerRadius;
179 | t = mCircleCenter[1] - mPointerRadius;
180 | r = mCircleCenter[0] + mPointerRadius;
181 | b = mCircleCenter[1] + mPointerRadius;
182 | mPointerRectF = new RectF(l, t, r, b);
183 |
184 | mTickRadius = mOuterCircleRadius - mCircleWidth - mTickLength / 2 - mSpacing;
185 | l = mCircleCenter[0] - mTickRadius;
186 | t = mCircleCenter[1] - mTickRadius;
187 | r = mCircleCenter[0] + mTickRadius;
188 | b = mCircleCenter[1] + mTickRadius;
189 | mTickRectF = new RectF(l, t, r, b);
190 |
191 | mInnerArcRadius = mOuterCircleRadius - mCircleWidth - mTickLength - mInnerArcWidth / 2 - mSpacing * 2;
192 | l = mCircleCenter[0] - mInnerArcRadius;
193 | t = mCircleCenter[1] - mInnerArcRadius;
194 | r = mCircleCenter[0] + mInnerArcRadius;
195 | b = mCircleCenter[1] + mInnerArcRadius;
196 | mInnerArcRectF = new RectF(l, t, r, b);
197 |
198 | l += mInnerArcWidth;
199 | t += mInnerArcWidth;
200 | r -= mInnerArcWidth;
201 | b -= mInnerArcWidth;
202 | mTextRectF = new RectF(l, t, r, b);
203 |
204 | mTrianglePath = new Path();
205 | mTrianglePath.moveTo(mCircleCenter[0] - mPadding / 2, mPadding / 4);
206 | mTrianglePath.lineTo(mCircleCenter[0] + mPadding / 2, mPadding / 4);
207 | mTrianglePath.lineTo(mCircleCenter[0], mPadding - mPadding / 4);
208 | }
209 |
210 | @Override
211 | protected void onSizeChanged(int w, int h, int oldw, int oldh) {
212 | super.onSizeChanged(w, h, oldw, oldh);
213 | System.out.println("on size changed");
214 | }
215 |
216 | @Override
217 | protected void onDraw(Canvas canvas) {
218 | super.onDraw(canvas);
219 | drawDarkFlameMaster(canvas);
220 | drawInnerArc(canvas);
221 | }
222 |
223 | /**
224 | * 画外圆, 计算当前指针角度对应的刻度, 遍历旋转绘制刻度
225 | * @param canvas
226 | */
227 | private void drawDarkFlameMaster(Canvas canvas) {
228 | /* draw circle */
229 | canvas.drawCircle(mCircleCenter[0], mCircleCenter[1], mPointerRadius, mOuterCirclePaint);
230 |
231 | /* draw tick mark, triangle mark and color pointer */
232 | int dBPointer = (int) (mPointerDegree * mDecibel / mInnerArcDegree);
233 | for (int i = 0; i <= mDecibel; i++) {
234 | canvas.save();
235 | float rotateDegree;
236 | rotateDegree = mStartAngle + 90 + mPerDegree * i;
237 | canvas.rotate(rotateDegree, mCircleCenter[0], mCircleCenter[1]);
238 | if (i <= dBPointer) {
239 | mTickPaint.setColor(getPointerColor(i));//ContextCompat.getColor(mCtx, R.color.blair_grey));
240 | canvas.drawLine(
241 | mCircleCenter[0],
242 | mTickRectF.top - mTickLength / 2,
243 | mCircleCenter[0],
244 | mTickRectF.top + mTickLength / 2,
245 | mTickPaint);
246 | if (i == dBPointer) {
247 | mPointerPaint.setColor(getPointerColor(i));
248 | canvas.drawLine(
249 | mCircleCenter[0],
250 | mPadding,
251 | mCircleCenter[0],
252 | mTickRectF.top + mTickLength / 2,
253 | mPointerPaint);
254 | // canvas.drawPath(mTrianglePath, mTrianglePaint);
255 | }
256 | } else {
257 | mTickPaint.setColor(ContextCompat.getColor(mCtx, R.color.tension_grey));
258 | canvas.drawLine(
259 | mCircleCenter[0],
260 | mTickRectF.top - mTickLength / 2,
261 | mCircleCenter[0],
262 | mTickRectF.top + mTickLength / 2,
263 | mTickPaint);
264 | }
265 | canvas.restore();
266 | }
267 | drawLeapText(canvas, dBPointer + 1);
268 | }
269 |
270 | private void drawLeapText(Canvas canvas, int decibel) {
271 | String text = decibel + "";
272 | mTextPaint.setTextSize(mLeapTextSize);
273 | mTextPaint.setColor(mBackgroundColor);
274 | canvas.drawCircle(mCircleCenter[0], mCircleCenter[1], Math.abs(mTextRectF.bottom - mTextRectF.top) / 2, mTextPaint);
275 | mTextPaint.setColor(ColorUtil.getColorReverse(mBackgroundColor));
276 | float textWidth = mTextPaint.measureText(text) * 1.0f;
277 | float textHalfHeight = FontMatrixUtils.calcTextHalfHeightPoint(mTextPaint);
278 | canvas.drawText(text, mCircleCenter[0] - textWidth / 2, mCircleCenter[1] + textHalfHeight / 2, mTextPaint);
279 | mTextPaint.setTextSize(mLeapTextSize / 2);
280 | canvas.drawText("dB", mCircleCenter[0] + textWidth / 2, mCircleCenter[1] + textHalfHeight / 2, mTextPaint);
281 | }
282 |
283 | private void drawInnerArc(Canvas canvas) {
284 | /* draw colorful gradient */
285 | SweepGradient sweepGradient = new SweepGradient(
286 | mCircleCenter[0],
287 | mCircleCenter[1],
288 | mInnerArcGradientColors,
289 | mGalaxyPositions);
290 | mInnerArcPaint.setShader(sweepGradient);
291 | Path path = new Path();
292 | canvas.save();
293 | canvas.rotate(mStartAngle, mCircleCenter[0], mCircleCenter[1]);
294 | path.addArc(mInnerArcRectF, 0, mInnerArcDegree);
295 | canvas.drawPath(path, mInnerArcPaint);
296 | canvas.restore();
297 | }
298 |
299 | /**
300 | * 重置当前指针并开始动画
301 | * reset current pointer and start anim
302 | * @param value
303 | */
304 | public void setPointerDecibel(int value) {
305 | value = value % mDecibel;
306 | float degree = mInnerArcDegree * value / mDecibel;
307 | this.mLastValue = mPointerDegree;
308 | this.mPointerDegree = degree;
309 | startPointerAnim();
310 | // invalidate();
311 | }
312 |
313 | /**
314 | * SweepGradient 颜色值对应的1f:360f(圆的角度)
315 | * pointerDegreeRate 计算解释:pointerTick/totalPointer对应了颜色值的渐变,
316 | * 因为当前圆可以设置为存在缺口的圆弧, 所以按照比例同成于圆弧 galaxyDegree/360f(圆的角度)
317 | * 颜色区间计算:通过上述得到的float值, 去校验当前指针在 positions 的哪个区间, 然后换算当前颜色值RGB
318 | */
319 | public int getPointerColor(int pointerTick) {
320 | if (mInnerArcGradientColors.length == 1) {
321 | return mInnerArcGradientColors[0];
322 | }
323 | float degreeRate = mInnerArcDegree / 360f;
324 | float pointerDegreeRate = pointerTick * 1f / (mDecibel + 1) * degreeRate;
325 | int resSColor = ContextCompat.getColor(mCtx, R.color.white);
326 | int resEColor = ContextCompat.getColor(mCtx, R.color.oxygen_green);
327 | float rangeColorRate = 0f;
328 | for (int i = 0 ; i < mGalaxyPositions.length; i++) {
329 | if (i == 0) {
330 | resSColor = mInnerArcGradientColors[0];
331 | resEColor = mInnerArcGradientColors[1];
332 | continue;
333 | }
334 | if (pointerDegreeRate < mGalaxyPositions[i]) {
335 | float s = mGalaxyPositions[i-1];
336 | float e = mGalaxyPositions[i];
337 | rangeColorRate = (pointerDegreeRate - s) / (e - s);
338 | resSColor = mInnerArcGradientColors[i-1];
339 | resEColor = mInnerArcGradientColors[i];
340 | break;
341 | }
342 | }
343 | return ColorUtil.getCompositeColor(resSColor, resEColor, rangeColorRate);
344 | }
345 |
346 | private ValueAnimator mAnimator;
347 | private float mLastValue = 0f;
348 | private void startPointerAnim() {
349 | long duration = (long) (10 * Math.abs(mPointerDegree - mLastValue));
350 | mAnimator = ValueAnimator.ofFloat(mLastValue, mPointerDegree);
351 | mAnimator.setDuration(duration);
352 | mAnimator.setInterpolator(new DecelerateInterpolator());
353 | mAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
354 | @Override
355 | public void onAnimationUpdate(ValueAnimator animation) {
356 | mPointerDegree = (float) animation.getAnimatedValue();
357 | postInvalidate();
358 | }
359 | });
360 | mAnimator.addListener(new Animator.AnimatorListener() {
361 | @Override
362 | public void onAnimationStart(Animator animation) {
363 |
364 | }
365 |
366 | @Override
367 | public void onAnimationEnd(Animator animation) {
368 | if (listener != null) {
369 | listener.startTension();
370 | }
371 | }
372 |
373 | @Override
374 | public void onAnimationCancel(Animator animation) {
375 |
376 | }
377 |
378 | @Override
379 | public void onAnimationRepeat(Animator animation) {
380 |
381 | }
382 | });
383 | mAnimator.start();
384 | }
385 |
386 | private ServantListener listener;
387 | public void setServantListener(ServantListener listener) {
388 | this.listener = listener;
389 | }
390 | public interface ServantListener {
391 | void startTension();
392 | }
393 |
394 | public int[] getGalaxyColors() {
395 | return mInnerArcGradientColors;
396 | }
397 |
398 | /**
399 | * 设置颜色的时候同时计算 positions
400 | * !不处理任何颜色值设置异常
401 | * set color gradient and automatic calculate positions
402 | * not care about the color with positions exceptions, 'cause the os will handle with it
403 | */
404 | public void setInnerArcColors(@Nullable int[] colors) {
405 | if (colors == null) {
406 | colors = new int[] {
407 | ContextCompat.getColor(mCtx, R.color.white),
408 | ContextCompat.getColor(mCtx, R.color.oxygen_green),
409 | ContextCompat.getColor(mCtx, R.color.cinnabar_red)
410 | };
411 | }
412 | this.mInnerArcGradientColors = colors;
413 | setPositions(null);
414 | invalidate();
415 | }
416 |
417 | /* not allow use */
418 | private float[] getGalaxyPositions() {
419 | return mGalaxyPositions;
420 | }
421 |
422 | /* not allow use */
423 |
424 | /**
425 | * calc per color to per position
426 | * @param positions
427 | */
428 | private void setPositions(@Nullable float[] positions) {
429 | float degreeRate = mInnerArcDegree / 360f;
430 | if (positions == null) {// set positions average allocation
431 | positions = new float[mInnerArcGradientColors.length];
432 | for (int i = 0; i < positions.length; i++) {
433 | positions[i] = i * degreeRate / (positions.length - 1);
434 | }
435 | } else {// use degree rate while reset positions
436 | for (int i = 0; i < positions.length; i++) {
437 | positions[i] = positions[i] * degreeRate;
438 | }
439 | }
440 | this.mGalaxyPositions = positions;
441 | }
442 |
443 | private float dp2Px(float dp) {
444 | return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, mCtx.getResources().getDisplayMetrics());
445 | }
446 |
447 | @Override
448 | public boolean onTouchEvent(MotionEvent event) {
449 | return super.onTouchEvent(event);
450 | }
451 | }
452 |
--------------------------------------------------------------------------------
/compassservant/src/main/java/com/dyzs/compassservant/FontMatrixUtils.java:
--------------------------------------------------------------------------------
1 | package com.dyzs.compassservant;
2 |
3 | import android.graphics.Paint;
4 |
5 | /**
6 | * @author dyzs
7 | * Created on 2016/3/24.
8 | */
9 | public class FontMatrixUtils {
10 | private FontMatrixUtils() {
11 | throw new UnsupportedOperationException("font matrix can't be initialized");
12 | }
13 | /**
14 | * @details 准确的计算画笔绘制文字的Y轴的正确坐标,这个坐标计算的基础为 paint X = 0,y = 0
15 | * @param paint
16 | * @return
17 | */
18 | public static float calcTextCenterVerticalBaselineY(Paint paint) {
19 | Paint.FontMetrics fm = paint.getFontMetrics();
20 | float ret = paint.getTextSize() / 2 - fm.descent
21 | + (fm.bottom - fm.top) / 2;
22 | return ret;
23 | }
24 |
25 | public static float calcTextHalfHeightPoint (Paint paint) {
26 | Paint.FontMetrics fm = paint.getFontMetrics();
27 | float ret = (fm.bottom - fm.top) / 2;
28 | return ret;
29 | }
30 | }
31 |
--------------------------------------------------------------------------------
/compassservant/src/main/res/values/attrs.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
--------------------------------------------------------------------------------
/compassservant/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | #3F51B5
4 | #303F9F
5 | #FF4081
6 |
7 | #000000
8 | #FFFFFF
9 | #F15250
10 | #35D599
11 | #7bbef7
12 | #55666666
13 | #F3A5AF
14 | #80BFF5
15 | #BDBDBD
16 |
17 | #FC2572
18 | #FF2B66
19 | #FF9911
20 | #510069
21 |
22 |
--------------------------------------------------------------------------------
/compassservant/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
2 | CompassServant
3 |
4 |
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.jar:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Mon Jan 15 20:52:34 CST 2018
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-4.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', ':compassservant'
2 |
--------------------------------------------------------------------------------
/video/compass_servant.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/video/compass_servant.gif
--------------------------------------------------------------------------------
/video/compass_servant.mp4:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/dyzs/CompassServant/feb3c55a1a78a21d84959301c543725a00ec27f6/video/compass_servant.mp4
--------------------------------------------------------------------------------