├── .gitignore
├── CHANGELOG.md
├── LICENSE.txt
├── README.md
├── art
└── speechrecognitionview.gif
├── build.gradle
├── gradle.properties
├── gradle
└── wrapper
│ ├── gradle-wrapper.jar
│ └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── settings.gradle
├── speechrecognitionview-sample
├── build.gradle
└── src
│ └── main
│ ├── AndroidManifest.xml
│ ├── java
│ └── com
│ │ └── github
│ │ └── zagum
│ │ └── speechrecognitionview
│ │ └── sample
│ │ └── 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
│ ├── colors.xml
│ ├── strings.xml
│ └── styles.xml
└── speechrecognitionview
├── build.gradle
└── src
└── main
├── AndroidManifest.xml
└── java
└── com
└── github
└── zagum
└── speechrecognitionview
├── RecognitionBar.java
├── RecognitionProgressView.java
├── adapters
└── RecognitionListenerAdapter.java
└── animators
├── BarParamsAnimator.java
├── BarRmsAnimator.java
├── IdleAnimator.java
├── RmsAnimator.java
├── RotatingAnimator.java
└── TransformAnimator.java
/.gitignore:
--------------------------------------------------------------------------------
1 | # IntelliJ IDEA
2 | .idea
3 | *.iml
4 |
5 | # Gradle
6 | .gradle/
7 | build/
8 |
9 | # Local configuration file (sdk path, etc)
10 | local.properties
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | Change Log
2 | ==========
3 |
4 |
5 | ## Version 1.0.1
6 |
7 | _2016-04-30_
8 |
9 | Initial release.
10 |
--------------------------------------------------------------------------------
/LICENSE.txt:
--------------------------------------------------------------------------------
1 |
2 | Apache License
3 | Version 2.0, January 2004
4 | http://www.apache.org/licenses/
5 |
6 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7 |
8 | 1. Definitions.
9 |
10 | "License" shall mean the terms and conditions for use, reproduction,
11 | and distribution as defined by Sections 1 through 9 of this document.
12 |
13 | "Licensor" shall mean the copyright owner or entity authorized by
14 | the copyright owner that is granting the License.
15 |
16 | "Legal Entity" shall mean the union of the acting entity and all
17 | other entities that control, are controlled by, or are under common
18 | control with that entity. For the purposes of this definition,
19 | "control" means (i) the power, direct or indirect, to cause the
20 | direction or management of such entity, whether by contract or
21 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
22 | outstanding shares, or (iii) beneficial ownership of such entity.
23 |
24 | "You" (or "Your") shall mean an individual or Legal Entity
25 | exercising permissions granted by this License.
26 |
27 | "Source" form shall mean the preferred form for making modifications,
28 | including but not limited to software source code, documentation
29 | source, and configuration files.
30 |
31 | "Object" form shall mean any form resulting from mechanical
32 | transformation or translation of a Source form, including but
33 | not limited to compiled object code, generated documentation,
34 | and conversions to other media types.
35 |
36 | "Work" shall mean the work of authorship, whether in Source or
37 | Object form, made available under the License, as indicated by a
38 | copyright notice that is included in or attached to the work
39 | (an example is provided in the Appendix below).
40 |
41 | "Derivative Works" shall mean any work, whether in Source or Object
42 | form, that is based on (or derived from) the Work and for which the
43 | editorial revisions, annotations, elaborations, or other modifications
44 | represent, as a whole, an original work of authorship. For the purposes
45 | of this License, Derivative Works shall not include works that remain
46 | separable from, or merely link (or bind by name) to the interfaces of,
47 | the Work and Derivative Works thereof.
48 |
49 | "Contribution" shall mean any work of authorship, including
50 | the original version of the Work and any modifications or additions
51 | to that Work or Derivative Works thereof, that is intentionally
52 | submitted to Licensor for inclusion in the Work by the copyright owner
53 | or by an individual or Legal Entity authorized to submit on behalf of
54 | the copyright owner. For the purposes of this definition, "submitted"
55 | means any form of electronic, verbal, or written communication sent
56 | to the Licensor or its representatives, including but not limited to
57 | communication on electronic mailing lists, source code control systems,
58 | and issue tracking systems that are managed by, or on behalf of, the
59 | Licensor for the purpose of discussing and improving the Work, but
60 | excluding communication that is conspicuously marked or otherwise
61 | designated in writing by the copyright owner as "Not a Contribution."
62 |
63 | "Contributor" shall mean Licensor and any individual or Legal Entity
64 | on behalf of whom a Contribution has been received by Licensor and
65 | subsequently incorporated within the Work.
66 |
67 | 2. Grant of Copyright License. Subject to the terms and conditions of
68 | this License, each Contributor hereby grants to You a perpetual,
69 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70 | copyright license to reproduce, prepare Derivative Works of,
71 | publicly display, publicly perform, sublicense, and distribute the
72 | Work and such Derivative Works in Source or Object form.
73 |
74 | 3. Grant of Patent License. Subject to the terms and conditions of
75 | this License, each Contributor hereby grants to You a perpetual,
76 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77 | (except as stated in this section) patent license to make, have made,
78 | use, offer to sell, sell, import, and otherwise transfer the Work,
79 | where such license applies only to those patent claims licensable
80 | by such Contributor that are necessarily infringed by their
81 | Contribution(s) alone or by combination of their Contribution(s)
82 | with the Work to which such Contribution(s) was submitted. If You
83 | institute patent litigation against any entity (including a
84 | cross-claim or counterclaim in a lawsuit) alleging that the Work
85 | or a Contribution incorporated within the Work constitutes direct
86 | or contributory patent infringement, then any patent licenses
87 | granted to You under this License for that Work shall terminate
88 | as of the date such litigation is filed.
89 |
90 | 4. Redistribution. You may reproduce and distribute copies of the
91 | Work or Derivative Works thereof in any medium, with or without
92 | modifications, and in Source or Object form, provided that You
93 | meet the following conditions:
94 |
95 | (a) You must give any other recipients of the Work or
96 | Derivative Works a copy of this License; and
97 |
98 | (b) You must cause any modified files to carry prominent notices
99 | stating that You changed the files; and
100 |
101 | (c) You must retain, in the Source form of any Derivative Works
102 | that You distribute, all copyright, patent, trademark, and
103 | attribution notices from the Source form of the Work,
104 | excluding those notices that do not pertain to any part of
105 | the Derivative Works; and
106 |
107 | (d) If the Work includes a "NOTICE" text file as part of its
108 | distribution, then any Derivative Works that You distribute must
109 | include a readable copy of the attribution notices contained
110 | within such NOTICE file, excluding those notices that do not
111 | pertain to any part of the Derivative Works, in at least one
112 | of the following places: within a NOTICE text file distributed
113 | as part of the Derivative Works; within the Source form or
114 | documentation, if provided along with the Derivative Works; or,
115 | within a display generated by the Derivative Works, if and
116 | wherever such third-party notices normally appear. The contents
117 | of the NOTICE file are for informational purposes only and
118 | do not modify the License. You may add Your own attribution
119 | notices within Derivative Works that You distribute, alongside
120 | or as an addendum to the NOTICE text from the Work, provided
121 | that such additional attribution notices cannot be construed
122 | as modifying the License.
123 |
124 | You may add Your own copyright statement to Your modifications and
125 | may provide additional or different license terms and conditions
126 | for use, reproduction, or distribution of Your modifications, or
127 | for any such Derivative Works as a whole, provided Your use,
128 | reproduction, and distribution of the Work otherwise complies with
129 | the conditions stated in this License.
130 |
131 | 5. Submission of Contributions. Unless You explicitly state otherwise,
132 | any Contribution intentionally submitted for inclusion in the Work
133 | by You to the Licensor shall be under the terms and conditions of
134 | this License, without any additional terms or conditions.
135 | Notwithstanding the above, nothing herein shall supersede or modify
136 | the terms of any separate license agreement you may have executed
137 | with Licensor regarding such Contributions.
138 |
139 | 6. Trademarks. This License does not grant permission to use the trade
140 | names, trademarks, service marks, or product names of the Licensor,
141 | except as required for reasonable and customary use in describing the
142 | origin of the Work and reproducing the content of the NOTICE file.
143 |
144 | 7. Disclaimer of Warranty. Unless required by applicable law or
145 | agreed to in writing, Licensor provides the Work (and each
146 | Contributor provides its Contributions) on an "AS IS" BASIS,
147 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148 | implied, including, without limitation, any warranties or conditions
149 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150 | PARTICULAR PURPOSE. You are solely responsible for determining the
151 | appropriateness of using or redistributing the Work and assume any
152 | risks associated with Your exercise of permissions under this License.
153 |
154 | 8. Limitation of Liability. In no event and under no legal theory,
155 | whether in tort (including negligence), contract, or otherwise,
156 | unless required by applicable law (such as deliberate and grossly
157 | negligent acts) or agreed to in writing, shall any Contributor be
158 | liable to You for damages, including any direct, indirect, special,
159 | incidental, or consequential damages of any character arising as a
160 | result of this License or out of the use or inability to use the
161 | Work (including but not limited to damages for loss of goodwill,
162 | work stoppage, computer failure or malfunction, or any and all
163 | other commercial damages or losses), even if such Contributor
164 | has been advised of the possibility of such damages.
165 |
166 | 9. Accepting Warranty or Additional Liability. While redistributing
167 | the Work or Derivative Works thereof, You may choose to offer,
168 | and charge a fee for, acceptance of support, warranty, indemnity,
169 | or other liability obligations and/or rights consistent with this
170 | License. However, in accepting such obligations, You may act only
171 | on Your own behalf and on Your sole responsibility, not on behalf
172 | of any other Contributor, and only if You agree to indemnify,
173 | defend, and hold each Contributor harmless for any liability
174 | incurred by, or claims asserted against, such Contributor by reason
175 | of your accepting any such warranty or additional liability.
176 |
177 | END OF TERMS AND CONDITIONS
178 |
179 | APPENDIX: How to apply the Apache License to your work.
180 |
181 | To apply the Apache License to your work, attach the following
182 | boilerplate notice, with the fields enclosed by brackets "[]"
183 | replaced with your own identifying information. (Don't include
184 | the brackets!) The text should be enclosed in the appropriate
185 | comment syntax for the file format. We also recommend that a
186 | file or class name and description of purpose be included on the
187 | same "printed page" as the copyright notice for easier
188 | identification within third-party archives.
189 |
190 | Copyright [yyyy] [name of copyright owner]
191 |
192 | Licensed under the Apache License, Version 2.0 (the "License");
193 | you may not use this file except in compliance with the License.
194 | You may obtain a copy of the License at
195 |
196 | http://www.apache.org/licenses/LICENSE-2.0
197 |
198 | Unless required by applicable law or agreed to in writing, software
199 | distributed under the License is distributed on an "AS IS" BASIS,
200 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201 | See the License for the specific language governing permissions and
202 | limitations under the License.
203 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | SpeechRecognitionView
2 | ======================
3 |
4 | [](http://android-arsenal.com/details/1/3518)
5 | [](https://jitpack.io/#zagum/SpeechRecognitionView)
6 |
7 | "Google Now" style animation for [Speech Recognizer][1].
8 |
9 | 
10 |
11 |
12 | Compatibility
13 | -------------
14 |
15 | This library is compatible from API 15 (Android 4.0.3).
16 |
17 |
18 | Download
19 | --------
20 |
21 |
22 | Add it in your root build.gradle at the end of repositories:
23 |
24 | ```groovy
25 | allprojects {
26 | repositories {
27 | ...
28 | maven { url "https://jitpack.io" }
29 | }
30 | }
31 | ```
32 |
33 | Add the dependency
34 |
35 | ```groovy
36 | dependencies {
37 | compile 'com.github.zagum:SpeechRecognitionView:1.2.2'
38 | }
39 | ```
40 |
41 |
42 | Usage
43 | -----
44 |
45 | * Xml file:
46 |
47 | Simply add view to your layout:
48 |
49 | ``` xml
50 |
55 | ```
56 | * Initialization:
57 |
58 | Init speech recognizer:
59 | ``` java
60 | SpeechRecognizer speechRecognizer = SpeechRecognizer.createSpeechRecognizer(context);
61 | ```
62 |
63 | Init RecognitionProgressView:
64 | ``` java
65 | RecognitionProgressView recognitionProgressView = (RecognitionProgressView) findViewById(R.id.recognition_view);
66 | recognitionProgressView.setSpeechRecognizer(speechRecognizer);
67 | recognitionProgressView.setRecognitionListener(new RecognitionListenerAdapter() {
68 | @Override
69 | public void onResults(Bundle results) {
70 | showResults(results);
71 | }
72 | });
73 | ```
74 |
75 | When SpeechRecognizer and RecognitionProgressView inited, use your speech recognizer as usual:
76 | ``` java
77 | listen.setOnClickListener(new View.OnClickListener() {
78 | @Override
79 | public void onClick(View v) {
80 | startRecognition();
81 | }
82 | });
83 |
84 | private void startRecognition() {
85 | Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
86 | intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
87 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
88 | speechRecognizer.startListening(intent);
89 | }
90 | ```
91 |
92 | Start and stop RecognitionProgressView animation:
93 | ``` java
94 | recognitionProgressView.play();
95 |
96 | recognitionProgressView.stop();
97 | ```
98 |
99 | * Customization:
100 |
101 | Set custom colors:
102 | ``` java
103 | int[] colors = {
104 | ContextCompat.getColor(this, R.color.color1),
105 | ContextCompat.getColor(this, R.color.color2),
106 | ContextCompat.getColor(this, R.color.color3),
107 | ContextCompat.getColor(this, R.color.color4),
108 | ContextCompat.getColor(this, R.color.color5)
109 | };
110 | recognitionProgressView.setColors(colors);
111 | ```
112 |
113 | Set custom bars heights:
114 | ``` java
115 | int[] heights = {60, 76, 58, 80, 55};
116 | recognitionProgressView.setBarMaxHeightsInDp(heights);
117 | ```
118 |
119 | Set custom circle radius/spacing between circles/idle animation amolitude size/rotation animation radius:
120 | ``` java
121 | recognitionProgressView.setCircleRadiusInDp(2);
122 | recognitionProgressView.setSpacingInDp(2);
123 | recognitionProgressView.setIdleStateAmplitudeInDp(2);
124 | recognitionProgressView.setRotationRadiusInDp(10);
125 | ```
126 | Don't forget to add permission to your AndroidManifest.xml file
127 | ``` xml
128 |
129 | ```
130 |
131 |
132 | * Warning
133 |
134 | From [Android Documentation](http://developer.android.com/reference/android/speech/RecognitionListener.html#onRmsChanged(float))
135 | For ```java public abstract void onRmsChanged (float rmsdB)``` callback ```There is no guarantee that this method will be called.```,
136 | so if this callback does not return values the Bars animation will be skipped.
137 |
138 | I found some hack to make it working every time you want to start speech recognition:
139 | ``` java
140 | listen.setOnClickListener(new View.OnClickListener() {
141 | @Override
142 | public void onClick(View v) {
143 | startRecognition();
144 | recognitionProgressView.postDelayed(new Runnable() {
145 | @Override
146 | public void run() {
147 | startRecognition();
148 | }
149 | }, 50);
150 | }
151 | });
152 | ```
153 |
154 |
155 | License
156 | -------
157 |
158 | Copyright 2016 Evgenii Zagumennyi
159 |
160 | Licensed under the Apache License, Version 2.0 (the "License");
161 | you may not use this file except in compliance with the License.
162 | You may obtain a copy of the License at
163 |
164 | http://www.apache.org/licenses/LICENSE-2.0
165 |
166 | Unless required by applicable law or agreed to in writing, software
167 | distributed under the License is distributed on an "AS IS" BASIS,
168 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
169 | See the License for the specific language governing permissions and
170 | limitations under the License.
171 |
172 |
173 | [1]: http://developer.android.com/intl/ru/reference/android/speech/SpeechRecognizer.html
174 |
--------------------------------------------------------------------------------
/art/speechrecognitionview.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zagum/SpeechRecognitionView/c45c3d5b84598337c378800c5407c93b62e7fe72/art/speechrecognitionview.gif
--------------------------------------------------------------------------------
/build.gradle:
--------------------------------------------------------------------------------
1 | buildscript {
2 | repositories {
3 | google()
4 | jcenter()
5 | }
6 | dependencies {
7 | classpath 'com.android.tools.build:gradle:3.1.4'
8 | }
9 | }
10 |
11 | allprojects {
12 | repositories {
13 | google()
14 | jcenter()
15 | maven { url "https://jitpack.io" }
16 | }
17 | }
18 |
19 | ext {
20 | minSdkVersion = 15
21 | compileSdkVersion = 27
22 | buildToolsVersion = '27.0.3'
23 |
24 | supportAppCompat = 'com.android.support:appcompat-v7:27.1.1'
25 | }
26 |
27 | task clean(type: Delete) {
28 | delete rootProject.buildDir
29 | }
--------------------------------------------------------------------------------
/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/zagum/SpeechRecognitionView/c45c3d5b84598337c378800c5407c93b62e7fe72/gradle/wrapper/gradle-wrapper.jar
--------------------------------------------------------------------------------
/gradle/wrapper/gradle-wrapper.properties:
--------------------------------------------------------------------------------
1 | #Thu Aug 30 11:14:58 MSK 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.4-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 ':speechrecognitionview'
2 | include ':speechrecognitionview-sample'
--------------------------------------------------------------------------------
/speechrecognitionview-sample/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.application'
2 |
3 | android {
4 | compileSdkVersion rootProject.ext.compileSdkVersion
5 | buildToolsVersion rootProject.ext.buildToolsVersion
6 |
7 | defaultConfig {
8 | applicationId "com.github.zagum.speechrecognitionview.sample"
9 | minSdkVersion rootProject.ext.minSdkVersion
10 | targetSdkVersion rootProject.ext.compileSdkVersion
11 | versionCode 1
12 | versionName "1.0"
13 | }
14 | }
15 |
16 | dependencies {
17 | implementation 'com.github.zagum:SpeechRecognitionView:1.2.2'
18 | }
--------------------------------------------------------------------------------
/speechrecognitionview-sample/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
4 |
5 |
6 |
7 |
13 |
14 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
--------------------------------------------------------------------------------
/speechrecognitionview-sample/src/main/java/com/github/zagum/speechrecognitionview/sample/MainActivity.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Evgenii Zagumennyi
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.zagum.speechrecognitionview.sample;
18 |
19 | import android.Manifest;
20 | import android.content.Intent;
21 | import android.content.pm.PackageManager;
22 | import android.os.Bundle;
23 | import android.speech.RecognizerIntent;
24 | import android.speech.SpeechRecognizer;
25 | import android.support.v4.app.ActivityCompat;
26 | import android.support.v4.content.ContextCompat;
27 | import android.support.v7.app.AppCompatActivity;
28 | import android.view.View;
29 | import android.widget.Button;
30 | import android.widget.Toast;
31 | import com.github.zagum.speechrecognitionview.RecognitionProgressView;
32 | import com.github.zagum.speechrecognitionview.adapters.RecognitionListenerAdapter;
33 | import java.util.ArrayList;
34 |
35 | public class MainActivity extends AppCompatActivity {
36 | private static final String TAG = "MainActivity";
37 | private static final int REQUEST_RECORD_AUDIO_PERMISSION_CODE = 1;
38 |
39 | private SpeechRecognizer speechRecognizer;
40 |
41 | @Override
42 | protected void onCreate(Bundle savedInstanceState) {
43 | super.onCreate(savedInstanceState);
44 | setContentView(R.layout.activity_main);
45 |
46 | int[] colors = {
47 | ContextCompat.getColor(this, R.color.color1),
48 | ContextCompat.getColor(this, R.color.color2),
49 | ContextCompat.getColor(this, R.color.color3),
50 | ContextCompat.getColor(this, R.color.color4),
51 | ContextCompat.getColor(this, R.color.color5)
52 | };
53 |
54 | int[] heights = { 20, 24, 18, 23, 16 };
55 |
56 | speechRecognizer = SpeechRecognizer.createSpeechRecognizer(this);
57 |
58 | final RecognitionProgressView recognitionProgressView = (RecognitionProgressView) findViewById(R.id.recognition_view);
59 | recognitionProgressView.setSpeechRecognizer(speechRecognizer);
60 | recognitionProgressView.setRecognitionListener(new RecognitionListenerAdapter() {
61 | @Override
62 | public void onResults(Bundle results) {
63 | showResults(results);
64 | }
65 | });
66 | recognitionProgressView.setColors(colors);
67 | recognitionProgressView.setBarMaxHeightsInDp(heights);
68 | recognitionProgressView.setCircleRadiusInDp(2);
69 | recognitionProgressView.setSpacingInDp(2);
70 | recognitionProgressView.setIdleStateAmplitudeInDp(2);
71 | recognitionProgressView.setRotationRadiusInDp(10);
72 | recognitionProgressView.play();
73 |
74 | Button listen = (Button) findViewById(R.id.listen);
75 | Button reset = (Button) findViewById(R.id.reset);
76 |
77 | listen.setOnClickListener(new View.OnClickListener() {
78 | @Override
79 | public void onClick(View v) {
80 | if (ContextCompat.checkSelfPermission(MainActivity.this,
81 | Manifest.permission.RECORD_AUDIO)
82 | != PackageManager.PERMISSION_GRANTED) {
83 | requestPermission();
84 | } else {
85 | startRecognition();
86 | recognitionProgressView.postDelayed(new Runnable() {
87 | @Override
88 | public void run() {
89 | startRecognition();
90 | }
91 | }, 50);
92 | }
93 | }
94 | });
95 |
96 | reset.setOnClickListener(new View.OnClickListener() {
97 | @Override
98 | public void onClick(View v) {
99 | recognitionProgressView.stop();
100 | recognitionProgressView.play();
101 | }
102 | });
103 | }
104 |
105 | @Override
106 | protected void onDestroy() {
107 | if (speechRecognizer != null) {
108 | speechRecognizer.destroy();
109 | }
110 | super.onDestroy();
111 | }
112 |
113 | private void startRecognition() {
114 | Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH);
115 | intent.putExtra(RecognizerIntent.EXTRA_CALLING_PACKAGE, getPackageName());
116 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE_MODEL, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM);
117 | intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, "en");
118 | speechRecognizer.startListening(intent);
119 | }
120 |
121 | private void showResults(Bundle results) {
122 | ArrayList matches = results
123 | .getStringArrayList(SpeechRecognizer.RESULTS_RECOGNITION);
124 | Toast.makeText(this, matches.get(0), Toast.LENGTH_LONG).show();
125 | }
126 |
127 | private void requestPermission() {
128 | if (ActivityCompat.shouldShowRequestPermissionRationale(this,
129 | Manifest.permission.RECORD_AUDIO)) {
130 | Toast.makeText(this, "Requires RECORD_AUDIO permission", Toast.LENGTH_SHORT).show();
131 | } else {
132 | ActivityCompat.requestPermissions(this,
133 | new String[] { Manifest.permission.RECORD_AUDIO },
134 | REQUEST_RECORD_AUDIO_PERMISSION_CODE);
135 | }
136 | }
137 | }
--------------------------------------------------------------------------------
/speechrecognitionview-sample/src/main/res/layout/activity_main.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
21 |
22 |
27 |
28 |
35 |
36 |
42 |
43 |
44 |
49 |
50 |
51 |
52 |
--------------------------------------------------------------------------------
/speechrecognitionview-sample/src/main/res/mipmap-hdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zagum/SpeechRecognitionView/c45c3d5b84598337c378800c5407c93b62e7fe72/speechrecognitionview-sample/src/main/res/mipmap-hdpi/ic_launcher.png
--------------------------------------------------------------------------------
/speechrecognitionview-sample/src/main/res/mipmap-mdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zagum/SpeechRecognitionView/c45c3d5b84598337c378800c5407c93b62e7fe72/speechrecognitionview-sample/src/main/res/mipmap-mdpi/ic_launcher.png
--------------------------------------------------------------------------------
/speechrecognitionview-sample/src/main/res/mipmap-xhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zagum/SpeechRecognitionView/c45c3d5b84598337c378800c5407c93b62e7fe72/speechrecognitionview-sample/src/main/res/mipmap-xhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/speechrecognitionview-sample/src/main/res/mipmap-xxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zagum/SpeechRecognitionView/c45c3d5b84598337c378800c5407c93b62e7fe72/speechrecognitionview-sample/src/main/res/mipmap-xxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/speechrecognitionview-sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/zagum/SpeechRecognitionView/c45c3d5b84598337c378800c5407c93b62e7fe72/speechrecognitionview-sample/src/main/res/mipmap-xxxhdpi/ic_launcher.png
--------------------------------------------------------------------------------
/speechrecognitionview-sample/src/main/res/values/colors.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 | #3F51B5
20 | #303F9F
21 | #FF4081
22 | #3164d7
23 | #d92d29
24 | #eeaa10
25 | #3164d7
26 | #2e9641
27 |
28 |
--------------------------------------------------------------------------------
/speechrecognitionview-sample/src/main/res/values/strings.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 | SpeechRecognitionView
20 |
21 |
--------------------------------------------------------------------------------
/speechrecognitionview-sample/src/main/res/values/styles.xml:
--------------------------------------------------------------------------------
1 |
16 |
17 |
18 |
19 |
24 |
25 |
--------------------------------------------------------------------------------
/speechrecognitionview/build.gradle:
--------------------------------------------------------------------------------
1 | apply plugin: 'com.android.library'
2 |
3 | android {
4 | compileSdkVersion rootProject.ext.compileSdkVersion
5 | buildToolsVersion rootProject.ext.buildToolsVersion
6 |
7 | defaultConfig {
8 | minSdkVersion rootProject.ext.minSdkVersion
9 | }
10 | }
11 |
12 | dependencies {
13 | compile rootProject.ext.supportAppCompat
14 | }
--------------------------------------------------------------------------------
/speechrecognitionview/src/main/AndroidManifest.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
--------------------------------------------------------------------------------
/speechrecognitionview/src/main/java/com/github/zagum/speechrecognitionview/RecognitionBar.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Evgenii Zagumennyi
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.zagum.speechrecognitionview;
18 |
19 | import android.graphics.RectF;
20 |
21 | public class RecognitionBar {
22 |
23 | private int x;
24 | private int y;
25 | private int radius;
26 | private int height;
27 |
28 | private final int maxHeight;
29 | private final int startX;
30 | private final int startY;
31 | final private RectF rect;
32 |
33 | public RecognitionBar(int x, int y, int height, int maxHeight, int radius) {
34 | this.x = x;
35 | this.y = y;
36 | this.radius = radius;
37 | this.startX = x;
38 | this.startY = y;
39 | this.height = height;
40 | this.maxHeight = maxHeight;
41 | this.rect = new RectF(x - radius,
42 | y - height / 2,
43 | x + radius,
44 | y + height / 2);
45 | }
46 |
47 | public void update() {
48 | rect.set(x - radius,
49 | y - height / 2,
50 | x + radius,
51 | y + height / 2);
52 | }
53 |
54 | public int getX() {
55 | return x;
56 | }
57 |
58 | public void setX(int x) {
59 | this.x = x;
60 | }
61 |
62 | public int getY() {
63 | return y;
64 | }
65 |
66 | public void setY(int y) {
67 | this.y = y;
68 | }
69 |
70 | public int getHeight() {
71 | return height;
72 | }
73 |
74 | public void setHeight(int height) {
75 | this.height = height;
76 | }
77 |
78 | public int getMaxHeight() {
79 | return maxHeight;
80 | }
81 |
82 | public int getStartX() {
83 | return startX;
84 | }
85 |
86 | public int getStartY() {
87 | return startY;
88 | }
89 |
90 | public RectF getRect() {
91 | return rect;
92 | }
93 |
94 | public int getRadius() {
95 | return radius;
96 | }
97 | }
--------------------------------------------------------------------------------
/speechrecognitionview/src/main/java/com/github/zagum/speechrecognitionview/RecognitionProgressView.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Evgenii Zagumennyi
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.zagum.speechrecognitionview;
18 |
19 | import android.content.Context;
20 | import android.graphics.Canvas;
21 | import android.graphics.Color;
22 | import android.graphics.Paint;
23 | import android.os.Bundle;
24 | import android.speech.RecognitionListener;
25 | import android.speech.SpeechRecognizer;
26 | import android.util.AttributeSet;
27 | import android.view.View;
28 | import com.github.zagum.speechrecognitionview.animators.BarParamsAnimator;
29 | import com.github.zagum.speechrecognitionview.animators.IdleAnimator;
30 | import com.github.zagum.speechrecognitionview.animators.RmsAnimator;
31 | import com.github.zagum.speechrecognitionview.animators.RotatingAnimator;
32 | import com.github.zagum.speechrecognitionview.animators.TransformAnimator;
33 | import java.util.ArrayList;
34 | import java.util.List;
35 |
36 | public class RecognitionProgressView extends View implements RecognitionListener {
37 |
38 | public static final int BARS_COUNT = 5;
39 |
40 | private static final int CIRCLE_RADIUS_DP = 5;
41 | private static final int CIRCLE_SPACING_DP = 11;
42 | private static final int ROTATION_RADIUS_DP = 25;
43 | private static final int IDLE_FLOATING_AMPLITUDE_DP = 3;
44 |
45 | private static final int[] DEFAULT_BARS_HEIGHT_DP = { 60, 46, 70, 54, 64 };
46 |
47 | private static final float MDPI_DENSITY = 1.5f;
48 |
49 | private final List recognitionBars = new ArrayList<>();
50 | private Paint paint;
51 | private BarParamsAnimator animator;
52 |
53 | private int radius;
54 | private int spacing;
55 | private int rotationRadius;
56 | private int amplitude;
57 |
58 | private float density;
59 |
60 | private boolean isSpeaking;
61 | private boolean animating;
62 |
63 | private SpeechRecognizer speechRecognizer;
64 | private RecognitionListener recognitionListener;
65 | private int barColor = -1;
66 | private int[] barColors;
67 | private int[] barMaxHeights;
68 |
69 | public RecognitionProgressView(Context context) {
70 | super(context);
71 | init();
72 | }
73 |
74 | public RecognitionProgressView(Context context, AttributeSet attrs) {
75 | super(context, attrs);
76 | init();
77 | }
78 |
79 | public RecognitionProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
80 | super(context, attrs, defStyleAttr);
81 | init();
82 | }
83 |
84 | /**
85 | * Set SpeechRecognizer which is view animated with
86 | */
87 | public void setSpeechRecognizer(SpeechRecognizer recognizer) {
88 | speechRecognizer = recognizer;
89 | speechRecognizer.setRecognitionListener(this);
90 | }
91 |
92 | /**
93 | * Set RecognitionListener to receive callbacks from {@link SpeechRecognizer}
94 | */
95 | public void setRecognitionListener(RecognitionListener listener) {
96 | recognitionListener = listener;
97 | }
98 |
99 | /**
100 | * Starts animating view
101 | */
102 | public void play() {
103 | startIdleInterpolation();
104 | animating = true;
105 | }
106 |
107 | /**
108 | * Stops animating view
109 | */
110 | public void stop() {
111 | if (animator != null) {
112 | animator.stop();
113 | animator = null;
114 | }
115 | animating = false;
116 | resetBars();
117 | }
118 |
119 | /**
120 | * Set one color to all bars in view
121 | */
122 | public void setSingleColor(int color) {
123 | barColor = color;
124 | }
125 |
126 | /**
127 | * Set different colors to bars in view
128 | *
129 | * @param colors - array with size = {@link #BARS_COUNT}
130 | */
131 | public void setColors(int[] colors) {
132 | if (colors == null) return;
133 |
134 | barColors = new int[BARS_COUNT];
135 | if (colors.length < BARS_COUNT) {
136 | System.arraycopy(colors, 0, barColors, 0, colors.length);
137 | for (int i = colors.length; i < BARS_COUNT; i++) {
138 | barColors[i] = colors[0];
139 | }
140 | } else {
141 | System.arraycopy(colors, 0, barColors, 0, BARS_COUNT);
142 | }
143 | }
144 |
145 | /**
146 | * Set sizes of bars in view
147 | *
148 | * @param heights - array with size = {@link #BARS_COUNT},
149 | * if not set uses default bars heights
150 | */
151 | public void setBarMaxHeightsInDp(int[] heights) {
152 | if (heights == null) return;
153 |
154 | barMaxHeights = new int[BARS_COUNT];
155 | if (heights.length < BARS_COUNT) {
156 | System.arraycopy(heights, 0, barMaxHeights, 0, heights.length);
157 | for (int i = heights.length; i < BARS_COUNT; i++) {
158 | barMaxHeights[i] = heights[0];
159 | }
160 | } else {
161 | System.arraycopy(heights, 0, barMaxHeights, 0, BARS_COUNT);
162 | }
163 | }
164 |
165 | /**
166 | * Set radius of circle
167 | *
168 | * @param radius - Default value = {@link #CIRCLE_RADIUS_DP}
169 | */
170 | public void setCircleRadiusInDp(int radius) {
171 | this.radius = (int) (radius * density);
172 | }
173 |
174 | /**
175 | * Set spacing between circles
176 | *
177 | * @param spacing - Default value = {@link #CIRCLE_SPACING_DP}
178 | */
179 | public void setSpacingInDp(int spacing) {
180 | this.spacing = (int) (spacing * density);
181 | }
182 |
183 | /**
184 | * Set idle animation amplitude
185 | *
186 | * @param amplitude - Default value = {@link #IDLE_FLOATING_AMPLITUDE_DP}
187 | */
188 | public void setIdleStateAmplitudeInDp(int amplitude) {
189 | this.amplitude = (int) (amplitude * density);
190 | }
191 |
192 | /**
193 | * Set rotation animation radius
194 | *
195 | * @param radius - Default value = {@link #ROTATION_RADIUS_DP}
196 | */
197 | public void setRotationRadiusInDp(int radius) {
198 | this.rotationRadius = (int) (radius * density);
199 | }
200 |
201 | private void init() {
202 | paint = new Paint();
203 | paint.setFlags(Paint.ANTI_ALIAS_FLAG);
204 | paint.setColor(Color.GRAY);
205 |
206 | density = getResources().getDisplayMetrics().density;
207 |
208 | radius = (int) (CIRCLE_RADIUS_DP * density);
209 | spacing = (int) (CIRCLE_SPACING_DP * density);
210 | rotationRadius = (int) (ROTATION_RADIUS_DP * density);
211 | amplitude = (int) (IDLE_FLOATING_AMPLITUDE_DP * density);
212 |
213 | if (density <= MDPI_DENSITY) {
214 | amplitude *= 2;
215 | }
216 | }
217 |
218 | @Override
219 | protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
220 | super.onLayout(changed, left, top, right, bottom);
221 | if (recognitionBars.isEmpty()) {
222 | initBars();
223 | } else if (changed) {
224 | recognitionBars.clear();
225 | initBars();
226 | }
227 | }
228 |
229 | @Override
230 | protected void onDraw(Canvas canvas) {
231 | super.onDraw(canvas);
232 |
233 | if (recognitionBars.isEmpty()) {
234 | return;
235 | }
236 |
237 | if (animating) {
238 | animator.animate();
239 | }
240 |
241 | for (int i = 0; i < recognitionBars.size(); i++) {
242 | RecognitionBar bar = recognitionBars.get(i);
243 | if (barColors != null) {
244 | paint.setColor(barColors[i]);
245 | } else if (barColor != -1) {
246 | paint.setColor(barColor);
247 | }
248 | canvas.drawRoundRect(bar.getRect(), radius, radius, paint);
249 | }
250 |
251 | if (animating) {
252 | invalidate();
253 | }
254 | }
255 |
256 | private void initBars() {
257 | final List heights = initBarHeights();
258 | int firstCirclePosition = getMeasuredWidth() / 2 -
259 | 2 * spacing -
260 | 4 * radius;
261 | for (int i = 0; i < BARS_COUNT; i++) {
262 | int x = firstCirclePosition + (2 * radius + spacing) * i;
263 | RecognitionBar bar = new RecognitionBar(x, getMeasuredHeight() / 2, 2 * radius, heights.get(i), radius);
264 | recognitionBars.add(bar);
265 | }
266 | }
267 |
268 | private List initBarHeights() {
269 | final List barHeights = new ArrayList<>();
270 | if (barMaxHeights == null) {
271 | for (int i = 0; i < BARS_COUNT; i++) {
272 | barHeights.add((int) (DEFAULT_BARS_HEIGHT_DP[i] * density));
273 | }
274 | } else {
275 | for (int i = 0; i < BARS_COUNT; i++) {
276 | barHeights.add((int) (barMaxHeights[i] * density));
277 | }
278 | }
279 | return barHeights;
280 | }
281 |
282 | private void resetBars() {
283 | for (RecognitionBar bar : recognitionBars) {
284 | bar.setX(bar.getStartX());
285 | bar.setY(bar.getStartY());
286 | bar.setHeight(radius * 2);
287 | bar.update();
288 | }
289 | }
290 |
291 | private void startIdleInterpolation() {
292 | animator = new IdleAnimator(recognitionBars, amplitude);
293 | animator.start();
294 | }
295 |
296 | private void startRmsInterpolation() {
297 | resetBars();
298 | animator = new RmsAnimator(recognitionBars);
299 | animator.start();
300 | }
301 |
302 | private void startTransformInterpolation() {
303 | resetBars();
304 | animator = new TransformAnimator(recognitionBars, getWidth() / 2, getHeight() / 2, rotationRadius);
305 | animator.start();
306 | ((TransformAnimator) animator).setOnInterpolationFinishedListener(new TransformAnimator.OnInterpolationFinishedListener() {
307 | @Override
308 | public void onFinished() {
309 | startRotateInterpolation();
310 | }
311 | });
312 | }
313 |
314 | private void startRotateInterpolation() {
315 | animator = new RotatingAnimator(recognitionBars, getWidth() / 2, getHeight() / 2);
316 | animator.start();
317 | }
318 |
319 | @Override
320 | public void onReadyForSpeech(Bundle params) {
321 | if (recognitionListener != null) {
322 | recognitionListener.onReadyForSpeech(params);
323 | }
324 | }
325 |
326 | @Override
327 | public void onBeginningOfSpeech() {
328 | if (recognitionListener != null) {
329 | recognitionListener.onBeginningOfSpeech();
330 | }
331 | isSpeaking = true;
332 | }
333 |
334 | @Override
335 | public void onRmsChanged(float rmsdB) {
336 | if (recognitionListener != null) {
337 | recognitionListener.onRmsChanged(rmsdB);
338 | }
339 | if (animator == null || rmsdB < 1f) {
340 | return;
341 | }
342 | if (!(animator instanceof RmsAnimator) && isSpeaking) {
343 | startRmsInterpolation();
344 | }
345 | if (animator instanceof RmsAnimator) {
346 | ((RmsAnimator) animator).onRmsChanged(rmsdB);
347 | }
348 | }
349 |
350 | @Override
351 | public void onBufferReceived(byte[] buffer) {
352 | if (recognitionListener != null) {
353 | recognitionListener.onBufferReceived(buffer);
354 | }
355 | }
356 |
357 | @Override
358 | public void onEndOfSpeech() {
359 | if (recognitionListener != null) {
360 | recognitionListener.onEndOfSpeech();
361 | }
362 | isSpeaking = false;
363 | startTransformInterpolation();
364 | }
365 |
366 | @Override
367 | public void onError(int error) {
368 | if (recognitionListener != null) {
369 | recognitionListener.onError(error);
370 | }
371 | }
372 |
373 | @Override
374 | public void onResults(Bundle results) {
375 | if (recognitionListener != null) {
376 | recognitionListener.onResults(results);
377 | }
378 | }
379 |
380 | @Override
381 | public void onPartialResults(Bundle partialResults) {
382 | if (recognitionListener != null) {
383 | recognitionListener.onPartialResults(partialResults);
384 | }
385 | }
386 |
387 | @Override
388 | public void onEvent(int eventType, Bundle params) {
389 | if (recognitionListener != null) {
390 | recognitionListener.onEvent(eventType, params);
391 | }
392 | }
393 | }
--------------------------------------------------------------------------------
/speechrecognitionview/src/main/java/com/github/zagum/speechrecognitionview/adapters/RecognitionListenerAdapter.java:
--------------------------------------------------------------------------------
1 | package com.github.zagum.speechrecognitionview.adapters;
2 |
3 | import android.os.Bundle;
4 | import android.speech.RecognitionListener;
5 |
6 | public abstract class RecognitionListenerAdapter implements RecognitionListener {
7 | @Override
8 | public void onReadyForSpeech(Bundle params) {
9 | }
10 |
11 | @Override
12 | public void onBeginningOfSpeech() {
13 | }
14 |
15 | @Override
16 | public void onRmsChanged(float rmsdB) {
17 | }
18 |
19 | @Override
20 | public void onBufferReceived(byte[] buffer) {
21 | }
22 |
23 | @Override
24 | public void onEndOfSpeech() {
25 | }
26 |
27 | @Override
28 | public void onError(int error) {
29 | }
30 |
31 | @Override
32 | public void onResults(Bundle results) {
33 | }
34 |
35 | @Override
36 | public void onPartialResults(Bundle partialResults) {
37 | }
38 |
39 | @Override
40 | public void onEvent(int eventType, Bundle params) {
41 | }
42 | }
43 |
--------------------------------------------------------------------------------
/speechrecognitionview/src/main/java/com/github/zagum/speechrecognitionview/animators/BarParamsAnimator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Evgenii Zagumennyi
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.zagum.speechrecognitionview.animators;
18 |
19 | public interface BarParamsAnimator {
20 | void start();
21 | void stop();
22 | void animate();
23 | }
--------------------------------------------------------------------------------
/speechrecognitionview/src/main/java/com/github/zagum/speechrecognitionview/animators/BarRmsAnimator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Evgenii Zagumennyi
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.zagum.speechrecognitionview.animators;
18 |
19 | import android.view.animation.AccelerateInterpolator;
20 | import android.view.animation.DecelerateInterpolator;
21 |
22 | import com.github.zagum.speechrecognitionview.RecognitionBar;
23 |
24 | import java.util.Random;
25 |
26 | public class BarRmsAnimator implements BarParamsAnimator {
27 |
28 | private static final float QUIT_RMSDB_MAX = 2f;
29 | private static final float MEDIUM_RMSDB_MAX = 5.5f;
30 |
31 | private static final long BAR_ANIMATION_UP_DURATION = 130;
32 | private static final long BAR_ANIMATION_DOWN_DURATION = 500;
33 | final private RecognitionBar bar;
34 | private float fromHeightPart;
35 | private float toHeightPart;
36 | private long startTimestamp;
37 | private boolean isPlaying;
38 | private boolean isUpAnimation;
39 |
40 | public BarRmsAnimator(RecognitionBar bar) {
41 | this.bar = bar;
42 | }
43 |
44 | @Override
45 | public void start() {
46 | isPlaying = true;
47 | }
48 |
49 | @Override
50 | public void stop() {
51 | isPlaying = false;
52 | }
53 |
54 | @Override
55 | public void animate() {
56 | if (isPlaying) {
57 | update();
58 | }
59 | }
60 |
61 | public void onRmsChanged(float rmsdB) {
62 | float newHeightPart;
63 |
64 | if (rmsdB < QUIT_RMSDB_MAX) {
65 | newHeightPart = 0.2f;
66 | } else if (rmsdB >= QUIT_RMSDB_MAX && rmsdB <= MEDIUM_RMSDB_MAX) {
67 | newHeightPart = 0.3f + new Random().nextFloat();
68 | if (newHeightPart > 0.6f) newHeightPart = 0.6f;
69 | } else {
70 | newHeightPart = 0.7f + new Random().nextFloat();
71 | if (newHeightPart > 1f) newHeightPart = 1f;
72 |
73 | }
74 |
75 | if (newHeightIsSmallerCurrent(newHeightPart)) {
76 | return;
77 | }
78 |
79 | fromHeightPart = (float) bar.getHeight() / bar.getMaxHeight();
80 | toHeightPart = newHeightPart;
81 |
82 | startTimestamp = System.currentTimeMillis();
83 | isUpAnimation = true;
84 | isPlaying = true;
85 | }
86 |
87 | private boolean newHeightIsSmallerCurrent(float newHeightPart) {
88 | return (float) bar.getHeight() / bar.getMaxHeight() > newHeightPart;
89 | }
90 |
91 | private void update() {
92 |
93 | long currTimestamp = System.currentTimeMillis();
94 | long delta = currTimestamp - startTimestamp;
95 |
96 | if (isUpAnimation) {
97 | animateUp(delta);
98 | } else {
99 | animateDown(delta);
100 | }
101 | }
102 |
103 | private void animateUp(long delta) {
104 | boolean finished = false;
105 | int minHeight = (int) (fromHeightPart * bar.getMaxHeight());
106 | int toHeight = (int) (bar.getMaxHeight() * toHeightPart);
107 |
108 | float timePart = (float) delta / BAR_ANIMATION_UP_DURATION;
109 |
110 | AccelerateInterpolator interpolator = new AccelerateInterpolator();
111 | int height = minHeight + (int) (interpolator.getInterpolation(timePart) * (toHeight - minHeight));
112 |
113 | if (height < bar.getHeight()) {
114 | return;
115 | }
116 |
117 | if (height >= toHeight) {
118 | height = toHeight;
119 | finished = true;
120 | }
121 |
122 | bar.setHeight(height);
123 | bar.update();
124 |
125 | if (finished) {
126 | isUpAnimation = false;
127 | startTimestamp = System.currentTimeMillis();
128 | }
129 | }
130 |
131 | private void animateDown(long delta) {
132 | int minHeight = bar.getRadius() * 2;
133 | int fromHeight = (int) (bar.getMaxHeight() * toHeightPart);
134 |
135 | float timePart = (float) delta / BAR_ANIMATION_DOWN_DURATION;
136 |
137 | DecelerateInterpolator interpolator = new DecelerateInterpolator();
138 | int height = minHeight + (int) ((1f - interpolator.getInterpolation(timePart)) * (fromHeight - minHeight));
139 |
140 | if (height > bar.getHeight()) {
141 | return;
142 | }
143 |
144 | if (height <= minHeight) {
145 | finish();
146 | return;
147 | }
148 |
149 | bar.setHeight(height);
150 | bar.update();
151 | }
152 |
153 | private void finish() {
154 | bar.setHeight(bar.getRadius() * 2);
155 | bar.update();
156 | isPlaying = false;
157 | }
158 | }
--------------------------------------------------------------------------------
/speechrecognitionview/src/main/java/com/github/zagum/speechrecognitionview/animators/IdleAnimator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Evgenii Zagumennyi
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.zagum.speechrecognitionview.animators;
18 |
19 | import com.github.zagum.speechrecognitionview.RecognitionBar;
20 |
21 | import java.util.List;
22 |
23 | public class IdleAnimator implements BarParamsAnimator {
24 |
25 | private static final long IDLE_DURATION = 1500;
26 |
27 | private long startTimestamp;
28 | private boolean isPlaying;
29 |
30 | private final int floatingAmplitude;
31 | private final List bars;
32 |
33 | public IdleAnimator(List bars, int floatingAmplitude) {
34 | this.floatingAmplitude = floatingAmplitude;
35 | this.bars = bars;
36 | }
37 |
38 | @Override
39 | public void start() {
40 | isPlaying = true;
41 | startTimestamp = System.currentTimeMillis();
42 | }
43 |
44 | @Override
45 | public void stop() {
46 | isPlaying = false;
47 | }
48 |
49 | @Override
50 | public void animate() {
51 | if (isPlaying) {
52 | update(bars);
53 | }
54 | }
55 |
56 | public void update(List bars) {
57 |
58 | long currTimestamp = System.currentTimeMillis();
59 | if (currTimestamp - startTimestamp > IDLE_DURATION) {
60 | startTimestamp += IDLE_DURATION;
61 | }
62 | long delta = currTimestamp - startTimestamp;
63 |
64 | int i = 0;
65 | for (RecognitionBar bar : bars) {
66 | updateCirclePosition(bar, delta, i);
67 | i++;
68 | }
69 | }
70 |
71 | private void updateCirclePosition(RecognitionBar bar, long delta, int num) {
72 | float angle = ((float) delta / IDLE_DURATION) * 360f + 120f * num;
73 | int y = (int) (Math.sin(Math.toRadians(angle)) * floatingAmplitude) + bar.getStartY();
74 | bar.setY(y);
75 | bar.update();
76 | }
77 | }
--------------------------------------------------------------------------------
/speechrecognitionview/src/main/java/com/github/zagum/speechrecognitionview/animators/RmsAnimator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Evgenii Zagumennyi
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.zagum.speechrecognitionview.animators;
18 |
19 | import com.github.zagum.speechrecognitionview.RecognitionBar;
20 |
21 | import java.util.ArrayList;
22 | import java.util.List;
23 |
24 | public class RmsAnimator implements BarParamsAnimator {
25 | final private List barAnimators;
26 |
27 |
28 | public RmsAnimator(List recognitionBars) {
29 | this.barAnimators = new ArrayList<>();
30 | for (RecognitionBar bar : recognitionBars) {
31 | barAnimators.add(new BarRmsAnimator(bar));
32 | }
33 | }
34 |
35 | @Override
36 | public void start() {
37 | for (BarRmsAnimator barAnimator : barAnimators) {
38 | barAnimator.start();
39 | }
40 | }
41 |
42 | @Override
43 | public void stop() {
44 | for (BarRmsAnimator barAnimator : barAnimators) {
45 | barAnimator.stop();
46 | }
47 | }
48 |
49 | @Override
50 | public void animate() {
51 | for (BarRmsAnimator barAnimator : barAnimators) {
52 | barAnimator.animate();
53 | }
54 | }
55 |
56 | public void onRmsChanged(float rmsDB) {
57 | for (BarRmsAnimator barAnimator : barAnimators) {
58 | barAnimator.onRmsChanged(rmsDB);
59 | }
60 | }
61 | }
--------------------------------------------------------------------------------
/speechrecognitionview/src/main/java/com/github/zagum/speechrecognitionview/animators/RotatingAnimator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Evgenii Zagumennyi
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.zagum.speechrecognitionview.animators;
18 |
19 | import android.graphics.Point;
20 | import android.view.animation.AccelerateDecelerateInterpolator;
21 |
22 | import com.github.zagum.speechrecognitionview.RecognitionBar;
23 |
24 | import java.util.ArrayList;
25 | import java.util.List;
26 |
27 | public class RotatingAnimator implements BarParamsAnimator {
28 |
29 | private static final long DURATION = 2000;
30 | private static final long ACCELERATE_ROTATION_DURATION = 1000;
31 | private static final long DECELERATE_ROTATION_DURATION = 1000;
32 | private static final float ROTATION_DEGREES = 720f;
33 | private static final float ACCELERATION_ROTATION_DEGREES = 40f;
34 |
35 | private long startTimestamp;
36 | private boolean isPlaying;
37 |
38 | private final int centerX, centerY;
39 | private final List startPositions;
40 | private final List bars;
41 |
42 | public RotatingAnimator(List bars, int centerX, int centerY) {
43 | this.centerX = centerX;
44 | this.centerY = centerY;
45 | this.bars = bars;
46 | this.startPositions = new ArrayList<>();
47 | for (RecognitionBar bar : bars) {
48 | startPositions.add(new Point(bar.getX(), bar.getY()));
49 | }
50 | }
51 |
52 | @Override
53 | public void start() {
54 | isPlaying = true;
55 | startTimestamp = System.currentTimeMillis();
56 | }
57 |
58 | @Override
59 | public void stop() {
60 | isPlaying = false;
61 | }
62 |
63 | @Override
64 | public void animate() {
65 | if (!isPlaying) return;
66 |
67 | long currTimestamp = System.currentTimeMillis();
68 | if (currTimestamp - startTimestamp > DURATION) {
69 | startTimestamp += DURATION;
70 | }
71 |
72 | long delta = currTimestamp - startTimestamp;
73 |
74 | float interpolatedTime = (float) delta / DURATION;
75 |
76 | float angle = interpolatedTime * ROTATION_DEGREES;
77 |
78 | int i = 0;
79 | for (RecognitionBar bar : bars) {
80 | float finalAngle = angle;
81 | if (i > 0 && delta > ACCELERATE_ROTATION_DURATION) {
82 | finalAngle += decelerate(delta, bars.size() - i);
83 | } else if (i > 0) {
84 | finalAngle += accelerate(delta, bars.size() - i);
85 | }
86 | rotate(bar, finalAngle, startPositions.get(i));
87 | i++;
88 | }
89 | }
90 |
91 | private float decelerate(long delta, int scale) {
92 | long accelerationDelta = delta - ACCELERATE_ROTATION_DURATION;
93 | AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator();
94 | float interpolatedTime = interpolator.getInterpolation((float) accelerationDelta / DECELERATE_ROTATION_DURATION);
95 | float decelerationAngle = -interpolatedTime * (ACCELERATION_ROTATION_DEGREES * scale);
96 | return ACCELERATION_ROTATION_DEGREES * scale + decelerationAngle;
97 | }
98 |
99 | private float accelerate(long delta, int scale) {
100 | long accelerationDelta = delta;
101 | AccelerateDecelerateInterpolator interpolator = new AccelerateDecelerateInterpolator();
102 | float interpolatedTime = interpolator.getInterpolation((float) accelerationDelta / ACCELERATE_ROTATION_DURATION);
103 | float accelerationAngle = interpolatedTime * (ACCELERATION_ROTATION_DEGREES * scale);
104 | return accelerationAngle;
105 | }
106 |
107 | /**
108 | * X = x0 + (x - x0) * cos(a) - (y - y0) * sin(a);
109 | * Y = y0 + (y - y0) * cos(a) + (x - x0) * sin(a);
110 | */
111 | private void rotate(RecognitionBar bar, double degrees, Point startPosition) {
112 |
113 | double angle = Math.toRadians(degrees);
114 |
115 | int x = centerX + (int) ((startPosition.x - centerX) * Math.cos(angle) -
116 | (startPosition.y - centerY) * Math.sin(angle));
117 |
118 | int y = centerY + (int) ((startPosition.x - centerX) * Math.sin(angle) +
119 | (startPosition.y - centerY) * Math.cos(angle));
120 |
121 | bar.setX(x);
122 | bar.setY(y);
123 | bar.update();
124 | }
125 | }
--------------------------------------------------------------------------------
/speechrecognitionview/src/main/java/com/github/zagum/speechrecognitionview/animators/TransformAnimator.java:
--------------------------------------------------------------------------------
1 | /*
2 | * Copyright (C) 2016 Evgenii Zagumennyi
3 | *
4 | * Licensed under the Apache License, Version 2.0 (the "License");
5 | * you may not use this file except in compliance with the License.
6 | * You may obtain a copy of the License at
7 | *
8 | * http://www.apache.org/licenses/LICENSE-2.0
9 | *
10 | * Unless required by applicable law or agreed to in writing, software
11 | * distributed under the License is distributed on an "AS IS" BASIS,
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 | * See the License for the specific language governing permissions and
14 | * limitations under the License.
15 | */
16 |
17 | package com.github.zagum.speechrecognitionview.animators;
18 |
19 | import android.graphics.Point;
20 |
21 | import com.github.zagum.speechrecognitionview.RecognitionBar;
22 | import com.github.zagum.speechrecognitionview.RecognitionProgressView;
23 |
24 | import java.util.ArrayList;
25 | import java.util.List;
26 |
27 | public class TransformAnimator implements BarParamsAnimator {
28 |
29 | private static final long DURATION = 300;
30 |
31 | private long startTimestamp;
32 | private boolean isPlaying;
33 |
34 |
35 | private OnInterpolationFinishedListener listener;
36 |
37 | private final int radius;
38 | private final int centerX, centerY;
39 | private final List finalPositions = new ArrayList<>();
40 | private final List bars;
41 |
42 | public TransformAnimator(List bars, int centerX, int centerY, int radius) {
43 | this.centerX = centerX;
44 | this.centerY = centerY;
45 | this.bars = bars;
46 | this.radius = radius;
47 | }
48 |
49 | @Override
50 | public void start() {
51 | isPlaying = true;
52 | startTimestamp = System.currentTimeMillis();
53 | initFinalPositions();
54 | }
55 |
56 | @Override
57 | public void stop() {
58 | isPlaying = false;
59 | if (listener != null) {
60 | listener.onFinished();
61 | }
62 | }
63 |
64 | @Override
65 | public void animate() {
66 | if (!isPlaying) return;
67 |
68 | long currTimestamp = System.currentTimeMillis();
69 | long delta = currTimestamp - startTimestamp;
70 | if (delta > DURATION) {
71 | delta = DURATION;
72 | }
73 |
74 | for (int i = 0; i < bars.size(); i++) {
75 | RecognitionBar bar = bars.get(i);
76 |
77 | int x = bar.getStartX() + (int) ((finalPositions.get(i).x - bar.getStartX()) * ((float) delta / DURATION));
78 | int y = bar.getStartY() + (int) ((finalPositions.get(i).y - bar.getStartY()) * ((float) delta / DURATION));
79 |
80 | bar.setX(x);
81 | bar.setY(y);
82 | bar.update();
83 | }
84 |
85 |
86 | if (delta == DURATION) {
87 | stop();
88 | }
89 | }
90 |
91 | private void initFinalPositions() {
92 | Point startPoint = new Point();
93 | startPoint.x = centerX;
94 | startPoint.y = centerY - radius;
95 | for (int i = 0; i < RecognitionProgressView.BARS_COUNT; i++) {
96 | Point point = new Point(startPoint);
97 | rotate((360d / RecognitionProgressView.BARS_COUNT) * i, point);
98 | finalPositions.add(point);
99 | }
100 | }
101 |
102 | /**
103 | * X = x0 + (x - x0) * cos(a) - (y - y0) * sin(a);
104 | * Y = y0 + (y - y0) * cos(a) + (x - x0) * sin(a);
105 | **/
106 | private void rotate(double degrees, Point point) {
107 |
108 | double angle = Math.toRadians(degrees);
109 |
110 | int x = centerX + (int) ((point.x - centerX) * Math.cos(angle) -
111 | (point.y - centerY) * Math.sin(angle));
112 |
113 | int y = centerY + (int) ((point.x - centerX) * Math.sin(angle) +
114 | (point.y - centerY) * Math.cos(angle));
115 |
116 | point.x = x;
117 | point.y = y;
118 | }
119 |
120 | public void setOnInterpolationFinishedListener(OnInterpolationFinishedListener listener) {
121 | this.listener = listener;
122 | }
123 |
124 | public interface OnInterpolationFinishedListener {
125 | void onFinished();
126 | }
127 | }
--------------------------------------------------------------------------------