Android(安卓)View绘制过程以及事件传递原理
2019独角兽企业重金招聘Python工程师标准>>>
一. 对于控件,Android中的测量方式
在Android中,控件绘制的步骤是 measure,layout,draw
//在onMeasure调用之前调用的测量方式private void measureView(View child) { ViewGroup.LayoutParams p = child.getLayoutParams(); if (p == null) { p = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT); } //根据父布局获得child宽度的建议大小 int childWidthSpec = ViewGroup.getChildMeasureSpec(0, mListPadding.left + mListPadding.right, p.width); //高度不是固定的,可能比父布局高,因此不应该调动ViewGroup.getChildMeasureSpec int lpHeight = p.height; int childHeightSpec; if (lpHeight > 0) { childHeightSpec = MeasureSpec.makeMeasureSpec(lpHeight, MeasureSpec.EXACTLY); //精确高度 } else { childHeightSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED); //要多大有多大 } child.measure(childWidthSpec, childHeightSpec);}
一般来说,如果父布局的宽度和高度,内外边距位指定,那么,上面的方法可以简化如下
private void measureView(View child){ child.measure(0, 0);}
在android开发中,常用的测量方法还有 measureChildren(); measureChild
二.对于滑动事件
手指一动的最小距离获取应该大于ViewConfiguration.getScaledTouchSlop ()才开始滑动
注:ViewConfiguration注册了各种滑动和触摸的最小距离
三.宽度和高度的获取
一般来说在onCreate中无法得到控件的高度,除非设定成已知数据,或者已对控件进行测量,但getWidth不一定是已知的,只能得到getMeasureWidth
一般来说有如下几种获取方式
@Override public void onWindowFocusChanged(boolean hasFocus) { View iv1 = findViewById(R.id.iv1);View iv2=findViewById(R.id.iv2);String msg1="iv1' width:"+iv1.getWidth()+" height:"+iv1.getHeight()+" measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+" measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();i("onWindowFocusChanged() "+msg1);i("onWindowFocusChanged() "+msg2); super.onWindowFocusChanged(hasFocus); }
或者如下,imageView完全被绘制出展示
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {@Overridepublic void onGlobalLayout() {view.postDelayed(new Runnable() {@Overridepublic void run() {View iv1 = findViewById(R.id.iv1);View iv2=findViewById(R.id.iv2);String msg1="iv1' width:"+iv1.getWidth()+" height:"+iv1.getHeight()+" measuredWidth:"+iv1.getMeasuredWidth()+"measuredHeight:"+iv1.getMeasuredHeight();String msg2="iv2' width:"+iv2.getWidth()+" height:"+iv2.getHeight()+" measuredWidth:"+iv2.getMeasuredWidth()+"measuredHeight:"+iv2.getMeasuredHeight();i("onWindowFocusChanged() "+msg1);i("onWindowFocusChanged() "+msg2);}}, 300);}});
四.事件的分发传递
Android中对于事件的操作,主要集中于3个方法,dispatchTouchEvent, onInteceptorTouchEvent(ViewGroup独有), onTouchEvent,其中,传递方向有2个
Activity->Window->ViewGroup->...->childView[--->分发拦截阶段Activity<-Window<-ViewGroup<-...<-childView[--->处理阶段
分发拦截阶段,dispatchTouchEvent会调用onInteceptorTouchEvent进行判断,是否继续传递,如果onInteceptorTouchEvent返回值为ture,
表示直接停止分发,把事件交由onTouchEvent进行处理;否则想tragetview传递,childview调用dispatchTouchEvent进行类似判断
处理阶段,完全由onTouchEvent进行处理,返回值为ture,终止事件回传
注意:调用requestDisallowInterceptTouchEvent(true)时,onInteceptorTouchEvent不会被调用,详情请看源码
五.View的绘制
DecorView为顶级View,DecoreView分为2部分,第一部分是Title,第二部分是Content,对应的ID分别为R.Android.id.title和R.android.id.content
view 的绘制流程从 ViewRoot 的 performTraversals 开始,代码流程是这样的:
performMeasure -> measure -> onMeasure
performLayout -> layout -> onLayout
performDraw -> draw -> onDraw
View内部的绘制流程
view的绘制大致遵循如下流程:先绘制背景,再绘制自己(onDraw),接着绘制子元素(dispatchDraw),最后绘制一些装饰等比如滚动条(onDrawScrollBars)
参考:http://blog.csdn.net/singwhatiwanna/article/details/42614953
转载于:https://my.oschina.net/ososchina/blog/390402
更多相关文章
- android MediaPlayer 架构介绍
- Unity3D游戏引擎实现在Android中打开WebView的实例
- Android(安卓)MediaPlayer
- Android+JNI调用–文件操作
- android 画饼图 Canvas drawArc
- android客户端程序访问服务器端webservice,几篇不错的文章!
- Android(安卓)CountDownTimer的使用
- android IOS在WebView 和 JS 交互对比
- Unity3D 调用Android原生方法