最近在公司项目里面发现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. 0_Simple__simplePrintf

    在设备代码中使用函数 printf(),没有新的认识. ▶ 源代码 #include <stdio.h> #include <cuda_runtime.h> #include ...

  2. CUDA C Programming Guide 在线教程学习笔记 Part 1

    1. 简介 2. 编程模型 ▶ SM version 指的是硬件构架和特性,CUDA version 指的是软件平台版本. 3. 编程接口.参考 http://chenrudan.github.io/ ...

  3. three添加和移除对象

    创建场景在第一章的地方就讲过怎么样创建一个最基本的场景,这里不重复了html:部分 <!doctype html><html lang="en"><h ...

  4. 楼层验证B1~1F等

    $("#mapManagerEditForm").bootstrapValidator("addField","floors",{ vali ...

  5. linux的基础

    vmtools 的安装:作用 ——>共享linux和windows的文件夹 reboot 重启 远程登录 : 远程登录时要打开sshd(监视22端口) 怎么设施sshd: 打开远端,输入setu ...

  6. xe7 c++builder 日期时间头文件函数大全 date

    c++builde r时间日期函数大全,在头文件System.DateUtils.hpp,不过没有IncMonth,因为这个函数定义在System.SysUtils.hpp里头了,唉 date,dat ...

  7. menu 一组 只能选择一个

    menu 一组 只能选择一个 将属性表的 groupindex 给一个值,假设你有三个Lang选项 就把这三个 manuitemex.groupindex 都设成同一个编号 (ex: 10) 以及把 ...

  8. delphi c++builder JSON 生成与解析 例子

    json,System.JSON,REST.JSON JSON有两种数据结构,对象和数组. 对象在js中表示为“{}”括起来的内容,数据结构为 {key:value,key:value,...} 数组 ...

  9. chrome 常用插件下载安装

    可在google的应用商店进行下载:chrome://apps/ 但大多时间无法链接. 国内插件下载地址: http://www.cnplugins.com http://chromecj.com/ ...

  10. UI5-文档-4.30-Debugging Tools

    虽然我们在前面的步骤中添加了一个基本的测试覆盖率,但是我们似乎不小心破坏了我们的应用程序,因为它不再显示价格到我们的发票上.我们需要调试这个问题,并在有人发现之前修复它. 幸运的是,SAPUI5提供了 ...