本文转载自微信公众号「Android开发编程」,作者Android开发编程 。转载本文请联系Android开发编程公众号。
10余年的南阳网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。成都全网营销的优势是能够根据用户设备显示端的尺寸不同,自动调整南阳建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。成都创新互联从事“南阳网站设计”,“南阳网站推广”以来,每个客户项目都认真落实执行。
ViewDragHelper类,是用来处理View边界拖动相关的类;
主要功能处理在View上的触摸事件,记录触摸点、计算距离、滚动动画、状态回调等,如果我们自己手动实现自然会很麻烦还可能出错,而这个类会帮助我们大大简化工作量;
今天我们就来分析一波;
1、ViewDragHelper create(ViewGroup forParent, Callback cb)
一个静态的创建方法;
2、shouldInterceptTouchEvent(MotionEvent ev)
处理事件分发的(怎么说这个方法呢?主要是将ViewGroup的事件分发,委托给ViewDragHelper进行处理);
3、processTouchEvent(MotionEvent event)
处理相应TouchEvent的方法,这里要注意一个问题,处理相应的TouchEvent的时候要将结果返回为true,消费本次事件,否则将无法使用ViewDragHelper处理相应的拖拽事件;
4、ViewDragHelper.Callback的API
tryCaptureView(View child, int pointerId) 这是一个抽象类,必须去实现,也只有在这个方法返回true的时候下面的方法才会生效;
onViewDragStateChanged(int state) 当状态改变的时候回调,返回相应的状态(这里有三种状态);
onViewPositionChanged(View changedView, int left, int top, int dx, int dy) 当你拖动的View位置发生改变的时候回调;
onViewCaptured(View capturedChild, int activePointerId)捕获View的时候调用的方法
onViewReleased(View releasedChild, float xvel, float yvel) 当View停止拖拽的时候调用的方法
clampViewPositionVertical(View child, int top, int dy) 竖直拖拽的时候回调的方法
clampViewPositionHorizontal(View child, int left, int dx) 水平拖拽的时候回调的方法
1、初始化
- private ViewDragHelper(Context context, ViewGroup forParent, Callback cb) {
- ...
- mParentView = forParent;//BaseView
- mCallback = cb;//callback
- final ViewConfiguration vc = ViewConfiguration.get(context);
- final float density = context.getResources().getDisplayMetrics().density;
- mEdgeSize = (int) (EDGE_SIZE * density + 0.5f);//边界拖动距离范围
- mTouchSlop = vc.getScaledTouchSlop();//拖动距离阈值
- mScroller = new OverScroller(context, sInterpolator);//滚动器
- }
2.拦截事件处理
该类提供了boolean shouldInterceptTouchEvent(MotionEvent)方法:
- override fun onInterceptTouchEvent(ev: MotionEvent?) =
- dragHelper?.shouldInterceptTouchEvent(ev) ?: super.onInterceptTouchEvent(ev)
该方法用于处理mParentView是否拦截此次事件
- public boolean shouldInterceptTouchEvent(MotionEvent ev) {
- ...
- switch (action) {
- ...
- case MotionEvent.ACTION_MOVE: {
- if (mInitialMotionX == null || mInitialMotionY == null) break;
- // First to cross a touch slop over a draggable view wins. Also report edge drags.
- final int pointerCount = ev.getPointerCount();
- for (int i = 0; i < pointerCount; i++) {
- final int pointerId = ev.getPointerId(i);
- // If pointer is invalid then skip the ACTION_MOVE.
- if (!isValidPointerForActionMove(pointerId)) continue;
- final float x = ev.getX(i);
- final float y = ev.getY(i);
- final float dx = x - mInitialMotionX[pointerId];
- final float dy = y - mInitialMotionY[pointerId];
- final View toCapture = findTopChildUnder((int) x, (int) y);
- final boolean pastSlop = toCapture != null && checkTouchSlop(toCapture, dx, dy);
- ...
- //判断pointer的拖动边界
- reportNewEdgeDrags(dx, dy, pointerId);
- ...
- }
- saveLastMotion(ev);
- break;
- }
- ...
- }
- return mDragState == STATE_DRAGGING;
- }
拦截事件的前提是mDragState为STATE_DRAGGING,也就是正在拖动状态下才会拦截,那么什么时候会变为拖动状态呢?当ACTION_MOVE时,调用reportNewEdgeDrags方法:
- private void reportNewEdgeDrags(float dx, float dy, int pointerId) {
- int dragsStarted = 0;
- //判断是否在Left边缘进行滑动
- if (checkNewEdgeDrag(dx, dy, pointerId, EDGE_LEFT)) {
- dragsStarted |= EDGE_LEFT;
- }
- if (checkNewEdgeDrag(dy, dx, pointerId, EDGE_TOP)) {
- dragsStarted |= EDGE_TOP;
- }
- ...
- if (dragsStarted != 0) {
- mEdgeDragsInProgress[pointerId] |= dragsStarted;
- //回调拖动的边
- mCallback.onEdgeDragStarted(dragsStarted, pointerId);
- }
- }
- private boolean checkNewEdgeDrag(float delta, float odelta, int pointerId, int edge) {
- final float absDelta = Math.abs(delta);
- final float absODelta = Math.abs(odelta);
- //是否支持edge的拖动以及是否满足拖动距离的阈值
- if ((mInitialEdgesTouched[pointerId] & edge) != edge || (mTrackingEdges & edge) == 0
- || (mEdgeDragsLocked[pointerId] & edge) == edge
- || (mEdgeDragsInProgress[pointerId] & edge) == edge
- || (absDelta <= mTouchSlop && absODelta <= mTouchSlop)) {
- return false;
- }
- if (absDelta < absODelta * 0.5f && mCallback.onEdgeLock(edge)) {
- mEdgeDragsLocked[pointerId] |= edge;
- return false;
- }
- return (mEdgeDragsInProgress[pointerId] & edge) == 0 && absDelta > mTouchSlop;
- }
可以看到,当ACTION_MOVE时,会尝试找到pointer对应的拖动边界,这个边界可以由我们来制定,比如侧滑关闭页面是从左侧开始的,所以我们可以调用setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT)来设置只支持左侧滑动。而一旦有滚动发生,就会回调callback的onEdgeDragStarted方法,交由我们做如下操作:
- override fun onEdgeDragStarted(edgeFlags: Int, pointerId: Int) {
- super.onEdgeDragStarted(edgeFlags, pointerId)
- dragHelper?.captureChildView(getChildAt(0), pointerId)
- }
- 我们调用了ViewDragHelper的captureChildView方法:
- public void captureChildView(View childView, int activePointerId) {
- mCapturedView = childView;//记录拖动view
- mActivePointerId = activePointerId;
- mCallback.onViewCaptured(childView, activePointerId);
- setDragState(STATE_DRAGGING);//设置状态为开始拖动
- }
此时,就记录了拖动的View,并将状态置为拖动,那么在下次ACTION_MOVE的时候,该mParentView就会拦截事件,交由自己的onTouchEvent方法处理拖动了;
3.拖动事件处理
该类提供了void processTouchEvent(MotionEvent)方法,通常我们需要这么写:
- override fun onTouchEvent(event: MotionEvent?): Boolean {
- dragHelper?.processTouchEvent(event)//交由ViewDragHelper处理
- return true
- }
该方法用于处理mParentView拦截事件后的拖动处理:
- public void processTouchEvent(MotionEvent ev) {
- ...
- switch (action) {
- ...
- case MotionEvent.ACTION_MOVE: {
- if (mDragState == STATE_DRAGGING) {
- // If pointer is invalid then skip the ACTION_MOVE.
- if (!isValidPointerForActionMove(mActivePointerId)) break;
- final int index = ev.findPointerIndex(mActivePointerId);
- final float x = ev.getX(index);
- final float y = ev.getY(index);
- //计算距离上次的拖动距离
- final int idx = (int) (x - mLastMotionX[mActivePointerId]);
- final int idy = (int) (y - mLastMotionY[mActivePointerId]);
- dragTo(mCapturedView.getLeft() + idx, mCapturedView.getTop() + idy, idx, idy);//处理拖动
- saveLastMotion(ev);//记录当前触摸点
- }...
- break;
- }
- ...
- case MotionEvent.ACTION_UP: {
- if (mDragState == STATE_DRAGGING) {
- releaseViewForPointerUp();//释放拖动view
- }
- cancel();
- break;
- }...
- }
- }
(1)拖动
ACTION_MOVE时,会计算出pointer距离上次的位移,然后计算出capturedView的目标位置,进行拖动处理;
- private void dragTo(int left, int top, int dx, int dy) {
- int clampedX = left;
- int clampedY = top;
- final int oldLeft = mCapturedView.getLeft();
- final int oldTop = mCapturedView.getTop();
- if (dx != 0) {
- clampedX = mCallback.clampViewPositionHorizontal(mCapturedView, left, dx);//通过callback获取真正的移动值
- ViewCompat.offsetLeftAndRight(mCapturedView, clampedX - oldLeft);//进行位移
- }
- if (dy != 0) {
- clampedY = mCallback.clampViewPositionVertical(mCapturedView, top, dy);
- ViewCompat.offsetTopAndBottom(mCapturedView, clampedY - oldTop);
- }
- if (dx != 0 || dy != 0) {
- final int clampedDx = clampedX - oldLeft;
- final int clampedDy = clampedY - oldTop;
- mCallback.onViewPositionChanged(mCapturedView, clampedX, clampedY,
- clampedDx, clampedDy);//callback回调移动后的位置
- }
- }
通过callback的clampViewPositionHorizontal方法决定实际移动的水平距离,通常都是返回left值,即拖动了多少就移动多少;
通过callback的onViewPositionChanged方法,可以对View拖动后的新位置做一些处理,如;
- override fun onViewPositionChanged(changedView: View?, left: Int, top: Int, dx: Int, dy: Int) {
- super.onViewPositionChanged(changedView, left, top, dx, dy)
- //当新的left位置到达width时,即滑动除了界面,关闭页面
- if (left >= width && context is Activity && !context.isFinishing) {
- context.finish()
- }
- }
(2)释放
而ACTION_UP动作时,要释放拖动View
- private void releaseViewForPointerUp() {
- ...
- dispatchViewReleased(xvel, yvel);
- }
- private void dispatchViewReleased(float xvel, float yvel) {
- mReleaseInProgress = true;
- mCallback.onViewReleased(mCapturedView, xvel, yvel);//callback回调释放
- mReleaseInProgress = false;
- if (mDragState == STATE_DRAGGING) {
- // onViewReleased didn't call a method that would have changed this. Go idle.
- setDragState(STATE_IDLE);//重置状态
- }
- }
通常在callback的onViewReleased方法中,我们可以判断当前释放点的位置,从而决定是要回弹页面还是滑出屏幕
- override fun onViewReleased(releasedChild: View?, xvel: Float, yvel: Float) {
- super.onViewReleased(releasedChild, xvel, yvel)
- //滑动速度到达一定值时直接关闭
- if (xvel >= 300) {//滑动页面到屏幕外,关闭页面
- dragHelper?.settleCapturedViewAt(width, 0)
- } else {//回弹页面
- dragHelper?.settleCapturedViewAt(0, 0)
- }
- //刷新,开始关闭或重置动画
- invalidate()
- }
如滑动速度大于300时,我们调用settleCapturedViewAt方法将页面滚动出屏幕,否则调用该方法进行回弹
(3)滚动
- public boolean settleCapturedViewAt(int finalLeft, int finalTop) {
- return forceSettleCapturedViewAt(finalLeft, finalTop,
- (int) mVelocityTracker.getXVelocity(mActivePointerId),
- (int) mVelocityTracker.getYVelocity(mActivePointerId));
- }
- private boolean forceSettleCapturedViewAt(int finalLeft, int finalTop, int xvel, int yvel) {
- //当前位置
- final int startLeft = mCapturedView.getLeft();
- final int startTop = mCapturedView.getTop();
- //偏移量
- final int dx = finalLeft - startLeft;
- final int dy = finalTop - startTop;
- ...
- final int duration = computeSettleDuration(mCapturedView, dx, dy, xvel, yvel);
- //使用Scroller对象开始滚动
- mScroller.startScroll(startLeft, startTop, dx, dy, duration);
- //重置状态为滚动
- setDragState(STATE_SETTLING);
- return true;
- }
- public boolean continueSettling(boolean deferCallbacks) {
- if (mDragState == STATE_SETTLING) {
- //是否滚动结束
- boolean keepGoing = mScroller.computeScrollOffset();
- //当前滚动值
- final int x = mScroller.getCurrX();
- final int y = mScroller.getCurrY();
- //偏移量
- final int dx = x - mCapturedView.getLeft();
- final int dy = y - mCapturedView.getTop();
- //便宜操作
- if (dx != 0) {
- ViewCompat.offsetLeftAndRight(mCapturedView, dx);
- }
- if (dy != 0) {
- ViewCompat.offsetTopAndBottom(mCapturedView, dy);
- }
- //回调
- if (dx != 0 || dy != 0) {
- mCallback.onViewPositionChanged(mCapturedView, x, y, dx, dy);
- }
- //滚动结束状态
- if (!keepGoing) {
- if (deferCallbacks) {
- mParentView.post(mSetIdleRunnable);
- } else {
- setDragState(STATE_IDLE);
- }
- }
- }
- return mDragState == STATE_SETTLING;
- }
在我们的View中
- override fun computeScroll() {
- super.computeScroll()
- if (dragHelper?.continueSettling(true) == true) {
- invalidate()
- }
- }
- 以上,就是ViewDragHelper的实现原理和使用方式
- override fun computeScroll() {
- super.computeScroll()
- if (dragHelper?.continueSettling(true) == true) {
- invalidate()
- }
- }
以上,就是ViewDragHelper的实现原理和使用方式
ViewDragHelper本质上是对MotionEvent的分析及处理,并提供了一系列的监听回调方法,来帮助我们减轻开发负担,更为方便地处理控件的滑动拖拽逻辑;
是不是觉得很简单,一起加油,各位老铁们;
新闻名称:Android源码进阶之ViewDragHelper原理机制解析
本文链接:http://www.mswzjz.com/qtweb/news46/195396.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联