阶段一: 深入了解view
时间 2022.08.31
学习view, 以及view 在项目中的使用; 总结 练习, 加复盘!!
view 与 viewGroup
view 滑动:
(1) onlayout() | offsetLeftAndRight() offsetTopAndBottom | LayoutParams | scrollTo() scrollBy() | Scroller;
(2) Scroller 源码分析;
view的事件分发机制
(1) 源码解析activity的构成:
Activity phoneWindow decroview contentView
(2) 事件分发
dispatchTouchEvent(): 事件分发
onInterceptTouchEvent(): 拦截事件
onTouchEvent(): 处理是按
Event: action_down, action_move, action_up
分发是由Activity 向 底下的view 进行传递, 如果某一部分确定消耗该Event, 则Event 不分发;
如果一直到最后一个view都不处理, 则再往上抛;
public boolean dispatchTouchEvent(MotionEvent ev) { boolean consume = false;//事件是否被消费 if (onInterceptTouchEvent(ev)){//调用onInterceptTouchEvent判断是否拦截事件 consume = onTouchEvent(ev);//如果拦截则调用自身的onTouchEvent方法 }else{ consume = child.dispatchTouchEvent(ev);//不拦截调用子View的dispatchTouchEvent方法 } return consume;//返回值表示事件是否被消费,true事件终止,false调用父View的onTouchEvent方法 }
核心理解: 判断当前view 是否拦截, 如果拦截则事件不分发, 然后确定当前view是否处理
如果不拦截, 则继续分发给子view;
所以总体看 : dispatchTouchEvent 是事件分发的起点, 具体的执行是由onInterceptTouchEvent() 和 onTouchEvent() 来实现的
可参考该文章: https://www.51cto.com/article/678248.html
知识点:
View.onTouchEvent() 方法作用: 处理屏幕触摸事件
handle touch screen motion events;
View.invalidate() 方法作用: 导致view 重绘
Invalidate the whole view. If the view is visible, onDraw(Canvas) will be called at some point in the future.
View.computeScroll() 方法作用:
Called by a parent to request that a child update its values for mScrollX and mScrollY if necessary.
This will typically be done if the child is animating a scroll using a Scroller object.
Scroller. computeScrollOffset() 方法作用: 计算最新位置
Call this when you want to know the new location. If it returns true, the animation is not yet finished