ViewDragHelper让你处理View拖动时,代码减半!

出处:ViewDragHelper是V4包下的一个文件。

我们在自定义ViewGroup的时候,有时候觉得很头疼,其中很大一部分原因就是因为事件处理太麻烦,需要记录大量的成员变量,还有各种判断等等。 Google也感觉到了这个麻烦,所以ViewDragHelper就出现了,ViewDragHelper功能到底是什么呢?从字面意思上看是View拖拽的帮助类,简而言之就是,在简化View拖拽的时候的代码量。我们先来看一看到底这个类的帮助有多大? 先来看一个测拉菜单效果

先来分析一下,如果我们不借助这个帮助类实现情况: 1、重写一个RelativeLayout; 2、重写其中的onInterceptTouchEvent(做相应的事件拦截操作) 2、重写其中的onTouchEvent方法(这里面做大量的代码) 3、定义一个Scroller变量,用来控制手指松开以后的操作 这里我就不去写代码了,,代码量肯定很大! 再来看看借助ViewDragHelper类实现的代码

/** * Created by gyzhong on 15/4/8. */{private ViewDragHelper mViewDragHelper;private View mCaptureView;private float mInitialMotionX;private float mInitialMotionY;private boolean mIsUnableToDrag;private int mSlideRange;private float mSlideOffset;public VdhLayout01(Context context) {this(context, null);}public VdhLayout01(Context context, AttributeSet attrs) {this(context, attrs, 0);}public VdhLayout01(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initView(context);}(Context context) {mViewDragHelper = ViewDragHelper.create(this, 1.0f, new DragHelperCall());}() {super.onFinishInflate();mCaptureView = findViewById(R.id.id_capture_view);TextView textView = (TextView) findViewById(R.id.id_text);textView.setText(Shakespeare.DIALOGUE[0]);}() {super.onAttachedToWindow();mCaptureView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {() {mCaptureView.getViewTreeObserver().removeOnPreDrawListener(this);mSlideRange = mCaptureView.getMeasuredWidth();return false;}});}.(View child, int pointerId) {return child == mCaptureView;}(View changedView, int left, int top, int dx, int dy) {super.onViewPositionChanged(changedView, left, top, dx, dy);mSlideOffset = left * 1.0f / mSlideRange*2;}(View child, int left, int dx) {return clamp(left, 0, mSlideRange / 2);}(View child) {return mSlideRange/2;}(View releasedChild, float xvel, float yvel) {int finalLeft;if (xvel > 0 || xvel == 0 && mSlideOffset > .5f) {finalLeft = mSlideRange/2 ;}else {finalLeft = 0 ;}mViewDragHelper.settleCapturedViewAt( finalLeft, mCaptureView.getTop());invalidate();}}() {if (mViewDragHelper.continueSettling(true)){ViewCompat.postInvalidateOnAnimation(this);}}(int value, int min, int max) {return Math.min(max, Math.max(min, value));}(MotionEvent ev) {final int action = MotionEventCompat.getActionMasked(ev);if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {mViewDragHelper.cancel();return false;}if (!isEnabled() || (mIsUnableToDrag && action != MotionEvent.ACTION_DOWN)) {mViewDragHelper.cancel();return super.onInterceptTouchEvent(ev);}int index = MotionEventCompat.getActionIndex(ev) ;switch (action) {case MotionEvent.ACTION_DOWN: {final float x = ev.getX();final float y = ev.getY();mInitialMotionX = x;mInitialMotionY = y;mIsUnableToDrag = false;break;}case MotionEvent.ACTION_MOVE: {final float x = ev.getX();final float y = ev.getY();final float adx = Math.abs(x – mInitialMotionX);final float ady = Math.abs(y – mInitialMotionY);int slop = mViewDragHelper.getTouchSlop();if (adx > slop && adx < ady) {mIsUnableToDrag = true;mViewDragHelper.cancel();return false;}break;}}return mViewDragHelper.shouldInterceptTouchEvent(ev);}(MotionEvent event) {mViewDragHelper.processTouchEvent(event);return true;}}一路走来,我们无法猜测将是迎接什么样的风景,

ViewDragHelper让你处理View拖动时,代码减半!

相关文章:

你感兴趣的文章:

标签云: