android settext 速度,android textview settext卡顿深层次原因

本文深入解析了Android中TextView的setText过程,特别是checkForRelayout函数的作用。当文本内容变化时,该函数会根据文字数量决定是否需要重新布局。如果宽度固定或高度不变,可以快速生成新布局,减少setText的时间消耗。对于动态宽度的情况,必须请求新的布局以适应文本变化。
摘要由CSDN通过智能技术生成

........40334034 if (mMovement != null) {4035 mMovement.initialize(this, (Spannable) text);40364037 /*4038 * Initializing the movement method will have set the4039 * selection, so reset mSelectionMoved to keep that from4040 * interfering with the normal on-focus selection-setting.4041 */4042 if (mEditor != null) mEditor.mSelectionMoved = false;4043 }4044 }40454046 if (mLayout != null) {

//这个函数非常重要4047 checkForRelayout();4048 }40494050 sendOnTextChanged(text, 0, oldlen, textLength);4051 onTextChanged(text, 0, oldlen, textLength);40524053 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 layout6809 // if the text height stays the same or if the view height is fixed.68106811 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.68166817 int oldht = mLayout.getHeight();6818 int want = mLayout.getWidth();6819 int hintWant = mHintLayout == null ? 0 : mHintLayout.getWidth();68206821 /*6822 * No need to bring the text into view, since the size is not6823 * changing (unless we do the requestLayout(), in which case it6824 * will happen at measure).6825 */6826 makeNewLayout(want, hintWant, UNKNOWN_BORING, UNKNOWN_BORING,6827 mRight - mLeft - getCompoundPaddingLeft() - getCompoundPaddingRight(),6828 false);68296830 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 }68376838 // 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 }68466847 // 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 new6853 // view layout with a new text layout.6854 nullLayouts();6855 requestLayout();6856 invalidate();6857 }6858 }

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值