在安卓中左右侧滑菜单的使用用的比ios多得多,可能是谷歌带的头吧,几乎所有的谷歌应用都有侧滑菜单。谷歌没有开放这个源码,在一个成熟的开源代码出现之前,大家都是各自为战,偶尔能看到一个勉强实现了的。MenuDrawer和其他的侧滑代码不同,他是一个性能高效且成熟的库。

在menuDraer出现之前我还用过slidemenu,效果差不多,但感觉没有MenuDrawer流畅,后来看了MenuDrawer和slidemenu的源码之后,才知道其实他们的实现思路是基本一致的,不过MenuDrawer的代码写的更好些。

MenuDrawer支持左、右、上、下四种侧滑方式,同时支持Overlay和Sliding两种模式。如下图:

但是MenuDrawer在github上的项目是gradle的android studio做的,直接将library导入到项目中会出现资源文件找不到的错误。因此要学会在eclipse中使用的话需要点耐心。官方已经有了其使用的方法,我这里就暂时不多说。其实要学会用这个库很简单,但是我的目的是要学会他实现滑动的基本方法,学会修改这个库,运用在更多的场景中。

我想弄明白下面的疑问:

在一个界面中如何自如的通过手势控制布局,想隐藏多少隐藏多少。

MenuDrawer的代码很多,但是其实我并不需要里面的很多功能,比如和actionbar的关联,甚至是上下滑动我也不需要,我只需要一个实现左滑菜单的范例,然后其他的我自己来,我不想一个项目下来用了很多别人的库,包含着一大堆跟业务无关的代码,而且我还不知道是拿来干嘛的。

经过研究,发现只需反复研究三个几个文件就能知道MenuDrawer的实现过程

这三个类以及他们的的继承关系如下:

MenuDrawer.java -> DraggableDrawer.java -> SlidingDrawer.java

MenuDrawer.java 这个是基本的类,继承自ViewGroup,他定义了许多公共方法和属性变量,MenuDrawer初始化也是在这个类中。其中包含如何加载菜单和主内容的layout,如何根据xml中的属性以及样式来获取一些初始化值,比如菜单的宽度,是否要阴影以及当前菜单的箭头指示资源文件等,绘制箭头和阴影的实现也在这个文件中。

DraggableDrawer.java实现了menu滑动的一些动画过度效果,其实感觉这里面的东西并不多。

SlidingDrawer.java这个是实现滑动的主要类,要知道如何通过手势滑动布局,这里就可以找到答案。

