zoukankan      html  css  js  c++  java
  • Android onMeasure(widthSpec, heightSpec)


    每个View中都有一个测量绘制控件大小的方法onMeasure(int widthMeasureSpec, int heightMeasureSpec) ,该方法用于初始化控件所占的区域:

    摘自SlidingDrawer.java,仅作参考说明onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法

    @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            //onMeasure方法传入的两个参数widthMeasureSpec, heightMeasureSpec,其单位为dp或dip
        
            int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);    //这里判断单位是dp或dip,有或者其他的单位类型
            int widthSpecSize =  MeasureSpec.getSize(widthMeasureSpec); //将单位转为px
    
            int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);
            int heightSpecSize =  MeasureSpec.getSize(heightMeasureSpec);
    
            if (widthSpecMode == MeasureSpec.UNSPECIFIED || heightSpecMode == MeasureSpec.UNSPECIFIED) {
                throw new RuntimeException("SlidingDrawer cannot have UNSPECIFIED dimensions");
            }
    
            //
            final View handle = mHandle;
            measureChild(handle, widthMeasureSpec, heightMeasureSpec);
    
            if (mVertical) {
                int height = heightSpecSize - handle.getMeasuredHeight() - mTopOffset;
                mContent.measure(MeasureSpec.makeMeasureSpec(widthSpecSize, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY));
            } else {
                int width = widthSpecSize - handle.getMeasuredWidth() - mTopOffset;
                mContent.measure(MeasureSpec.makeMeasureSpec(width, MeasureSpec.EXACTLY),
                        MeasureSpec.makeMeasureSpec(heightSpecSize, MeasureSpec.EXACTLY));
            }
    
            //最后绘制父控件的大小
            setMeasuredDimension(widthSpecSize, heightSpecSize);
            //super.onMeasure(widthMeasureSpec, heightMeasureSpec)原型则是下面这样写的:
            //setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
            //        getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
        }


       

  • 相关阅读:
    6、linux中同步、互斥、阻塞(原子操作、信号量、阻塞)
    lightOJ-1199 Partitioning Game (SG函数)
    HDU-1013 Digital Roots
    HDU-1004 Let the Balloon Rise (STL map)
    HDU-1020 Encoding (字符串)
    POJ-2524 Ubiquitous Religions (并查集)
    POJ-1988 Cube Stacking (带权并查集)
    POJ-2236 Wireless Network (并查集)
    HDU-1002 A + B Problem II (模拟大数相加)
    HDU-1829 A Bug's Life (种类并查集)
  • 原文地址:https://www.cnblogs.com/rfheh/p/4164495.html
Copyright © 2011-2022 走看看