`

Android性能优化系列---Improving Layout Performance(四)

阅读更多

 

       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);

 

          那么我们就可以直接访问里面的数据了,省去了重复查询,提升了性能。

分享到:
评论

相关推荐

    Android代码-android性能优化视频、文档和工具收集

    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...

    Android代码-android-priority-jobqueue

    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.

    improving-network-performance-in-multi-core-systems-paper

    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 ...

    Improving the Lightning Performance

    该标准文档的标题“Improving the Lightning Performance”(提升雷电性能)主要指通过一系列技术和方法来提高电力系统中架空配电线路对于雷电冲击的抵抗能力。在电力行业中,“雷电性能”的提升通常涉及到对现有...

    Android代码-ChatMessagesAdapter-android

    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 ...

    921164-MB51-Improving the runtime using database

    "921164-MB51-Improving the runtime using database"这个主题主要关注通过优化数据库来提高MB51和类似事务如MMBE的运行性能。 首先,了解数据库优化的重要性是关键。在SAP系统中,大部分业务流程都需要与数据库...

    Improving Lookup Performance over a widely-deployed DHT

    本文《Improving Lookup Performance over a widely-deployed DHT》聚焦于Kademlia协议,一种广泛部署的DHT,旨在探讨并优化其查找性能。 ### 核心知识点 #### 1. DHT与Kademlia简介 DHT是一种分布式数据存储机制...

    High Performance in-memory computing with Apache Ignite.pdf

    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. ...

    藏经阁-Improving Python and Spark Performance and Interoperability.

    藏经阁-Improving Python and Spark Performance and Interoperability 在本篇文章中,我们将讨论如何提高 Python 和 Spark 的性能和互操作性。Spark 是一个基于 Scala 编写的分布式计算框架,运行在 Java 虚拟机...

    2020-自校正网络20cvpr-SCNet-Improving Convolutional Networks with Sel

    然而,"2020-自校正网络20cvpr-SCNet-Improving Convolutional Networks with Self-Calibrated Convolutions"这篇论文提出了一种新的思路,即不通过调整网络架构,而是改进CNN的基础卷积特征转换过程来提升性能。...

    Improving WS-Security Performance with a Template-Based Approach

    ### 基于模板的方法提升WS-Security性能 #### 概述 随着Web服务在全球范围内的广泛应用,确保这些服务的安全性和可靠性的需求日益增长。为了满足这一需求,一系列与Web服务安全相关的规范被提出,其中最核心的是WS...

    Improving the Performance of an IP-over-P2P

    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 ...

    Chapter 5 - Improving Managed Code Performance.pdf

    在.NET平台上,托管代码性能优化是一个重要课题,微软官方文档提供了一系列的设计和编程技巧来帮助开发者提升其.NET应用程序的性能。本章的目的是提供一系列优化和改进托管代码性能的技术,指导开发者编写可以充分...

    Optimizing Java_Practical Techniques for Improving JVM Application Performance

    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, ...

    Android代码-cloudopt-next

    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 ...

    藏经阁-Improving Python and Spark Per.pdf

    藏经阁-Improving Python and Spark Performance and Interoperability with Apache Arrow 本篇文章介绍了 PySpark UDF(User Defined Functions)的当前状态和限制,并讨论了 Apache Arrow 如何改进 PySpark UDF ...

    Android代码-FastTextView

    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 ...

Global site tag (gtag.js) - Google Analytics