下面是SlidingDrawer的代码,不过是我修改过了的。

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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
package net.simonvt.menudrawer;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
public class SlidingDrawer extends DraggableDrawer {
    private static final String TAG =
"OverlayDrawer";
    SlidingDrawer(Activity activity, int dragMode) {
        super(activity, dragMode);
    }
    public SlidingDrawer(Context context) {
        super(context);
    }
    public SlidingDrawer(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public SlidingDrawer(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    protected void initDrawer(Context context, AttributeSet attrs, int defStyle) {
        super.initDrawer(context, attrs, defStyle);
        super.addView(mMenuContainer, -1,
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        super.addView(mContentContainer, -1,
new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }
    @Override
    public void openMenu(boolean animate) {
        int animateTo = 0;
        switch
(getPosition()) {
            case
LEFT:
            case
TOP:
                animateTo = mMenuSize;
                break;
            case
RIGHT:
            case
BOTTOM:
                animateTo = -mMenuSize;
                break;
        }
        animateOffsetTo(animateTo, 0, animate);
    }
    @Override
    public void closeMenu(boolean animate) {
        animateOffsetTo(mMenuCloseSize, 0, animate);
    }
    @Override
    protected void onOffsetPixelsChanged(int offsetPixels) {
        if
(USE_TRANSLATIONS) {
            switch
(getPosition()) {
                case
TOP:
                case
BOTTOM:
                    mContentContainer.setTranslationY(offsetPixels);
                    break;
                default:
                    mContentContainer.setTranslationX(offsetPixels);
                    break;
            }
        }
else {
            switch
(getPosition()) {
                case
TOP:
                case
BOTTOM:
                    mContentContainer.offsetTopAndBottom(offsetPixels - mContentContainer.getTop());
                    break;
                default:
                    mContentContainer.offsetLeftAndRight(offsetPixels - mContentContainer.getLeft());
                    break;
            }
        }
        offsetMenu(offsetPixels);
        invalidate();
    }
    @Override
    protected void initPeekScroller() {
        switch
(getPosition()) {
            case
RIGHT:
            case
BOTTOM: {
                final int dx = -mMenuSize / 3;
                mPeekScroller.startScroll(0, 0, dx, 0, PEEK_DURATION);
                break;
            }
            default: {
                final int dx = mMenuSize / 3;
                mPeekScroller.startScroll(0, 0, dx, 0, PEEK_DURATION);
                break;
            }
        }
    }
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        onOffsetPixelsChanged((int) mOffsetPixels);
    }
    @Override
    protected void drawOverlay(Canvas canvas) {
        final int width = getWidth();
        final int height = getHeight();
        final int offsetPixels = (int) mOffsetPixels;
        final float openRatio = Math.abs(mOffsetPixels) / mMenuSize;
        switch
(getPosition()) {
            case
LEFT:
                mMenuOverlay.setBounds(0, 0, offsetPixels, height);
                break;
            case
RIGHT:
                mMenuOverlay.setBounds(width + offsetPixels, 0, width, height);
                break;
            case
TOP:
                mMenuOverlay.setBounds(0, 0, width, offsetPixels);
                break;
            case
BOTTOM:
                mMenuOverlay.setBounds(0, height + offsetPixels, width, height);
                break;
        }
        mMenuOverlay.setAlpha((int) (MAX_MENU_OVERLAY_ALPHA * (1.f - openRatio)));
        mMenuOverlay.draw(canvas);
    }
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        final int width = r - l;
        final int height = b - t;
        if
(USE_TRANSLATIONS) {
            mContentContainer.layout(0, 0, width, height);
        }
else {
            final int offsetPixels = (int) mOffsetPixels;
            if
(getPosition() == Position.LEFT || getPosition() == Position.RIGHT) {
                mContentContainer.layout(offsetPixels, 0, width + offsetPixels, height);
            }
else {
                mContentContainer.layout(0, offsetPixels, width, height + offsetPixels);
            }
        }
        switch
(getPosition()) {
            case
LEFT:
                mMenuContainer.layout(0, 0, mMenuSize, height);
                break;
            case
RIGHT:
                mMenuContainer.layout(width - mMenuSize, 0, width, height);
                break;
            case
TOP:
                mMenuContainer.layout(0, 0, width, mMenuSize);
                break;
            case
BOTTOM:
                mMenuContainer.layout(0, height - mMenuSize, width, height);
                break;
        }
    }
    /**
     * Offsets the menu relative to its original position based on the position of the content.
     *
     * @param offsetPixels The number of pixels the content if offset.
     */
    private void offsetMenu(int offsetPixels) {
        if
(!mOffsetMenu || mMenuSize == 0) {
            return;
        }
        final int width = getWidth();
        final int height = getHeight();
        final int menuSize = mMenuSize;
        final int sign = (int) (mOffsetPixels / Math.abs(mOffsetPixels));
        final float openRatio = Math.abs(mOffsetPixels) / menuSize;
        final int offset = (int) (-0.25f * ((1.0f - openRatio) * menuSize) * sign);
        switch
(getPosition()) {
            case
LEFT: {
                if
(USE_TRANSLATIONS) {
                    if
(offsetPixels > 0) {
                        mMenuContainer.setTranslationX(offset);
                    }
else {
                        mMenuContainer.setTranslationX(-menuSize);
                    }
                }
else {
                    mMenuContainer.offsetLeftAndRight(offset - mMenuContainer.getLeft());
                    mMenuContainer.setVisibility(offsetPixels == 0 ? INVISIBLE : VISIBLE);
                }
                break;
            }
            case
RIGHT: {
                if
(USE_TRANSLATIONS) {
                    if
(offsetPixels != 0) {
                        mMenuContainer.setTranslationX(offset);
                    }
else {
                        mMenuContainer.setTranslationX(menuSize);
                    }
                }
else {
                    final int oldOffset = mMenuContainer.getRight() - width;
                    final int offsetBy = offset - oldOffset;
                    mMenuContainer.offsetLeftAndRight(offsetBy);
                    mMenuContainer.setVisibility(offsetPixels == 0 ? INVISIBLE : VISIBLE);
                }
                break;
            }
            case
TOP: {
                if
(USE_TRANSLATIONS) {
                    if
(offsetPixels > 0) {
                        mMenuContainer.setTranslationY(offset);
                    }
else {
                        mMenuContainer.setTranslationY(-menuSize);
                    }
                }
else {
                    mMenuContainer.offsetTopAndBottom(offset - mMenuContainer.getTop());
                    mMenuContainer.setVisibility(offsetPixels == 0 ? INVISIBLE : VISIBLE);
                }
                break;
            }
            case
BOTTOM: {
                if
(USE_TRANSLATIONS) {
                    if
(offsetPixels != 0) {
                        mMenuContainer.setTranslationY(offset);
                    }
else {
                        mMenuContainer.setTranslationY(menuSize);
                    }
                }
else {
                    final int oldOffset = mMenuContainer.getBottom() - height;
                    final int offsetBy = offset - oldOffset;
                    mMenuContainer.offsetTopAndBottom(offsetBy);
                    mMenuContainer.setVisibility(offsetPixels == 0 ? INVISIBLE : VISIBLE);
                }
                break;
            }
        }
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        final int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        final int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if
(widthMode == MeasureSpec.UNSPECIFIED || heightMode == MeasureSpec.UNSPECIFIED) {
            throw
new IllegalStateException("Must measure with an exact size");
        }
        final int width = MeasureSpec.getSize(widthMeasureSpec);
        final int height = MeasureSpec.getSize(heightMeasureSpec);
                                                                                                                                                                                                                                                                                                                                                                                                                                          
        /** by hejie */
        mMenuSize = width;
        mMenuCloseSize =  (int) (mMenuSize * 0.3f);;
        //mMenuCloseSize =  (int) (mMenuSize * 0.4f);;
       // if (mOffsetPixels == -1) openMenu(false);
         openMenu(false);
       /** */
        int menuWidthMeasureSpec;
        int menuHeightMeasureSpec;
        switch
(getPosition()) {
            case
TOP:
            case
BOTTOM:
                menuWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 0, width);
                menuHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, 0, mMenuSize);
                break;
            default:
                // LEFT/RIGHT
                menuWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 0, mMenuSize);
                menuHeightMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 0, height);
        }
        mMenuContainer.measure(menuWidthMeasureSpec, menuHeightMeasureSpec);
        final int contentWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 0, width);
        final int contentHeightMeasureSpec = getChildMeasureSpec(widthMeasureSpec, 0, height);
        mContentContainer.measure(contentWidthMeasureSpec, contentHeightMeasureSpec);
        setMeasuredDimension(width, height);
        updateTouchAreaSize();
    }
    private boolean isContentTouch(int x, int y) {
        boolean contentTouch =
false;
        switch
(getPosition()) {
            case
LEFT:
                contentTouch = ViewHelper.getLeft(mContentContainer) < x;
                break;
            case
RIGHT:
                contentTouch = ViewHelper.getRight(mContentContainer) > x;
                break;
            case
TOP:
                contentTouch = ViewHelper.getTop(mContentContainer) < y;
                break;
            case
BOTTOM:
                contentTouch = ViewHelper.getBottom(mContentContainer) > y;
                break;
        }
        return
contentTouch;
    }
    protected boolean onDownAllowDrag(int x, int y) {
        switch
(getPosition()) {
            case
LEFT:
                return
(!mMenuVisible && mInitialMotionX <= mTouchSize)
                        || (mMenuVisible && mInitialMotionX >= mOffsetPixels);
            case
RIGHT:
                final int width = getWidth();
                final int initialMotionX = (int) mInitialMotionX;
                return
(!mMenuVisible && initialMotionX >= width - mTouchSize)
                        || (mMenuVisible && initialMotionX <= width + mOffsetPixels);
            case
TOP:
                return
(!mMenuVisible && mInitialMotionY <= mTouchSize)
                        || (mMenuVisible && mInitialMotionY >= mOffsetPixels);
            case
BOTTOM:
                final int height = getHeight();
                return
(!mMenuVisible && mInitialMotionY >= height - mTouchSize)
                        || (mMenuVisible && mInitialMotionY <= height + mOffsetPixels);
        }
        return
false;
    }
    protected boolean onMoveAllowDrag(int x, int y, float dx, float dy) {
        switch
(getPosition()) {
            case
LEFT:
                return
(!mMenuVisible && mInitialMotionX <= mTouchSize && (dx > 0))
                        || (mMenuVisible && x >= mOffsetPixels);
            case
RIGHT:
                final int width = getWidth();
                return
(!mMenuVisible && mInitialMotionX >= width - mTouchSize && (dx < 0))
                        || (mMenuVisible && x <= width + mOffsetPixels);
            case
TOP:
                return
(!mMenuVisible && mInitialMotionY <= mTouchSize && (dy > 0))
                        || (mMenuVisible && y >= mOffsetPixels);
            case
BOTTOM:
                final int height = getHeight();
                return
(!mMenuVisible && mInitialMotionY >= height - mTouchSize && (dy < 0))
                        || (mMenuVisible && y <= height + mOffsetPixels);
        }
        return
false;
    }
    protected void onMoveEvent(float dx, float dy) {
        switch
(getPosition()) {
            case
LEFT:
                setOffsetPixels(Math.min(Math.max(mOffsetPixels + dx, mMenuCloseSize), mMenuSize));
                break;
            case
RIGHT:
                setOffsetPixels(Math.max(Math.min(mOffsetPixels + dx, 0), -mMenuSize));
                break;
            case
TOP:
                setOffsetPixels(Math.min(Math.max(mOffsetPixels + dy, 0), mMenuSize));
                break;
            case
BOTTOM:
                setOffsetPixels(Math.max(Math.min(mOffsetPixels + dy, 0), -mMenuSize));
                break;
        }
    }
    protected void onUpEvent(int x, int y) {
        final int offsetPixels = (int) mOffsetPixels;
        switch
(getPosition()) {
            case
LEFT: {
                if
(mIsDragging) {
                    mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
                    final int initialVelocity = (int) getXVelocity(mVelocityTracker);
                    mLastMotionX = x;
                    animateOffsetTo(initialVelocity > 0 ? mMenuSize : mMenuCloseSize, initialVelocity,
true);
                    // Close the menu when content is clicked while the menu is visible.
                }
else if
(mMenuVisible && x > offsetPixels) {
                    closeMenu();
                }
                break;
            }
            case
TOP: {
                if
(mIsDragging) {
                    mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
                    final int initialVelocity = (int) getYVelocity(mVelocityTracker);
                    mLastMotionY = y;
                    animateOffsetTo(initialVelocity > 0 ? mMenuSize : 0, initialVelocity,
true);
                    // Close the menu when content is clicked while the menu is visible.
                }
else if
(mMenuVisible && y > offsetPixels) {
                    closeMenu();
                }
                break;
            }
            case
RIGHT: {
                final int width = getWidth();
                if
(mIsDragging) {
                    mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
                    final int initialVelocity = (int) getXVelocity(mVelocityTracker);
                    mLastMotionX = x;
                    animateOffsetTo(initialVelocity > 0 ? 0 : -mMenuSize, initialVelocity,
true);
                    // Close the menu when content is clicked while the menu is visible.
                }
else if
(mMenuVisible && x < width + offsetPixels) {
                    closeMenu();
                }
                break;
            }
            case
BOTTOM: {
                if
(mIsDragging) {
                    mVelocityTracker.computeCurrentVelocity(1000, mMaxVelocity);
                    final int initialVelocity = (int) getYVelocity(mVelocityTracker);
                    mLastMotionY = y;
                    animateOffsetTo(initialVelocity < 0 ? -mMenuSize : 0, initialVelocity,
true);
                    // Close the menu when content is clicked while the menu is visible.
                }
else if
(mMenuVisible && y < getHeight() + offsetPixels) {
                    closeMenu();
                }
                break;
            }
        }
    }
    protected boolean checkTouchSlop(float dx, float dy) {
        switch
(getPosition()) {
            case
TOP:
            case
BOTTOM:
                return
Math.abs(dy) > mTouchSlop && Math.abs(dy) > Math.abs(dx);
            default:
                return
Math.abs(dx) > mTouchSlop && Math.abs(dx) > Math.abs(dy);
        }
    }
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = ev.getAction() & MotionEvent.ACTION_MASK;
        if
