一. 对于控件,Android中的测量方式android
在Android中,控件绘制的步骤是 measure,layout,drawide
//在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(); measureChildpost
手指一动的最小距离获取应该大于ViewConfiguration.getScaledTouchSlop ()才开始滑动spa
注:ViewConfiguration注册了各类滑动和触摸的最小距离.net
通常来讲在onCreate中没法获得控件的高度,除非设定成已知数据,或者已对控件进行测量,但getWidth不必定是已知的,只能获得getMeasureWidthcode
通常来讲有以下几种获取方式orm
@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彻底被绘制出展现server
imageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { view.postDelayed(new Runnable() { @Override public 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个blog
Activity->Window->ViewGroup->...->childView[--->分发拦截阶段 Activity<-Window<-ViewGroup<-...<-childView[--->处理阶段
分发拦截阶段,dispatchTouchEvent会调用onInteceptorTouchEvent进行判断,是否继续传递,若是onInteceptorTouchEvent返回值为ture,
表示直接中止分发,把事件交由onTouchEvent进行处理;不然想tragetview传递,childview调用dispatchTouchEvent进行相似判断
处理阶段,彻底由onTouchEvent进行处理,返回值为ture,终止事件回传
注意:调用requestDisallowInterceptTouchEvent(true)时,onInteceptorTouchEvent不会被调用,详情请看源码
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