Making ListView Scrolling Smooth
让ListView平滑滚动的关键在于将程序的主线程(UI线程)从大量的处理中解脱出来。要要保证用单独的线程来进行磁盘,网络或SQL操作。想要测试你的程序的状态, 你可以开启StrictMode。
Use a Background Thread
使用后台线程(“工作线程”)可移除主线程中的压力,这样可以使得UI线程可以专注于描绘UI。大多数时候,AsycnTask实现了一种简单的把需要做的事情与main thread分离的方法。AsyncTask自动将所有execute()请求排成队列并按顺序执行他们。这种行为对一个特定进程来说是全局性的,这意味着你不必担心创建自己的线程池。
下方所示的简单代码中,利用AsyncTask在后台线程中加载图像,然后一旦完成便更新UI。当图片正在加载时可以显示一个Spinner进度条来代替正在加载的图像。
// Using an AsyncTask to load the slow images in a background thread
new AsyncTask<ViewHolder, Void, Bitmap>() {
private ViewHolder v;
@Override
protected Bitmap doInBackground(ViewHolder... params) {
v = params[0];
return mFakeImageLoader.getImage();
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
if (v.position == position) {
// If this item hasn't been recycled already, hide the
// progress and set and show the image
v.progress.setVisibility(View.GONE);
v.icon.setVisibility(View.VISIBLE);
v.icon.setImageBitmap(result);
}
}
}.execute(holder);
从 Android 3.0 (API level 11)开始,对于AsyncTask有个新特性:在多核处理器的情况下,我们可以使用executeOnExecutor() 来替代execute(),这样系统会根据当前设备的内核数量同时执行多个任务。
Hold View Objects in a View Holder
你在滑动ListView时可能需要频繁地调用findViewById(),这样会降低性能。尽管Adapter会因为循环机制返回一个创建好的View。你仍然需要查找到这些组件并更新它,避免这样的重复,可使用“ViewHolder”设计模式。
一个ViewHolder对象缓存Layout里的每一个组件视图。因此你能立即的访问这些视图而不需要重复查询他们。首页,你需要产生一个ViewHolder类,如下:
static class ViewHolder {
TextView text;
TextView timestamp;
ImageView icon;
ProgressBar progress;
int position;
}
这样之后,我们可以填充这个ViewHolder,并且保存到tag属性里。
ViewHolder holder = new ViewHolder();
holder.icon = (ImageView) convertView.findViewById(R.id.listitem_image);
holder.text = (TextView) convertView.findViewById(R.id.listitem_text);
holder.timestamp = (TextView) convertView.findViewById(R.id.listitem_timestamp);
holder.progress = (ProgressBar) convertView.findViewById(R.id.progress_spinner);
convertView.setTag(holder);
那么我们就可以直接访问里面的数据了,省去了重复查询,提升了性能。
相关推荐
awesome-android-performance This is a list of awesome Android tutorials, videos and tools for performance optimization View Google Official Videos Double Layout Taxation Android Performance Patterns...
Good-Habits-for-Great-Coding-Improving-Programming-Skills-with-Examples-in-Python.pdf
Priority Job Queue is an implementation of a Job Queue specifically written for Android to easily schedule jobs (tasks) that run in the background, improving UX and application stability.
Today’s resource-intensive applications running on Intel® multi-core processor-based platforms are driving the need not only for greater networking bandwidth, but also for more efficient processing ...
ChatMessagesAdapter for Android QuickBlox simple to use UI library for showing quickblox chat messages inside android application. Features Ready-to-go QBChatMessage view adapter with a set of view ...
IEEE Guide for Improving the Lightning Performance of Electric Power Overhead Distribution Lines
用基于模板的方法改善Web服务安全性能 Improving WS-Security Performance with a Template-Based Approach
持续集成 jolt大奖图书 提高软件质量 降低风险
How to improving Android Bootup Time. Android boot sequence. Measuring bootup time. Problem areas. Ideas for improvements.
Sharing Spark RDD states between different Spark applications for improving performance. Processing events & streaming data, integrate Apache Ignite with other frameworks like Storm, Camel, etc. ...
IEEE Std 1243-1997 Guide for Improving the Lightning Performance of Transmission Lines
Improve your coding skills and learn how to write readable code. Rather than teach basic programming, this book presumes that readers understand the fundamentals, and offers time-honed best practices ...
2020-自校正网络20cvpr-SCNet-Improving Convolutional Networks with Self-Calibrated Convolutions1
921164-MB51-Improving the runtime using database 提升MB51/MMBE等事务的查询性能 包含note 和 安装文件
藏经阁-Improving Python and Spark Performance and Interoperability
Improving routing performance when several routing protocols are used sequentially in a WSN
The investigation produced detailed measurements in order to profile the application and help on improving the performance. Code analysis shows that the current IPOP version suffers from ...
Optimizing Java_Practical Techniques for Improving JVM Application Performance-O’Reilly(2018) How do you define performance? Most developers, when asked about the performance of their application, ...
Radically reducing development time and improving development experience. Cloudopt Next absorbed the ideas of Spring Boot,JFinal,Resty,Vertx and other excellent projects, and not only had a very ...
https://engineering.instagram.com/improving-comment-rendering-on-android-a77d5db3d82e http://ragnraok.github.io/textview-pre-render-research.html Features 1.Faster than Android TextView 2.More ...