(action == MotionEvent.ACTION_UP || action == MotionEvent.ACTION_CANCEL) {
            mActivePointerId = INVALID_POINTER;
            mIsDragging =
false;
            if
(mVelocityTracker != null) {
                mVelocityTracker.recycle();
                mVelocityTracker =
null;
            }
            if
(Math.abs(mOffsetPixels) > mMenuSize / 2) {
                openMenu();
            }
else {
                closeMenu();
            }
            return
false;
        }
        if
(action == MotionEvent.ACTION_DOWN && mMenuVisible && isCloseEnough()) {
            setOffsetPixels(0);
            stopAnimation();
            endPeek();
            setDrawerState(STATE_CLOSED);
            mIsDragging =
false;
        }
        // Always intercept events over the content while menu is visible.
        if
(mMenuVisible) {
            int index = 0;
            if
(mActivePointerId != INVALID_POINTER) {
                index = ev.findPointerIndex(mActivePointerId);
                index = index == -1 ? 0 : index;
            }
            final int x = (int) ev.getX(index);
            final int y = (int) ev.getY(index);
            if
(isContentTouch(x, y)) {
                return
true;
            }
        }
        if
(!mMenuVisible && !mIsDragging && mTouchMode == TOUCH_MODE_NONE) {
            return
false;
        }
        if
