android textview settext卡顿深层次原因

最近在公司项目里面发现listview里面的textview在调用settext函数的时候非常耗时,当时都有点不敢相信,这是因为如果你把textview设置成wrap_content,则每次调用settext之后会调用到

<pre name="code" class="java">........
4033
4034            if (mMovement != null) {
4035                mMovement.initialize(this, (Spannable) text);
4036
4037                /*
4038                 * Initializing the movement method will have set the
4039                 * selection, so reset mSelectionMoved to keep that from
4040                 * interfering with the normal on-focus selection-setting.
4041                 */
4042                if (mEditor != null) mEditor.mSelectionMoved = false;
4043            }
4044        }
4045
4046        if (mLayout != null) {
4047            //这个函数非常重要
4047            checkForRelayout();
4048        }
4049
4050        sendOnTextChanged(text, 0, oldlen, textLength);
4051        onTextChanged(text, 0, oldlen, textLength);
4052
4053        notifyViewAccessibilityStateChangedIfNeeded(AccessibilityEvent.CONTENT_CHANGE_TYPE_TEXT);
4054
.........


 checkForRelayout函数,这个函数根据文字的多少重新开始布局 

6807    private void More ...checkForRelayout() {
6808        // If we have a fixed width, we can just swap in a new text layout
6809        // if the text height stays the same or if the view height is fixed.
6810
6811        if ((mLayoutParams.width != LayoutParams.WRAP_CONTENT ||
6812                (mMaxWidthMode == mMinWidthMode && mMaxWidth == mMinWidth)) &&
6813                (mHint == null || mHintLayout != null) &&
6814                (mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight() > 0)) {
6815            // Static width, so try making a new text layout.
6816
6817            int oldht = mLayout.getHeight();
6818            int want = mLayout.getWidth();
6819            int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth();
6820
6821            /*
6822             * No need to bring the text into view, since the size is not
6823             * changing (unless we do the requestLayout(), in which case it
6824             * will happen at measure).
6825             */
6826            makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING,
6827                          mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(),
6828                          false);
6829
6830            if (mEllipsize != TextUtils.TruncateAt.MARQUEE) {
6831                // In a fixed-height view, so use our new text layout.
6832                if (mLayoutParams.height != LayoutParams.WRAP_CONTENT &&
6833                    mLayoutParams.height != LayoutParams.MATCH_PARENT) {
6834                    invalidate();
6835                    return;
6836                }
6837
6838                // Dynamic height, but height has stayed the same,
6839                // so use our new text layout.
6840                if (mLayout.getHeight() == oldht &&
6841                    (mHintLayout == null || mHintLayout.getHeight() == oldht)) {
6842                    invalidate();
6843                    return;
6844                }
6845            }
6846
6847            // We lose: the height has changed and we have a dynamic height.
6848            // Request a new view layout using our new text layout.
6849            requestLayout();
6850            invalidate();
6851        } else {
6852            // Dynamic width, so we have no choice but to request a new
6853            // view layout with a new text layout.
6854            nullLayouts();
6855            requestLayout();
6856            invalidate();
6857        }
6858    }

注释已经写的很明白了,所以宽度设定为一个数字或者match_parent能够使得settext所耗时间变短,就是这样

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值