UI阻塞及其优化
2015-11-17 14:30
246 查看
There are simply two rules to Android's single thread model :安卓单线程模型两个基本原则
1.Don't block the UI thread 不要阻塞UI线程
2.Don't access the Android UI toolkit from outside the UI thread
不要在UI线程之外的其他线程中,对视图当中的组件进行设置
否则会报经典错误
Only the original thread that created a view hierarchy can touch its views只有创建View的那个线程才能对其进行修改
官方提供了两个解决方案
解决方法1:
建立一个线程,并调用view.post()方法调用视图中的组件,
线程ID,是操作系统识别线程的唯一标志。
在view.post中打印线程和主线程id一样都是1,如图所示,view.post方法会给任务队列中加入一个任务,Runable对象
UI主线程会检测任务队列中是否有任务需要完成,所以通过调用view.post()方法,可以解决建立新线程修改view组件
但是可读性差,可维护性差
解决方法2:Asynctask
1.Don't block the UI thread 不要阻塞UI线程
2.Don't access the Android UI toolkit from outside the UI thread
不要在UI线程之外的其他线程中,对视图当中的组件进行设置
否则会报经典错误
Only the original thread that created a view hierarchy can touch its views只有创建View的那个线程才能对其进行修改
官方提供了两个解决方案
解决方法1:
建立一个线程,并调用view.post()方法调用视图中的组件,
线程ID,是操作系统识别线程的唯一标志。
在view.post中打印线程和主线程id一样都是1,如图所示,view.post方法会给任务队列中加入一个任务,Runable对象
UI主线程会检测任务队列中是否有任务需要完成,所以通过调用view.post()方法,可以解决建立新线程修改view组件
但是可读性差,可维护性差
解决方法2:Asynctask
相关文章推荐
- UIlable 适应字体大小
- rpmbuild spec文件的注释问题
- SpannableStringBuilder的妙用
- Domain=com.alamofire.error.serialization.response Code=-1016 "Request failed: unacceptable content-type: text/html"
- UITabBarController的简单使用和属性方法总结
- 界面干货盘点 | 海量实用UI工具及示例等你来体验!
- easyui超链接中使用session值
- Android开发中关于UI图片资源的问题
- EasyUI DataGrid View
- LeetCode(232) Implement Queue using Stacks
- LeetCode(232) Implement Queue using Stacks
- 400 Bad Request
- 用SpriteBuilder简化"耕牛遍地走"的动画效果(三)
- 用SpriteBuilder简化"耕牛遍地走"的动画效果(三)
- 用SpriteBuilder简化"耕牛遍地走"的动画效果(三)
- 用SpriteBuilder简化"耕牛遍地走"的动画效果(二)
- 用SpriteBuilder简化"耕牛遍地走"的动画效果(二)
- 用SpriteBuilder简化"耕牛遍地走"的动画效果(二)
- 用SpriteBuilder简化"耕牛遍地走"的动画效果(一)
- 用SpriteBuilder简化"耕牛遍地走"的动画效果(一)