@Override
public void computeScroll() {
if (mTopViewDragHelper.continueSettling(true)) {
invalidate();
}
}
public void closeDrawer() {
if (mIsOpen) {
mTopViewDragHelper.smoothSlideViewTo(mDrawerView, mDrawerView.getLeft(), -mDrawerView.getHeight());
invalidate();
}
}
public void openDrawer() {
if (!mIsOpen) {
mTopViewDragHelper.smoothSlideViewTo(mDrawerView, mDrawerView.getLeft(), 0);
invalidate();
}
}
public boolean isDrawerOpened() {
return mIsOpen;
}
//Step3:重写onInterceptTouchEvent回调ViewDragHelper中对应的方法.
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return mTopViewDragHelper.shouldInterceptTouchEvent(ev);
}
//Step3:重写onTouchEvent回调ViewDragHelper中对应的方法.
@Override
public boolean onTouchEvent(MotionEvent event) {
mTopViewDragHelper.processTouchEvent(event);
return true;
}
@Override
protected LayoutParams generateDefaultLayoutParams() {
return new MarginLayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
}
@Override
protected LayoutParams generateLayoutParams(LayoutParams p) {
return new MarginLayoutParams§;
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new MarginLayoutParams(getContext(), attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int measureWidth = MeasureSpec.getSize(widthMeasureSpec);
int measureHeight = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(measureWidth, measureHeight);
mContentView = getChildAt(0);
mDrawerView = getChildAt(1);
MarginLayoutParams params = (MarginLayoutParams) mContentView.getLayoutParams();
int childWidthMeasureSpec = MeasureSpec.makeMeasureSpec(
measureWidth- (params.leftMargin + params.rightMargin), MeasureSpec.EXACTLY);
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec(
measureHeight - (params.topMargin + params.bottomMargin), MeasureSpec.EXACTLY);
mContentView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
mDrawerView.measure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
if (changed) {
MarginLayoutParams params = (MarginLayoutParams) mContentView.getLayoutParams();
mContentView.layout(params.leftMargin, params.topMargin,
mContentView.getMeasuredWidth() + params.leftMargin,
mContentView.getMeasuredHeight() + params.topMargin);
params = (MarginLayoutParams) mDrawerView.getLayoutParams();
mDrawerView.layout(params.leftMargin, mCurTop + params.topMargin,
mDrawerView.getMeasuredWidth() + params.leftMargin,
mCurTop + mDrawerView.getMeasuredHeight() + params.topMargin);
}
}
}
怎么样,简单吧。效果也有了,代码也有了,ViewDragHelper也体验了,接下来就该苦逼的看源码了。
【工匠若水 http://blog.youkuaiyun.com/yanbober 未经允许严禁转载,请尊重作者劳动成果。私信联系我】
3 ViewDragHelper局部源码浅析
==========================
上面的例子中我们可以知道,使用ViewDragHelper的第一步就是通过他提供的静态工厂方法create获取实例,因为ViewDragHelper的构造方法是私有的。既然这样那我们先看下这些静态工厂方法,如下:
public class ViewDragHelper {
public static ViewDragHelper create(ViewGroup forParent, float sensitivity, Callback cb) {
final ViewDragHelper helper = create(forParent, cb);
helper.mTouchSlop = (int) (helper.mTouchSlop * (1 / sensitivity));
return helper;
}
public static ViewDragHelper create(ViewGroup forParent, Callback cb) {
return new ViewDragHelper(forParent.getContext(), forParent, cb);
}
}
可以看见,三个参数的create方法实质调运的还是两个参数的create。其中forParent一般是我们自定义的ViewGroup,cb是控制子View相关状态的回调抽象类实现对象,sensitivity是用来调节mTouchSlop的,至于mTouchSlop是啥以及sensitivity的作用下面会有解释。接着可以发现两个参数的create实质是调运了ViewDragHelper的构造函数,那我们就来分析一下这个构造函数,如下源码:
private ViewDragHelper(Context context, ViewGroup forParent, Callback cb) {
…
//对参数进行赋值
mParentView = forParent;
mCallback = cb;
//通过ViewConfiguration等将dp转px得到mEdgeSize
final ViewConfiguration vc = ViewConfiguration.get(context);
final float density = context.getResources().getDisplayMetrics().density;
mEdgeSize = (int) (EDGE_SIZE * density + 0.5f);
//通过ViewConfiguration获取TouchSlop,默认为8
mTouchSlop = vc.getScaledTouchSlop();
//获得允许执行一个fling手势动作的最大速度值
mMaxVelocity = vc.getScaledMaximumFlingVelocity();
//获得允许执行一个fling手势动作的最小速度值
mMinVelocity = vc.getScaledMinimumFlingVelocity();
//通过兼容包的ScrollerCompat实例化Scroller,动画插值器为sInterpolator
mScroller = ScrollerCompat.create(context, sInterpolator);
}
可以看见,构造函数其实没有做啥特别的事情,主要就是一些参数的实例化,最主要的就是实例化了一个Scroller的内部成员,而且还在ViewDragHelper中重写了插值器,如下:
private static final Interpolator sInterpolator = new Interpolator() {
public float getInterpolation(float t) {
t -= 1.0f;
return t * t * t * t * t + 1.0f;
}
};
关于动画插值器这里不再说了,之前博文有讲过。我们还是把视线回到上面实例部分,可以看见,在获取ViewDragHelper实例之后我们接着重写了ViewGroup的onInterceptTouchEvent和onTouchEvent方法,在其中触发了ViewDragHelper的shouldInterceptTouchEvent和processTouchEvent方法。所以下面我们就来分析这两个方法,首先我们看下shouldInterceptTouchEvent方法,如下:
//这玩意返回值的作用在前面博客中有分析,我们先来看下ACTION_DOWN事件
public boolean shouldInterceptTouchEvent(MotionEvent ev) {
final int action = MotionEventCompat.getActionMasked(ev);
final int actionIndex = MotionEventCompat.getActionIndex(ev);
if (action == MotionEvent.ACTION_DOWN) {
//每次ACTION_DOWN都会调用cancel(),该方法中mVelocityTracker被清空&#x