Blame view

src/antistatic/spinnerwheel/AbstractWheelView.java 9.46 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
  /*
   * 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.animation.Animator;
  import android.animation.ObjectAnimator;
  import android.content.Context;
  import android.content.res.TypedArray;
  import android.graphics.Bitmap;
  import android.graphics.Canvas;
  import android.graphics.Paint;
  import android.graphics.PorterDuff;
  import android.graphics.PorterDuffXfermode;
  import android.graphics.drawable.Drawable;
  import android.util.AttributeSet;
  
  import com.ectrip.trips.check.R;
  
  
  /**
   * Abstract spinner spinnerwheel view.
   * This class should be subclassed.
   *
   * @author Yuri Kanivets
   * @author Dimitri Fedorov
   */
  public abstract class AbstractWheelView extends AbstractWheel {
  
      private static int itemID = -1;
  
      @SuppressWarnings("unused")
      private final String LOG_TAG = AbstractWheelView.class.getName() + " #" + (++itemID);
  
      //----------------------------------
      //  Default properties values
      //----------------------------------
  
      protected static final int DEF_ITEMS_DIMMED_ALPHA = 50; // 60 in ICS
  
      protected static final int DEF_SELECTION_DIVIDER_ACTIVE_ALPHA = 70;
  
      protected static final int DEF_SELECTION_DIVIDER_DIMMED_ALPHA = 70;
  
      protected static final int DEF_ITEM_OFFSET_PERCENT = 10;
  
      protected static final int DEF_ITEM_PADDING = 10;
      
      protected static final int DEF_SELECTION_DIVIDER_SIZE = 2;
  
      //----------------------------------
      //  Class properties
      //----------------------------------
  
      // configurable properties
  
      /** The alpha of the selector spinnerwheel when it is dimmed. */
      protected int mItemsDimmedAlpha;
  
      /** The alpha of separators spinnerwheel when they are shown. */
      protected int mSelectionDividerActiveAlpha;
  
      /** The alpha of separators when they are is dimmed. */
      protected int mSelectionDividerDimmedAlpha;
  
      /** Top and bottom items offset */
      protected int mItemOffsetPercent;
  
      /** Left and right padding value */
      protected int mItemsPadding;
  
      /** Divider for showing item to be selected while scrolling */
      protected Drawable mSelectionDivider;
  
      // the rest
  
      /**
       * The {@link android.graphics.Paint} for drawing the selector.
       */
      protected Paint mSelectorWheelPaint;
  
      /**
       * The {@link android.graphics.Paint} for drawing the separators.
       */
      protected Paint mSeparatorsPaint;
  
      /**
       * {@link com.nineoldandroids.animation.Animator} for dimming the selector spinnerwheel.
       */
      protected Animator mDimSelectorWheelAnimator;
  
      /**
       * {@link com.nineoldandroids.animation.Animator} for dimming the selector spinnerwheel.
       */
      protected Animator mDimSeparatorsAnimator;
  
      /**
       * The property for setting the selector paint.
       */
      protected static final String PROPERTY_SELECTOR_PAINT_COEFF = "selectorPaintCoeff";
  
      /**
       * The property for setting the separators paint.
       */
      protected static final String PROPERTY_SEPARATORS_PAINT_ALPHA = "separatorsPaintAlpha";
  
  
      protected Bitmap mSpinBitmap;
      protected Bitmap mSeparatorsBitmap;
  
  
      //--------------------------------------------------------------------------
      //
      //  Constructor
      //
      //--------------------------------------------------------------------------
  
      public AbstractWheelView(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
      }
  
      //--------------------------------------------------------------------------
      //
      //  Initiating assets and setters for paints
      //
      //--------------------------------------------------------------------------
  
      @Override
      protected void initAttributes(AttributeSet attrs, int defStyle) {
          super.initAttributes(attrs, defStyle);
          
          TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.AbstractWheelView, defStyle, 0);
          mItemsDimmedAlpha = a.getInt(R.styleable.AbstractWheelView_itemsDimmedAlpha, DEF_ITEMS_DIMMED_ALPHA);
          mSelectionDividerActiveAlpha = a.getInt(R.styleable.AbstractWheelView_selectionDividerActiveAlpha, DEF_SELECTION_DIVIDER_ACTIVE_ALPHA);
          mSelectionDividerDimmedAlpha = a.getInt(R.styleable.AbstractWheelView_selectionDividerDimmedAlpha, DEF_SELECTION_DIVIDER_DIMMED_ALPHA);
          mItemOffsetPercent = a.getInt(R.styleable.AbstractWheelView_itemOffsetPercent, DEF_ITEM_OFFSET_PERCENT);
          mItemsPadding = a.getDimensionPixelSize(R.styleable.AbstractWheelView_itemsPadding, DEF_ITEM_PADDING);
          mSelectionDivider = a.getDrawable(R.styleable.AbstractWheelView_selectionDivider);
          a.recycle();
      }
  
      @Override
      protected void initData(Context context) {
          super.initData(context);
  
          // creating animators
          mDimSelectorWheelAnimator = ObjectAnimator.ofFloat(this, PROPERTY_SELECTOR_PAINT_COEFF, 1, 0);
  
          mDimSeparatorsAnimator = ObjectAnimator.ofInt(this, PROPERTY_SEPARATORS_PAINT_ALPHA,
                  mSelectionDividerActiveAlpha, mSelectionDividerDimmedAlpha
          );
  
          // creating paints
          mSeparatorsPaint = new Paint();
          mSeparatorsPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
          mSeparatorsPaint.setAlpha(mSelectionDividerDimmedAlpha);
  
          mSelectorWheelPaint = new Paint();
          mSelectorWheelPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
      }
  
      /**
       * Recreates assets (like bitmaps) when layout size has been changed
       *
       * @param width New spinnerwheel width
       * @param height New spinnerwheel height
       */
      @Override
      protected void recreateAssets(int width, int height) {
          mSpinBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
          mSeparatorsBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
          setSelectorPaintCoeff(0);
      }
  
      /**
       * Sets the <code>alpha</code> of the {@link Paint} for drawing separators
       * spinnerwheel.
       * @param alpha alpha value from 0 to 255
       */
      @SuppressWarnings("unused")  // Called via reflection
      public void setSeparatorsPaintAlpha(int alpha) {
          mSeparatorsPaint.setAlpha(alpha);
          invalidate();
      }
  
      /**
       * Sets the <code>coeff</code> of the {@link Paint} for drawing
       * the selector spinnerwheel.
       *
       * @param coeff Coefficient from 0 (selector is passive) to 1 (selector is active)
       */
      abstract public void setSelectorPaintCoeff(float coeff);
  
  
      //--------------------------------------------------------------------------
      //
      //  Processing scroller events
      //
      //--------------------------------------------------------------------------
  
      @Override
      protected void onScrollTouched() {
          mDimSelectorWheelAnimator.cancel();
          mDimSeparatorsAnimator.cancel();
          setSelectorPaintCoeff(1);
          setSeparatorsPaintAlpha(mSelectionDividerActiveAlpha);
      }
  
      @Override
      protected void onScrollTouchedUp() {
          super.onScrollTouchedUp();
          fadeSelectorWheel(750);
          lightSeparators(750);
      }
  
      @Override
      protected void onScrollFinished() {
          fadeSelectorWheel(500);
          lightSeparators(500);
      }
  
      //----------------------------------
      //  Animating components
      //----------------------------------
  
      /**
       * Fade the selector spinnerwheel via an animation.
       *
       * @param animationDuration The duration of the animation.
       */
      private void fadeSelectorWheel(long animationDuration) {
          mDimSelectorWheelAnimator.setDuration(animationDuration);
          mDimSelectorWheelAnimator.start();
      }
  
      /**
       * Fade the selector spinnerwheel via an animation.
       *
       * @param animationDuration The duration of the animation.
       */
      private void lightSeparators(long animationDuration) {
          mDimSeparatorsAnimator.setDuration(animationDuration);
          mDimSeparatorsAnimator.start();
      }
  
  
      //--------------------------------------------------------------------------
      //
      //  Layout measuring
      //
      //--------------------------------------------------------------------------
  
      /**
       * Perform layout measurements
       */
      abstract protected void measureLayout();
  
  
      //--------------------------------------------------------------------------
      //
      //  Drawing stuff
      //
      //--------------------------------------------------------------------------
  
      @Override
      protected void onDraw(Canvas canvas) {
          super.onDraw(canvas);
  
          if (mViewAdapter != null && mViewAdapter.getItemsCount() > 0) {
              if (rebuildItems()) {
                  measureLayout();
              }
              doItemsLayout();
              drawItems(canvas);
          }
      }
  
      /**
       * Draws items on specified canvas
       *
       * @param canvas the canvas for drawing
       */
      abstract protected void drawItems(Canvas canvas);
  }