Blame view

src/antistatic/spinnerwheel/WheelScroller.java 8.05 KB
3c2353cd   杜方   1、畅游通核销app源码提交;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
  /*
   * android-spinnerwheel
   * https://github.com/ai212983/android-spinnerwheel
   *
   * based on
   *
   * Android Wheel Control.
   * https://code.google.com/p/android-wheel/
   *
   * Copyright 2011 Yuri Kanivets
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   * http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   * See the License for the specific language governing permissions and
   * limitations under the License.
   */
  
  package antistatic.spinnerwheel;
  
  import android.content.Context;
  import android.os.Handler;
  import android.os.Message;
  import android.view.GestureDetector;
  import android.view.MotionEvent;
  import android.view.GestureDetector.SimpleOnGestureListener;
  import android.view.animation.Interpolator;
  import android.widget.Scroller;
  
  /**
   * Scroller class handles scrolling events and updates the spinnerwheel
   */
  public abstract class WheelScroller {
      /**
       * Scrolling listener interface
       */
      public interface ScrollingListener {
          /**
           * Scrolling callback called when scrolling is performed.
           * @param distance the distance to scroll
           */
          void onScroll(int distance);
  
          /**
           * This callback is invoked when scroller has been touched
           */
          void onTouch();
  
          /**
           * This callback is invoked when touch is up
           */
          void onTouchUp();
  
          /**
           * Starting callback called when scrolling is started
           */
          void onStarted();
          
          /**
           * Finishing callback called after justifying
           */
          void onFinished();
          
          /**
           * Justifying callback called to justify a view when scrolling is ended
           */
          void onJustify();
      }
      
      /** Scrolling duration */
      private static final int SCROLLING_DURATION = 400;
  
      /** Minimum delta for scrolling */
      public static final int MIN_DELTA_FOR_SCROLLING = 1;
  
      // Listener
      private ScrollingListener listener;
      
      // Context
      private Context context;
      
      // Scrolling
      private GestureDetector gestureDetector;
      protected Scroller scroller;
      private int lastScrollPosition;
      private float lastTouchedPosition;
      private boolean isScrollingPerformed;
  
      /**
       * Constructor
       * @param context the current context
       * @param listener the scrolling listener
       */
      public WheelScroller(Context context, ScrollingListener listener) {
          gestureDetector = new GestureDetector(context, new SimpleOnGestureListener() {
              public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
                  // Do scrolling in onTouchEvent() since onScroll() are not call immediately
                  //  when user touch and move the spinnerwheel
                  return true;
              }
  
              public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                  lastScrollPosition = 0;
                  scrollerFling(lastScrollPosition, (int) velocityX, (int) velocityY);
                  setNextMessage(MESSAGE_SCROLL);
                  return true;
              }
  
              // public boolean onDown(MotionEvent motionEvent);
  
          });
          gestureDetector.setIsLongpressEnabled(false);
          
          scroller = new Scroller(context);
  
          this.listener = listener;
          this.context = context;
      }
      
      /**
       * Set the the specified scrolling interpolator
       * @param interpolator the interpolator
       */
      public void setInterpolator(Interpolator interpolator) {
          scroller.forceFinished(true);
          scroller = new Scroller(context, interpolator);
      }
      
      /**
       * Scroll the spinnerwheel
       * @param distance the scrolling distance
       * @param time the scrolling duration
       */
      public void scroll(int distance, int time) {
          scroller.forceFinished(true);
          lastScrollPosition = 0;
          scrollerStartScroll(distance, time != 0 ? time : SCROLLING_DURATION);
          setNextMessage(MESSAGE_SCROLL);
          startScrolling();
      }
     
      /**
       * Stops scrolling
       */
      public void stopScrolling() {
          scroller.forceFinished(true);
      }
      
      /**
       * Handles Touch event 
       * @param event the motion event
       * @return
       */
      public boolean onTouchEvent(MotionEvent event) {
          switch (event.getAction()) {
  
              case MotionEvent.ACTION_DOWN:
                  lastTouchedPosition = getMotionEventPosition(event);
                  scroller.forceFinished(true);
                  clearMessages();
                  listener.onTouch();
                  break;
  
              case MotionEvent.ACTION_UP:
                  if (scroller.isFinished())
                      listener.onTouchUp();
                  break;
  
  
              case MotionEvent.ACTION_MOVE:
                  // perform scrolling
                  int distance = (int)(getMotionEventPosition(event) - lastTouchedPosition);
                  if (distance != 0) {
                      startScrolling();
                      listener.onScroll(distance);
                      lastTouchedPosition = getMotionEventPosition(event);
                  }
                  break;
          }
  
          if (!gestureDetector.onTouchEvent(event) && event.getAction() == MotionEvent.ACTION_UP) {
              justify();
          }
  
          return true;
      }
  
  
      // Messages
      private final int MESSAGE_SCROLL = 0;
      private final int MESSAGE_JUSTIFY = 1;
      
      /**
       * Set next message to queue. Clears queue before.
       * 
       * @param message the message to set
       */
      private void setNextMessage(int message) {
          clearMessages();
          animationHandler.sendEmptyMessage(message);
      }
  
      /**
       * Clears messages from queue
       */
      private void clearMessages() {
          animationHandler.removeMessages(MESSAGE_SCROLL);
          animationHandler.removeMessages(MESSAGE_JUSTIFY);
      }
      
      // animation handler
      private Handler animationHandler = new Handler() {
          public void handleMessage(Message msg) {
              scroller.computeScrollOffset();
              int currPosition = getCurrentScrollerPosition();
              int delta = lastScrollPosition - currPosition;
              lastScrollPosition = currPosition;
              if (delta != 0) {
                  listener.onScroll(delta);
              }
              
              // scrolling is not finished when it comes to final Y
              // so, finish it manually 
              if (Math.abs(currPosition - getFinalScrollerPosition()) < MIN_DELTA_FOR_SCROLLING) {
                  // currPosition = getFinalScrollerPosition();
                  scroller.forceFinished(true);
              }
              if (!scroller.isFinished()) {
                  animationHandler.sendEmptyMessage(msg.what);
              } else if (msg.what == MESSAGE_SCROLL) {
                  justify();
              } else {
                  finishScrolling();
              }
          }
      };
      
      /**
       * Justifies spinnerwheel
       */
      private void justify() {
          listener.onJustify();
          setNextMessage(MESSAGE_JUSTIFY);
      }
  
      /**
       * Starts scrolling
       */
      private void startScrolling() {
          if (!isScrollingPerformed) {
              isScrollingPerformed = true;
              listener.onStarted();
          }
      }
  
      /**
       * Finishes scrolling
       */
      protected void finishScrolling() {
          if (isScrollingPerformed) {
              listener.onFinished();
              isScrollingPerformed = false;
          }
      }
  
      protected abstract int getCurrentScrollerPosition();
  
      protected abstract int getFinalScrollerPosition();
  
      protected abstract float getMotionEventPosition(MotionEvent event);
  
      protected abstract void scrollerStartScroll(int distance, int time);
  
      protected abstract void scrollerFling(int position, int velocityX, int velocityY);
  }