@@ -59,6 +59,8 @@ public void onReleased(VelocityTracker element) {
5959 final float mPastY [] = new float [NUM_PAST ];
6060 final long mPastTime [] = new long [NUM_PAST ];
6161
62+ int mLastTouch ;
63+
6264 float mYVelocity ;
6365 float mXVelocity ;
6466
@@ -105,7 +107,10 @@ private VelocityTracker() {
105107 * Reset the velocity tracker back to its initial state.
106108 */
107109 public void clear () {
108- mPastTime [0 ] = 0 ;
110+ final long [] pastTime = mPastTime ;
111+ for (int i = 0 ; i < NUM_PAST ; i ++) {
112+ pastTime [i ] = 0 ;
113+ }
109114 }
110115
111116 /**
@@ -128,42 +133,11 @@ public void addMovement(MotionEvent ev) {
128133 }
129134
130135 private void addPoint (float x , float y , long time ) {
131- int drop = -1 ;
132- int i ;
133- if (localLOGV ) Log .v (TAG , "Adding past y=" + y + " time=" + time );
134- final long [] pastTime = mPastTime ;
135- for (i =0 ; i <NUM_PAST ; i ++) {
136- if (pastTime [i ] == 0 ) {
137- break ;
138- } else if (pastTime [i ] < time -LONGEST_PAST_TIME ) {
139- if (localLOGV ) Log .v (TAG , "Dropping past too old at "
140- + i + " time=" + pastTime [i ]);
141- drop = i ;
142- }
143- }
144- if (localLOGV ) Log .v (TAG , "Add index: " + i );
145- if (i == NUM_PAST && drop < 0 ) {
146- drop = 0 ;
147- }
148- if (drop == i ) drop --;
149- final float [] pastX = mPastX ;
150- final float [] pastY = mPastY ;
151- if (drop >= 0 ) {
152- if (localLOGV ) Log .v (TAG , "Dropping up to #" + drop );
153- final int start = drop +1 ;
154- final int count = NUM_PAST -drop -1 ;
155- System .arraycopy (pastX , start , pastX , 0 , count );
156- System .arraycopy (pastY , start , pastY , 0 , count );
157- System .arraycopy (pastTime , start , pastTime , 0 , count );
158- i -= (drop +1 );
159- }
160- pastX [i ] = x ;
161- pastY [i ] = y ;
162- pastTime [i ] = time ;
163- i ++;
164- if (i < NUM_PAST ) {
165- pastTime [i ] = 0 ;
166- }
136+ final int lastTouch = (mLastTouch + 1 ) % NUM_PAST ;
137+ mPastX [lastTouch ] = x ;
138+ mPastY [lastTouch ] = y ;
139+ mPastTime [lastTouch ] = time ;
140+ mLastTouch = lastTouch ;
167141 }
168142
169143 /**
@@ -193,35 +167,39 @@ public void computeCurrentVelocity(int units, float maxVelocity) {
193167 final float [] pastX = mPastX ;
194168 final float [] pastY = mPastY ;
195169 final long [] pastTime = mPastTime ;
170+ final int lastTouch = mLastTouch ;
171+
172+ // find oldest acceptable time
173+ int oldestTouch = lastTouch ;
174+ if (pastTime [lastTouch ] > 0 ) { // cleared ?
175+ oldestTouch = (lastTouch + 1 ) % NUM_PAST ;
176+ final float acceptableTime = pastTime [lastTouch ] - LONGEST_PAST_TIME ;
177+ while (pastTime [oldestTouch ] < acceptableTime ) {
178+ oldestTouch = (oldestTouch + 1 ) % NUM_PAST ;
179+ }
180+ }
196181
197182 // Kind-of stupid.
198- final float oldestX = pastX [0 ];
199- final float oldestY = pastY [0 ];
200- final long oldestTime = pastTime [0 ];
183+ final float oldestX = pastX [oldestTouch ];
184+ final float oldestY = pastY [oldestTouch ];
185+ final long oldestTime = pastTime [oldestTouch ];
201186 float accumX = 0 ;
202187 float accumY = 0 ;
203- int N =0 ;
204- while (N < NUM_PAST ) {
205- if (pastTime [N ] == 0 ) {
206- break ;
207- }
208- N ++;
209- }
188+ float N = (lastTouch - oldestTouch + NUM_PAST ) % NUM_PAST + 1 ;
210189 // Skip the last received event, since it is probably pretty noisy.
211190 if (N > 3 ) N --;
212-
191+
213192 for (int i =1 ; i < N ; i ++) {
214- final int dur = (int )(pastTime [i ] - oldestTime );
193+ final int j = (oldestTouch + i ) % NUM_PAST ;
194+ final int dur = (int )(pastTime [j ] - oldestTime );
215195 if (dur == 0 ) continue ;
216- float dist = pastX [i ] - oldestX ;
196+ float dist = pastX [j ] - oldestX ;
217197 float vel = (dist /dur ) * units ; // pixels/frame.
218- if (accumX == 0 ) accumX = vel ;
219- else accumX = (accumX + vel ) * .5f ;
198+ accumX = (accumX == 0 ) ? vel : (accumX + vel ) * .5f ;
220199
221- dist = pastY [i ] - oldestY ;
200+ dist = pastY [j ] - oldestY ;
222201 vel = (dist /dur ) * units ; // pixels/frame.
223- if (accumY == 0 ) accumY = vel ;
224- else accumY = (accumY + vel ) * .5f ;
202+ accumY = (accumY == 0 ) ? vel : (accumY + vel ) * .5f ;
225203 }
226204 mXVelocity = accumX < 0.0f ? Math .max (accumX , -maxVelocity ) : Math .min (accumX , maxVelocity );
227205 mYVelocity = accumY < 0.0f ? Math .max (accumY , -maxVelocity ) : Math .min (accumY , maxVelocity );
0 commit comments