第四章 view的工作原理(下)(重点)

1.自定义view的流程

  1. 自定义view的属性
    在values的文件夹下创建attrs.xml,添加自定义属性
    <attr name="titleText" format="string" />
    <attr name="titleTextSize" format="dimension" />
    <attr name="titleTextColor" format="color" />
    <attr name="image" format="reference" />
    <attr name="imageScaleType">
        <enum name="fillXY" value="0" />
        <enum name="center" value="1" />
    </attr>

    <declare-styleable name="CustomImageView">
        <attr name="titleText" />
        <attr name="titleTextSize" />
        <attr name="titleTextColor" />
        <attr name="image" />
        <attr name="imageScaleType" />
    </declare-styleable>

其中要用到name="CustomImageView",其他属性分别有名字和类型,类型包括了string, color, demension, integer, enum, reference, float, boolean, fraction, flag 等。

  1. 使用自定义属性
    在父viewGroup添加xmlns:app="http://schemas.android.com/apk/res-auto"
    使用app的自定义名称设置属性
<customview.IconTitleView
        android:id="@+id/iconTitleView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:image="@mipmap/fr_icon_search_normal"
        app:imageScaleType="center"
        app:titleText="自定义"
        app:titleTextColor="@color/colorAccent"
        app:titleTextSize="10sp"
        android:padding="10dp"/>

3.自定义view,在构造函数中接收属性,并设置。

private void init(Context context, AttributeSet attrs, int defStyleAttr){
        
       //接收属性
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomImageView);
        titleColor = a.getColor(R.styleable.CustomImageView_titleTextColor, Color.BLUE);
        titleSize = a.getDimensionPixelSize(R.styleable.CustomImageView_titleTextSize,16);
        titleText = a.getString(R.styleable.CustomImageView_titleText);
        img = BitmapFactory.decodeResource(getResources(),a.getResourceId(R.styleable.CustomImageView_image,0));
        imageScaleType = a.getInt(R.styleable.CustomImageView_imageScaleType,0);
        
        a.recycle();
        rect = new Rect();
        paint = new Paint();
        textBound = new Rect();
        paint.setTextSize(titleSize);
        //计算文字的范围
        paint.getTextBounds(titleText,0,titleText.length(),textBound);
    }

重写onMeasure(重要)

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int widthSpec = MeasureSpec.getSize(widthMeasureSpec);
        int heightSpec = MeasureSpec.getSize(heightMeasureSpec);
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        //wrap
        if(widthMode == MeasureSpec.AT_MOST){
            mWidth = Math.min(Math.max(img.getWidth(),textBound.width()) +getPaddingRight() + getPaddingLeft(),widthSpec);
        }else {
            mWidth = widthSpec;
        }

        if(heightMode == MeasureSpec.AT_MOST){
            mHeight = Math.min(getPaddingTop() + getPaddingBottom() + textBound.height() + img.getHeight(),heightSpec);
        }else{
            mHeight = heightSpec;
        }

        setMeasuredDimension(mWidth,mHeight);
    }

获得onMeasure的width和height的具体 值和mode模式,mode模式有三种:

MeasureSpec.AT_MOST   //,父容器有一个可用大小的值,view不能大于这个值通常是wrap_parent
MeasureSpec.EXACTLY    //父容器已经检测出所需大小,通常是match_parent和精确值
MeasureSpec.UNSPECIFIC
 //父容器不对view有限制,想多大多大(比如HorizontalScrollView下的WeatherView)

计算的时候要考虑padding
重写onDraw进行绘制

onDraw()
2 .onMeasure中的一些原则

MeasureSpec代表32位int值,高2位是SpecMode,低30位是SpecSize。
--mode的值是由view的LayoutParams和父view的mode决定的。
通常来说wrap_content是最大模式,其他是精确模式,不过也不一定。
具体遵循下面的原则:

childLayoutParams / parentSpecMode Exactly At_most unspecified
dp/px Exactly(childSize) Exactly(childSize) Exactly(childSize)
match Exactly(parentSize) At_most(parentSize) unspecific(0)
wrap At_most(parentSize) At_most(parentSize) unspecific(0)

注意:自定义view使用wrap时,效果通常是match,因为是parentSize,需要重新onMeasure给一个值。父view是HorizontalScrollView,所以子控件0。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容