(action != MotionEvent.ACTION_DOWN && mIsDragging) {
            return
true;
        }
        switch
(action) {
            case
MotionEvent.ACTION_DOWN: {
                mLastMotionX = mInitialMotionX = ev.getX();
                mLastMotionY = mInitialMotionY = ev.getY();
                final boolean allowDrag = onDownAllowDrag((int) mLastMotionX, (int) mLastMotionY);
                mActivePointerId = ev.getPointerId(0);
                if
(allowDrag) {
                    setDrawerState(mMenuVisible ? STATE_OPEN : STATE_CLOSED);
                    stopAnimation();
                    endPeek();
                    mIsDragging =
false;
                }
                break;
            }
            case
MotionEvent.ACTION_MOVE: {
                final int activePointerId = mActivePointerId;
                if
(activePointerId == INVALID_POINTER) {
                    // If we don't have a valid id, the touch down wasn't on content.
                    break;
                }
                final int pointerIndex = ev.findPointerIndex(activePointerId);
                if
(pointerIndex == -1) {
                    mIsDragging =
false;
                    mActivePointerId = INVALID_POINTER;
                    endDrag();
                    closeMenu(true);
                    return
false;
                }
                final float x = ev.getX(pointerIndex);
                final float dx = x - mLastMotionX;
                final float y = ev.getY(pointerIndex);
                final float dy = y - mLastMotionY;
                if
(checkTouchSlop(dx, dy)) {
                    if
(mOnInterceptMoveEventListener != null
&& (mTouchMode == TOUCH_MODE_FULLSCREEN || mMenuVisible)
                            && canChildrenScroll((int) dx, (int) dy, (int) x, (int) y)) {
                        endDrag();
// Release the velocity tracker
                        requestDisallowInterceptTouchEvent(true);
                        return
false;
                    }
                    final boolean allowDrag = onMoveAllowDrag((int) x, (int) y, dx, dy);
                    if
(allowDrag) {
                        setDrawerState(STATE_DRAGGING);
                        mIsDragging =
true;
                        mLastMotionX = x;
                        mLastMotionY = y;
                    }
                }
                break;
            }
            case
MotionEvent.ACTION_POINTER_UP:
                onPointerUp(ev);
                mLastMotionX = ev.getX(ev.findPointerIndex(mActivePointerId));
                mLastMotionY = ev.getY(ev.findPointerIndex(mActivePointerId));
                break;
        }
        if
