android textview settext卡顿深层次原因
最近在公司项目里面发现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卡顿深层次原因的更多相关文章
- android TextView SetText卡顿原因
[android TextView SetText卡顿原因] 不要用wrap_content即可. 参考:http://blog.csdn.net/self_study/article/details ...
- Android TextView setText卡顿问题
TextView 是经常使用控件之中的一个,最经常使用的方法是setText() . 可是 我们在显示大量的文本的时候,使用setText还是会有一些性能的问题. 这篇文章 关于TextView的s ...
- android中fragment卡顿的原因
首页的ViewPager有十几个Fragment,在快速切换的时候,容易产生卡顿现象. 二.分析当ViewPager切换到当前的Fragment时,Fragment会加载布局并显示内容,如果用户这时快 ...
- android中listview滑动卡顿的原因
导致Android界面滑动卡顿主要有两个原因: 1.UI线程(main)有耗时操作 2.视图渲染时间过长,导致卡顿 http://www.tuicool.com/articles/fm2IFfU
- Android 界面滑动卡顿分析与解决方案(入门)
Android 界面滑动卡顿分析与解决方案(入门) 导致Android界面滑动卡顿主要有两个原因: 1.UI线程(main)有耗时操作 2.视图渲染时间过长,导致卡顿 目前只讲第1点,第二点相对比较复 ...
- Android性能优化----卡顿优化
前言 无论是启动,内存,布局等等这些优化,最终的目的就是为了应用不卡顿.应用的体验性好坏,最直观的表现就是应用的流畅程度,用户不知道什么启动优化,内存不足,等等,应用卡顿,那么这个应用就不行,被卸载的 ...
- Android中app卡顿原因分析示例
在知乎回答了一个“为什么微博的app在iPhone比Android上流畅”的问题.后面部分是一个典型的动画卡顿的性能分析过程,因此帖在这里.有编程问题可以在这里交流.知乎链接. =========== ...
- Android App解决卡顿慢之内存抖动及内存泄漏(发现和定位)
内存抖动是指在短时间内有大量的对象被创建或者被回收的现象,内存抖动出现原因主要是频繁(很重要)在循环里创建对象(导致大量对象在短时间内被创建,由于新对象是要占用内存空间的而且是频繁,如果一次或者两次在 ...
- android中app卡顿优化问题
所谓app卡顿原因就是在运行时出现了丢帧,还可能是UI线程被阻塞.首先来一下丢帧现象,android每16ms会对界面进行一次渲染,如果app的绘制.计算等超过了16ms那么只能等下一个16ms才能 ...
随机推荐
- shell $*与$@的区别
[root@bgx shell]# cat tt.sh #! /bin/bash test() { echo "未加引号,二者相同" echo $* echo $@ echo &q ...
- python json.loads json.dumps(ensure_ascii = False) 汉字乱码问题解决
python 转换为json时候 汉字编码问题 2017年03月23日 18:50:04 阅读数:5604 有这样一个需求: 需要一个json 文件 数据从数据库里查询出来 1. 设置文件头 # -* ...
- 0_Simple__simplePrintf
在设备代码中使用函数 printf(),没有新的认识. ▶ 源代码 #include <stdio.h> #include <cuda_runtime.h> #include ...
- JSON解析工具比较,主要GSON和FastJSON
JSON解析工具比较,主要GSON和FastJSON 一 .各个JSON技术的简介和优劣 1.json-lib json-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确 ...
- Spring boot @PropertySource, @ImportResource, @Bean
@PropertySource:加载指定的配置文件 /** * 将配置文件中配置的每一个属性的值,映射到这个组件中 * @ConfigurationProperties:告诉SpringBoot将本类 ...
- Redis 安装 和 启动
Redis下载官网 http://download.redis.io/releases/ 本人下载了stable版 1:安装步骤 ># wget http://download.redis.i ...
- String类的intern()方法
0.引言 什么都先不说,先看下面这个引入的例子: String str1 = new String("SEU")+ new String("Calvin"); ...
- org.apache.cxf.interceptor.Fault: Unmarshalling Error: 意外的元素 (uri:"", local:"mixornot")。
三月 09, 2018 3:09:14 下午 org.apache.cxf.phase.PhaseInterceptorChain doDefaultLogging警告: Interceptor fo ...
- sqlserver批量导出存储过程、函数、视图
select text from syscomments s1 join sysobjects s2 on s1.id=s2.id where xtype = 'V' xtype V 视图 P ...
- Android中MD5加密
最近项目中遇到MD5加密,代码很简单,又是死代码,不过要注意当长度不足32的时候要补个0.下面是具体代码,直接拷贝就能用. public static String getMD5(String str ...