3
3
// WARNING!!! This file has more magic numbers in it than you could shake a
4
4
// stick at
5
5
6
- import java .util .Random ;
7
-
8
6
import android .content .Context ;
9
7
import android .graphics .Bitmap ;
10
8
import android .graphics .Bitmap .Config ;
@@ -58,14 +56,6 @@ public VisualizerView(Context context)
58
56
private void init () {
59
57
mBytes = null ;
60
58
61
- mLinePaint .setStrokeWidth (1f );
62
- mLinePaint .setAntiAlias (true );
63
- mLinePaint .setColor (Color .argb (88 , 0 , 128 , 255 ));
64
-
65
- mSpecialLinePaint .setStrokeWidth (5f );
66
- mSpecialLinePaint .setAntiAlias (true );
67
- mSpecialLinePaint .setColor (Color .argb (188 , 255 , 255 , 255 ));
68
-
69
59
mProgressLinePaint .setStrokeWidth (4f );
70
60
mProgressLinePaint .setAntiAlias (true );
71
61
mProgressLinePaint .setColor (Color .argb (255 , 22 , 131 , 255 ));
@@ -79,7 +69,6 @@ private void init() {
79
69
80
70
public void updateVisualizer (byte [] bytes ) {
81
71
mBytes = bytes ;
82
- rotateColours ();
83
72
invalidate ();
84
73
}
85
74
@@ -100,25 +89,13 @@ public void updateVisualizerFFT(byte[] bytes) {
100
89
invalidate ();
101
90
}
102
91
103
-
104
- float colorCounter = 0 ;
105
- private void rotateColours ()
106
- {
107
- int r = (int )Math .floor (128 *(Math .sin (colorCounter ) + 1 ));
108
- int g = (int )Math .floor (128 *(Math .sin (colorCounter + 2 ) + 1 ));
109
- int b = (int )Math .floor (128 *(Math .sin (colorCounter + 4 ) + 1 ));
110
- mLinePaint .setColor (Color .argb (128 , r , g , b ));
111
- colorCounter += 0.03 ;
112
- }
113
-
114
92
Bitmap mCanvasBitmap ;
115
93
Canvas mCanvas ;
116
- Random mRandom = new Random ();
117
- float amplitude = 0 ;
118
94
119
95
BarGraphRenderer mBarGraphRendererTop ;
120
96
BarGraphRenderer mBarGraphRendererBottom ;
121
97
CircleRenderer mCircleRenderer ;
98
+ LineRenderer mLineRenderer ;
122
99
123
100
@ Override
124
101
protected void onDraw (Canvas canvas ) {
@@ -158,42 +135,26 @@ protected void onDraw(Canvas canvas) {
158
135
paint3 .setStrokeWidth (3f );
159
136
paint3 .setAntiAlias (true );
160
137
paint3 .setColor (Color .argb (255 , 222 , 92 , 143 ));
161
-
162
138
mCircleRenderer = new CircleRenderer (mCanvas , paint3 , true );
163
- }
164
139
165
- // Draw normal line - offset by amplitude
166
- for (int i = 0 ; i < mBytes .length - 1 ; i ++) {
167
- mPoints [i * 4 ] = mRect .width () * i / (mBytes .length - 1 );
168
- mPoints [i * 4 + 1 ] = mRect .height () / 2
169
- + ((byte ) (mBytes [i ] + 128 )) * (mRect .height () / 3 ) / 128 ;
170
- mPoints [i * 4 + 2 ] = mRect .width () * (i + 1 ) / (mBytes .length - 1 );
171
- mPoints [i * 4 + 3 ] = mRect .height () / 2
172
- + ((byte ) (mBytes [i + 1 ] + 128 )) * (mRect .height () / 3 ) / 128 ;
173
- }
140
+ Paint linePaint = new Paint ();
141
+ linePaint .setStrokeWidth (1f );
142
+ linePaint .setAntiAlias (true );
143
+ linePaint .setColor (Color .argb (88 , 0 , 128 , 255 ));
174
144
175
- // Calc amplitude for this waveform
176
- float accumulator = 0 ;
177
- for (int i = 0 ; i < mBytes .length - 1 ; i ++) {
178
- accumulator += Math .abs (mBytes [i ]);
145
+ Paint lineFlashPaint = new Paint ();
146
+ lineFlashPaint .setStrokeWidth (5f );
147
+ lineFlashPaint .setAntiAlias (true );
148
+ lineFlashPaint .setColor (Color .argb (188 , 255 , 255 , 255 ));
149
+ mLineRenderer = new LineRenderer (mCanvas , linePaint , lineFlashPaint , true );
179
150
}
180
151
181
- float amp = accumulator /(128 * mBytes .length );
182
- if (amp > amplitude )
183
- {
184
- amplitude = amp ;
185
- // Occassionally, make a prominent line
186
- mCanvas .drawLines (mPoints , mSpecialLinePaint );
187
- }
188
- else
189
- {
190
- amplitude *= 0.99 ;
191
- mCanvas .drawLines (mPoints , mLinePaint );
192
- }
152
+
193
153
194
154
195
155
AudioData audioData = new AudioData (mBytes );
196
156
mCircleRenderer .render (audioData , mRect );
157
+ mLineRenderer .render (audioData , mRect );
197
158
198
159
// FFT time!!!!
199
160
if (mFFTBytes == null ) {
@@ -205,11 +166,6 @@ protected void onDraw(Canvas canvas) {
205
166
mBarGraphRendererTop .render (fftData , mRect );
206
167
mBarGraphRendererBottom .render (fftData , mRect );
207
168
208
- // We totally need a thing moving along the bottom
209
- float cX = mRect .width ()*(SystemClock .currentThreadTimeMillis () - mFlashTime )/mFlashPeriod ;
210
-
211
- mCanvas .drawLine (cX - 35 , mRect .height (), cX , mRect .height (), mProgressLinePaint );
212
-
213
169
// Fade out old contents
214
170
mCanvas .drawPaint (mFadePaint );
215
171
0 commit comments