(mVelocityTracker == null) mVelocityTracker = VelocityTracker.obtain();
        mVelocityTracker.addMovement(ev);
        return
mIsDragging;
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if
(!mMenuVisible && !mIsDragging && mTouchMode == TOUCH_MODE_NONE) {
            return
false;
        }
        final int action = ev.getAction() & MotionEvent.ACTION_MASK;
        if
(mVelocityTracker == null) mVelocityTracker = VelocityTracker.obtain();
        mVelocityTracker.addMovement(ev);
        switch
(action) {
            case
MotionEvent.ACTION_DOWN: {
                mLastMotionX = mInitialMotionX = ev.getX();
                mLastMotionY = mInitialMotionY = ev.getY();
                final boolean allowDrag = onDownAllowDrag((int) mLastMotionX, (int) mLastMotionY);
                mActivePointerId = ev.getPointerId(0);
                if
(allowDrag) {
                    stopAnimation();
                    endPeek();
                    startLayerTranslation();
                }
                break;
            }
            case
MotionEvent.ACTION_MOVE: {
                final int pointerIndex = ev.findPointerIndex(mActivePointerId);
                if
(pointerIndex == -1) {
                    mIsDragging =
false;
                    mActivePointerId = INVALID_POINTER;
                    endDrag();
                    closeMenu(true);
                    return
false;
                }
                if
(!mIsDragging) {
                    final float x = ev.getX(pointerIndex);
                    final float dx = x - mLastMotionX;
                    final float y = ev.getY(pointerIndex);
                    final float dy = y - mLastMotionY;
                    if
(checkTouchSlop(dx, dy)) {
                        final boolean allowDrag = onMoveAllowDrag((int) x, (int) y, dx, dy);
                        if
(allowDrag) {
                            setDrawerState(STATE_DRAGGING);
                            mIsDragging =
true;
                            mLastMotionX = x;
                            mLastMotionY = y;
                        }
else {
                            mInitialMotionX = x;
                            mInitialMotionY = y;
                        }
                    }
                }
                if
(mIsDragging) {
                    startLayerTranslation();
                    final float x = ev.getX(pointerIndex);
                    final float dx = x - mLastMotionX;
                    final float y = ev.getY(pointerIndex);
                    final float dy = y - mLastMotionY;
                    mLastMotionX = x;
                    mLastMotionY = y;
                    onMoveEvent(dx, dy);
                }
                break;
            }
            case
MotionEvent.ACTION_CANCEL:
            case
MotionEvent.ACTION_UP: {
                int index = ev.findPointerIndex(mActivePointerId);
                index = index == -1 ? 0 : index;
                final int x = (int) ev.getX(index);
                final int y = (int) ev.getY(index);
                onUpEvent(x, y);
                mActivePointerId = INVALID_POINTER;
                mIsDragging =
false;
                break;
            }
            case
MotionEvent.ACTION_POINTER_DOWN:
                final int index = (ev.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK)
                        >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
                mLastMotionX = ev.getX(index);
                mLastMotionY = ev.getY(index);
                mActivePointerId = ev.getPointerId(index);
                break;
            case
MotionEvent.ACTION_POINTER_UP:
                onPointerUp(ev);
                mLastMotionX = ev.getX(ev.findPointerIndex(mActivePointerId));
                mLastMotionY = ev.getY(ev.findPointerIndex(mActivePointerId));
                break;
        }
        return
