Android开发系列:TextView绘制(四)过程分析

本文解析了Android TextView组件的测量(onMeasure)、布局(onLayout)和绘制(onDraw)关键步骤,涉及MeasureSpec、Layout参数计算、BoringLayout与StaticLayout等。了解这些核心过程有助于深入理解View绘制原理。

整体流程

onMeasure -> onLayout -> onDraw

onMeasure

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
   
   
	int widthMode = MeasureSpec.getMode(widthMeasureSpec);
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int widthSize = MeasureSpec.getSize(widthMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);
    ...
    if (widthMode == MeasureSpec.EXACTLY) {
   
   
        // 精确模式,以父容器给的宽度作当前TextView的宽度.
        width = widthSize;
    } else {
   
   
        if (mLayout != null && mEllipsize == null) {
   
   
            //调用desired方法计算期望值,如果行数大于1就返回-1,否则返回单行宽度
            des = desired(mLayout);
        }
        //小于0,即行数大于1行,就去判断是否是boring
        if (des < 0) {
   
   
            boring = BoringLayout.isBoring(mTransformed, mTextPaint, mTextDir, mBoring);
            if (boring != null) {
   
   
               mBoring = boring;
            }
        } else {
   
   
            fromexisting = true;
        }
        ...
        //加上Drawable的宽度
        final Drawables dr = mDrawables;
        if (dr != null) {
   
   
            width = Math.max(width, dr.mDrawableWidthTop);
            width = Math.max(width, dr.mDrawableWidthBottom
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

KWMax

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值