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

........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所耗时间变短,就是这样。

android textview settext卡顿深层次原因的更多相关文章

  1. android TextView SetText卡顿原因

    [android TextView SetText卡顿原因] 不要用wrap_content即可. 参考:http://blog.csdn.net/self_study/article/details ...

  2. Android TextView setText卡顿问题

    TextView 是经常使用控件之中的一个,最经常使用的方法是setText()  . 可是 我们在显示大量的文本的时候,使用setText还是会有一些性能的问题. 这篇文章 关于TextView的s ...

  3. android中fragment卡顿的原因

    首页的ViewPager有十几个Fragment,在快速切换的时候,容易产生卡顿现象. 二.分析当ViewPager切换到当前的Fragment时,Fragment会加载布局并显示内容,如果用户这时快 ...

  4. android中listview滑动卡顿的原因

    导致Android界面滑动卡顿主要有两个原因: 1.UI线程(main)有耗时操作 2.视图渲染时间过长,导致卡顿 http://www.tuicool.com/articles/fm2IFfU 

  5. Android 界面滑动卡顿分析与解决方案(入门)

    Android 界面滑动卡顿分析与解决方案(入门) 导致Android界面滑动卡顿主要有两个原因: 1.UI线程(main)有耗时操作 2.视图渲染时间过长,导致卡顿 目前只讲第1点,第二点相对比较复 ...

  6. Android性能优化----卡顿优化

    前言 无论是启动,内存,布局等等这些优化,最终的目的就是为了应用不卡顿.应用的体验性好坏,最直观的表现就是应用的流畅程度,用户不知道什么启动优化,内存不足,等等,应用卡顿,那么这个应用就不行,被卸载的 ...

  7. Android中app卡顿原因分析示例

    在知乎回答了一个“为什么微博的app在iPhone比Android上流畅”的问题.后面部分是一个典型的动画卡顿的性能分析过程,因此帖在这里.有编程问题可以在这里交流.知乎链接. =========== ...

  8. Android App解决卡顿慢之内存抖动及内存泄漏(发现和定位)

    内存抖动是指在短时间内有大量的对象被创建或者被回收的现象,内存抖动出现原因主要是频繁(很重要)在循环里创建对象(导致大量对象在短时间内被创建,由于新对象是要占用内存空间的而且是频繁,如果一次或者两次在 ...

  9. android中app卡顿优化问题

     所谓app卡顿原因就是在运行时出现了丢帧,还可能是UI线程被阻塞.首先来一下丢帧现象,android每16ms会对界面进行一次渲染,如果app的绘制.计算等超过了16ms那么只能等下一个16ms才能 ...

随机推荐

  1. C_point指针

    1,关于C语言中变量的访问方式,直接访问[使用变量名直接引用,操作变量进行赋值,改变变量值等操作],间接访问--指针,一种指向变量飞, 程序对变如量的读写操作,实际是对变量所在的存储空间进行写入和取出 ...

  2. C常用问题

    linux系统,gcc编译器包含引用的头文件位置

  3. leetcode27

    public class Solution { public int RemoveElement(int[] nums, int val) { var len = nums.Length; ; ; i ...

  4. leetcode268

    public class Solution { public int MissingNumber(int[] nums) { var list = nums.OrderBy(x => x).To ...

  5. thymeleaf的使用

    1.导包 <dependency> <groupId>org.springframework.boot</groupId> <artifactId>sp ...

  6. this指针的调整

    我们先来看一段代码: #include <iostream> using namespace std; class A { public: int a; A( ) { printf(&qu ...

  7. css 设置元素背景为透明

    要设置某一元素的背景为透明,在 chrome .firefox.opera 下是这样的: rgba 中的最后一个参数 0.4 就是想要的透明度,范围在0-1之间. 在 ie 中一般是这样的: filt ...

  8. SVM总结(转)

    支持向量机 看了JULY君的博客和文档后,个人对SVM的理解和总结,欢迎交流和指正.其理论部分可以查看下面文档链接,通俗易懂. 支持向量机通俗导论(理解SVM的三层境界)     第一篇:从四个关键词 ...

  9. t检验,T Test (Student’s T-Test)

    1.什么是T test? t-test:比较数据的均值,告诉你这两者之间是否相同,并给出这种不同的显著性(即是否是因为偶然导致的不同) The t test (also called Student’ ...

  10. linux 配置Tomcat开机启动

    一台安装有tomcat的linux服务器 方法/步骤   1 请自行下载安装配置tomcat的服务器环境 本经验仅仅介绍如何配置tomcat的开机自动启动 2 切换到tomcat/bin目录下 用vi ...