true;
    }
    private void onPointerUp(MotionEvent ev) {
        final int pointerIndex = ev.getActionIndex();
        final int pointerId = ev.getPointerId(pointerIndex);
        if
(pointerId == mActivePointerId) {
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mLastMotionX = ev.getX(newPointerIndex);
            mActivePointerId = ev.getPointerId(newPointerIndex);
            if
(mVelocityTracker != null) {
                mVelocityTracker.clear();
            }
        }
    }
}

onInterceptTouchEvent和onTouchEvent是捕捉用户滑动事件的方法,其中onInterceptTouchEvent的用途是在onTouchEvent之前截获事件,并提供一次改变事件传递方向的机会。当用户滑动屏幕,onTouchEvent会被调用并执行case MotionEvent.ACTION_MOVE:下面的代码,通过比较当前触摸点和上次触摸点的位置记录x和y方向上的偏移量,然后根据这个偏移量移动继承自ViewGroup的MenuDrawer。其实MenuDrawer的这个过程和ViewPager非常相似。

1
2
3
4
5
6
7
final float x = ev.getX(pointerIndex);
final float dx = x - mLastMotionX;
final float y = ev.getY(pointerIndex);
final float dy = y - mLastMotionY;
mLastMotionX = x;
mLastMotionY = y;
onMoveEvent(dx, dy)

onMoveEvent就是移动ViewGroup的方法。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
protected void onMoveEvent(float dx, float dy) {
    switch
(getPosition()) {
        case
LEFT:
            setOffsetPixels(Math.min(Math.max(mOffsetPixels + dx, mMenuCloseSize), mMenuSize));
            break;
        case
RIGHT:
            setOffsetPixels(Math.max(Math.min(mOffsetPixels + dx, 0), -mMenuSize));
            break;
        case
TOP:
            setOffsetPixels(Math.min(Math.max(mOffsetPixels + dy, 0), mMenuSize));
            break;
        case
BOTTOM:
            setOffsetPixels(Math.max(Math.min(mOffsetPixels + dy, 0), -mMenuSize));
            break;
    }
}

根据上面的代码我们知道手指滑动屏幕的过程中onTouchEvent onMoveEventsetOffsetPixels相继被调用。setOffsetPixels之后最终会在offsetMenu中用offsetLeftAndRight()来移动ViewGroup。

【开源项目5】测滑菜单MenuDrawer的使用以及解析的更多相关文章

  1. 美团动态线程池实践思路开源项目(DynamicTp),线程池源码解析及通知告警篇

    大家好,这篇文章我们来聊下动态线程池开源项目(DynamicTp)的通知告警模块.目前项目提供以下通知告警功能,每一个通知项都可以独立配置是否开启.告警阈值.告警间隔时间.平台等,具体代码请看core ...

  2. android側滑菜单-DrawerLayout的基本使用

    眼下主流App开发中,部分是以側滑菜单为主布局架构,曾经做android側滑菜单时.大多选择使用github上的第三方开源框架SildingMenu,可是这个框架还是稍显笨重.好消息是google已经 ...

  3. 安卓开发笔记——关于开源项目SlidingMenu的使用介绍(仿QQ5.0侧滑菜单)

    记得去年年末的时候写过这个侧滑效果,当时是利用自定义HorizontalScrollView来实现的,效果如下: 有兴趣的朋友可以看看这篇文件<安卓开发笔记——自定义HorizontalScro ...

  4. [ionic开源项目教程] - 第8讲 根据菜单分类加载数据(重要)

    [ionic开源项目教程] - 第8讲  根据菜单分类加载数据(重要) [效果图] 注意 今天遇到一个比较棘手的问题,就是左右滑动菜单的设计不合理性,所以tab1.html对应的视图层和control ...

  5. Android开源项目分类汇总

    目前包括: Android开源项目第一篇——个性化控件(View)篇   包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView. ...

  6. GitHub上史上最全的Android开源项目分类汇总 (转)

    GitHub上史上最全的Android开源项目分类汇总 标签: github android 开源 | 发表时间:2014-11-23 23:00 | 作者:u013149325 分享到: 出处:ht ...

  7. 【Android】Android开源项目分类汇总

    第一部分 个性化控件(View) 主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.Pro ...

  8. Android开源项目汇总【转】

    主要介绍那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Gallery.GridView.ImageView.ProgressBar.TextView ...

  9. GitHub上史上最全的Android开源项目分类汇总

    今天在看博客的时候,无意中发现了 @Trinea 在GitHub上的一个项目 Android开源项目分类汇总 ,由于类容太多了,我没有一个个完整地看完,但是里面介绍的开源项目都非常有参考价值,包括很炫 ...

随机推荐

  1. win7 文件共享 xp

    前几天因为需要将win7内一文件夹共享给XP使用,因为NT5跟NT6安全机制的问题,共享的实现没有XP共享的方便,很多人是牺牲(关闭)了win7的系统防火墙才达到共享给XP的目的,但是关闭防火墙势必会 ...

  2. POJ 1852 Ants (等价思考)

    题意:在一根杆上有 n 只蚂蚁,速度为1,方向不定,如果相碰,则反向运动,问你最长的时间和最短时间,所有蚂蚁都掉下杆去. 析:换个方法想,如果两只蚂蚁相碰了,会有什么现象?其实就和没有碰撞是一样的,没 ...

  3. Win8制作和使用恢复盘

    制作和使用恢复盘要制作恢复盘,请执行以下操作:注:确保计算机连接到交流电源.1. 将指针移至屏幕的右上角或右下角以显示超级按钮,然后单击搜索.2. 根据操作系统的不同,执行以下某项操作:• 在 Win ...

  4. CSS line-height 和 vertical-align 精解(下篇)

    申明本文转自:http://hi.baidu.com/wolongxzg/item/2383860ec8ac8b173a53eeb0 vertical-align 7.4.1 语法 vertical- ...

  5. SQL SERVER 2005如何建立自动备份的维护计划

    SQL Server 2005中可以使用维护计划来为数据库自动备份,减少数据库管理员的工作负担.其使用方法如下: (1)启动[sql server Management Studio],在[对象资源管 ...

  6. DateTable与List<T>相互转换 及JSON与DataTable(DataSet)相互转化

    http://www.360doc.com/content/13/0712/09/10504424_299336674.shtml Linq处理List数据 http://blog.163.com/l ...

  7. 1.单一职责原则(Single Responsibility Principle)

    1.定义 就一个类而言,应该仅有一个引起它变化的原因. 2.定义解读 这是六大原则中最简单的一种,通俗点说,就是不存在多个原因使得一个类发生变化,也就是一个类只负责一种职责的工作. 3.优点 类的复杂 ...

  8. Qt技巧:QProcess与外部程序的调用

    项目做到一定阶段,常常须要在原来的project上调用外部程序. Qt为此提供了QProcess类,QProcess可用于完毕启动外部程序,并与之交互通信. 一.启动外部程序的两种方式: (1)一体式 ...

  9. rails中的语法

    1. erb文件中的语法说明 erb文件中常混合使用Ruby语言和html语言,以下为两种常见的格式 <% 写逻辑脚本(Ruby语法) %> <%= 直接输出变量值或运算结果 %&g ...

  10. linux下登入mysql和加压zip文件

    1.类似于window中cmd登入一样 : mysql -u root -p   ---->  回车 ---> 输入密码  就可以了 2. unzip abc.zip  直接进行解压    ...