September 17th, 2009 by Tom van Zummeren

|

And I’m back!
Reporting live on the glorious adventures in the exciting world of
Android. This blog post is the second one in the Android series. This
time with code samples! Yeah!

In my first blog post about Android I talked about setting up a project using Android. This time I want to discuss a more “advanced” topic: ListView
performance. A ListView is a view component which allows you to display
and scroll through a list of items. It can display simple text in each
row, but is also able to display a more complicated structure. In the
latter case you will need to make sure your ListView still performs well
(read: renders fast and scrolls smoothly). I am going to provide
solutions to a few different performance problems when using a ListView

The ListAdapter

If you want to use a ListView, you will have to supply it with a ListAdapter to allow it to display any content. A few simple implementations of that adapter are already available in the SDK:

These implementations are perfect for displaying very simple lists.
But if your list is just a little more complicated than that, you will
need to write your own custom ListAdapter implementation. In most cases it’s useful to subclass ArrayAdapter
which already takes care of managing a list of objects. Now you only
have to tell it how to render each object in the list. Do this by
overriding the getView(int, View, ViewGroup) method of the ArrayAdapter class.

A simple example

To give you a simple example of a case in which you need to write your own ListAdapter: displaying a list of images with some text next to it.


Example of a ListView containing Youtube search results in the form of images and text

The images need to be on-the-fly downloaded from the internet. Let’s create a class which represents items in the list:

public class ImageAndText {
private String imageUrl;
private String text; public ImageAndText(String imageUrl, String text) {
this.imageUrl = imageUrl;
this.text = text;
}
public String getImageUrl() {
return imageUrl;
}
public String getText() {
return text;
}
}

Now, let’s create an implementation of a ListAdapter that is able to display a list of these ImageAndTexts.

public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {

    public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts) {
super(activity, 0, imageAndTexts);
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext();
LayoutInflater inflater = activity.getLayoutInflater(); // Inflate the views from XML
View rowView = inflater.inflate(R.layout.image_and_text_row, null);
ImageAndText imageAndText = getItem(position); // Load the image and set it on the ImageView
ImageView imageView = (ImageView) rowView.findViewById(R.id.image);
imageView.setImageDrawable(loadImageFromUrl(imageAndText.getImageUrl())); // Set the text on the TextView
TextView textView = (TextView) rowView.findViewById(R.id.text);
textView.setText(imageAndText.getText()); return rowView;
} public static Drawable loadImageFromUrl(String url) {
InputStream inputStream;
try {
inputStream = new URL(url).openStream();
} catch (IOException e) {
throw new RuntimeException(e);
}
return Drawable.createFromStream(inputStream, "src");
}
}

The views are inflated from an XML file called “image_and_text_row.xml”:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"> <ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/default_image"/> <TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </LinearLayout>

This ListAdapter implementation renders the ImageAndTexts in the ListView like you would expect. The only thing is that this only works for a very small list which doesn’t require scrolling to see all items. If the list of ImageAndTexts gets bigger you will notice that scrolling isn’t as smooth as it should be (in fact, it’s far off!).

Improving performance

The biggest bottleneck in the above example is the fact that the images have to be downloaded from the internet. Because we execute all our code in the same thread as the UI, the UI will get stuck each time an image is being downloaded. If you run the same application using a 3G internet connection instead of WiFi, the performance will even be worse.

To avoid this we want the image to be loaded in a separate thread to not disturb the UI thread too much. To make this happen, we could use an AsyncTask which is designed for cases like this. But in practice, you will notice that the AsyncTask is limited to 10 threads. This number is hardcoded somewhere in the Android SDK so we cannot change this. In this case it’s a limitation we cannot live with, because often more than 10 images are loaded at the same time.

AsyncImageLoader

An alternative is to manually spawn a new Thread for each image. In addition we should use Handlers to deliver the downloaded images to the UI thread. We want to do this because only from the UI thread you are allowed to modify the UI (read: draw an image on the screen). I created a class called AsyncImageLoader which takes care of loading images using Threads and Handlers like I just described. Also it caches images to avoid a single image to be downloaded multiple times.

public class AsyncImageLoader {
private HashMap<String, SoftReference<Drawable>> imageCache; public AsyncImageLoader() {
drawableMap = new HashMap<String, SoftReference<Drawable>>();
} public Drawable loadDrawable(final String imageUrl, final ImageCallback imageCallback) {
if (drawableMap.containsKey(imageUrl)) {
SoftReference<Drawable> softReference = imageCache.get(imageUrl);
Drawable drawable = softReference.get();
if (drawable != null) {
return drawable;
}
}
final Handler handler = new Handler() {
@Override
public void handleMessage(Message message) {
imageCallback.imageLoaded((Drawable) message.obj, imageUrl);
}
};
new Thread() {
@Override
public void run() {
Drawable drawable = loadImageFromUrl(imageUrl);
imageCache.put(imageUrl, new SoftReference<Drawable>(drawable));
Message message = handler.obtainMessage(0, drawable);
handler.sendMessage(message);
}
}.start();
return null;
} public static Drawable loadImageFromUrl(String url) {
// ...
} public interface ImageCallback {
public void imageLoaded(Drawable imageDrawable, String imageUrl);
}
}

Notice that I used a SoftReference for caching images, to allow the garbage collector to clean the images from the cache when needed. How it works:

  • Call loadDrawable(imageUrl, imageCallback) providing an anonymous implementation of the ImageCallback interface
  • If the image doesn’t exist in the cache yet, the image is downloaded in a separate thread and the ImageCallback is called as soon as the download is complete.
  • If the image DOES exist in the cache, it is immediately returned and the ImageCallback is never called.

Only one instance of AsyncImageLoader should exist in your application, or else the caching won’t work. If we take the example ImageAndTextListAdapter class we can now replace:

ImageView imageView = (ImageView) rowView.findViewById(R.id.image);
imageView.setImageDrawable(loadImageFromUrl(imageAndText.getImageUrl()));

with:

final ImageView imageView = (ImageView) rowView.findViewById(R.id.image);
Drawable cachedImage = asyncImageLoader.loadDrawable(imageAndText.getImageUrl(), new ImageCallback() {
public void imageLoaded(Drawable imageDrawable, String imageUrl) {
imageView.setImageDrawable(imageDrawable);
}
});
imageView.setImageDrawable(cachedImage);

Using this approach, the ListView performs a lot better and feels much more smooth because the UI thread is no longer blocked by the loading of images.

Improve the performance even more

If you tried the solution described above you will notice that the ListView is still not a 100% smooth. You will still notice some little disruptions that make it a little less smooth than it could be. There are two things remaining that can be improved:

  • the expensive call to findViewById()
  • inflating the entire row from XML every time

The solution is obvious: we should cache/reuse these things! Mark Murphy did a very nice job on writing a few blog entries describing how this can be done. To reuse the views which are inflated from XML read this blog entry:
http://www.androidguys.com/2008/07/17/fancy-listviews-part-two/

To cache the views returned by findViewById() read this blog entry:
http://www.androidguys.com/2008/07/22/fancy-listviews-part-three/

If we apply the strategies described in Mark Murphy’s blog entries our ImageAndTextListAdapter could look like the following:

public class ImageAndTextListAdapter extends ArrayAdapter<ImageAndText> {

    private ListView listView;
private AsyncImageLoader asyncImageLoader; public ImageAndTextListAdapter(Activity activity, List<ImageAndText> imageAndTexts, ListView listView) {
super(activity, 0, imageAndTexts);
this.listView = listView;
asyncImageLoader = new AsyncImageLoader();
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
Activity activity = (Activity) getContext(); // Inflate the views from XML
View rowView = convertView;
ViewCache viewCache;
if (rowView == null) {
LayoutInflater inflater = activity.getLayoutInflater();
rowView = inflater.inflate(R.layout.image_and_text_row, null);
viewCache = new ViewCache(rowView);
rowView.setTag(viewCache);
} else {
viewCache = (ViewCache) rowView.getTag();
}
ImageAndText imageAndText = getItem(position); // Load the image and set it on the ImageView
String imageUrl = imageAndText.getImageUrl();
ImageView imageView = viewCache.getImageView();
imageView.setTag(imageUrl);
Drawable cachedImage = asyncImageLoader.loadDrawable(imageUrl, new ImageCallback() {
public void imageLoaded(Drawable imageDrawable, String imageUrl) {
ImageView imageViewByTag = (ImageView) listView.findViewWithTag(imageUrl);
if (imageViewByTag != null) {
imageViewByTag.setImageDrawable(imageDrawable);
}
}
});
imageView.setImageDrawable(cachedImage); // Set the text on the TextView
TextView textView = viewCache.getTextView();
textView.setText(imageAndText.getText()); return rowView;
}
}

There are two things to notice. The first thing is that the drawable is not directly set to the ImageView anymore after loading. Instead, the right ImageView is looked up through it’s tag. This is done because we’re now reusing views and the images might end up on the wrong rows. We need a reference to the ListView to lookup ImageViews by tag.

The other thing to notice, is that this implementation uses an object called ViewCache. This is what the class for that object looks like:

public class ViewCache {

    private View baseView;
private TextView textView;
private ImageView imageView; public ViewCache(View baseView) {
this.baseView = baseView;
} public TextView getTextView() {
if (textView == null) {
textView = (TextView) baseView.findViewById(R.id.text);
}
return titleView;
} public ImageView getImageView() {
if (imageView == null) {
imageView = (ImageView) baseView.findViewById(R.id.image);
}
return imageView;
}
}

This ViewCache is the same as what Mark Murphy calls a “ViewWrapper” and takes care of caching individual views which normally would have to be looked up every time using the expensive call to findViewById().

To summarize

I’ve shown you how to improve performance of a ListView in three different ways:

  • By loading images in a seperate thread
  • By reusing rows in the list
  • By caching views within a row

It took me quite some time to figure this stuff out, especially the image loading part. So I thought it is all worth mentioning to avoid you having to waste too much time on it.

Next time I will discuss other interesting challenges in the world of Android!

TO BE CONTINUED …

Tags: Android, Java, Performance Posted in: Custom Development

Exploring the world of Android :: Part 2的更多相关文章

  1. Exploring the world of Android :: Part 1

    This blog is accidentally find out, it tells the story of one of our friends about the exploration o ...

  2. Android 异步网络图片加载

    ListView异步加载图片 http://www.eoeandroid.com/forum.php?mod=viewthread&tid=161586 [Android分享] ListVie ...

  3. Android Weekly Notes Issue #316

    July 1st, 2018 Android Weekly Issue #316 本期内容包含教你使用Kotlin通过Annotation Processor生成代码文件, JetPack中的Andr ...

  4. Android Weekly Notes Issue #233

    Android Weekly Issue #233 November 27th, 2016 Android Weekly Issue #233 本期内容包括: 用Mockito做RxJava的单元测试 ...

  5. Android Weekly Notes Issue #228

    Android Weekly Issue #228 October 23rd, 2016 Android Weekly Issue #228 本期内容包括: Android 7.1的App Short ...

  6. Android 7.1 App Shortcuts使用

    Android 7.1 App Shortcuts使用 Android 7.1已经发了预览版, 这里是API Overview: API overview. 其中App Shortcuts是新提供的一 ...

  7. Android Weekly Notes Issue #227

    Android Weekly Issue #227 October 16th, 2016 Android Weekly Issue #227. 本期内容包括: Google的Mobile Vision ...

  8. android调试工具DDMS的使用详解

    具体可见http://developer.android.com/tools/debugging/ddms.html. DDMS为IDE和emultor.真正的android设备架起来了一座桥梁.开发 ...

  9. 使用calabash测试开源中国Android客户端

    Calabash-android是支持android的UI自动化测试框架,前面已经介绍过<中文Win7下成功安装calabash-android步骤>,这篇博文尝试测试一个真实应用:开源中 ...

随机推荐

  1. Python模块和类.md

    模块的定义 代码的层次结构 对于python的层次结构一般为包->模块 包也就是文件夹,但是文件夹下必须有文件"init.py"那么此文件夹才可以被识别为包."in ...

  2. Structs复习 ActionMethod

    action在执行的是时候 可以不执行excute方法 可以由自己制定 可以在action标签里指定  也可以在方法里动态指定 推荐使用后者 jar包 web.xml <?xml version ...

  3. ubuntu14.04后安装win10记录

    1首先修改启动引导顺序,从U盘启动, 2自动安装,产生一个问题,gpt分区无法安装,解决方法,感谢https://jingyan.baidu.com/article/08b6a591c82df414a ...

  4. 游戏引擎中三大及时光照渲染方法介绍(以unity3d为例)

    (转)游戏引擎中三大及时光照渲染方法介绍(以unity3d为例)   重要:在目前市面上常见的游戏引擎中,主要采用以下三种灯光实现方式: 顶点照明渲染路径细节 Vertex Lit Rendering ...

  5. C# HttpWebRequest 模拟下载

    C# web 获取服务端cookie 原文地址:https://www.cnblogs.com/louby/p/5569536.html C#多线程环境下调用 HttpWebRequest 并发连接限 ...

  6. linux 安装nexus3

    准备工作: 环境:linux 系统:centos6.4-x86-x64 安装工具:nexus-3.14 软件下载:nexus-3.14 官网下载地址:点击打开链接 将下载的压缩包通过xft5上传至/o ...

  7. linux下mysql升级

    最近漏洞扫描,扫描出了数据库存在中高危漏洞,于是迫切需要进行数据库升级.上网查了各种资料,说法很多,也到自己虚拟机上试了好多方法,终于倒腾出来,做下小总结记录一下. 升级操作: 1.到mysql官网h ...

  8. sql 2012的补丁 SP4下载地址

    https://www.microsoft.com/zh-cn/download/confirmation.aspx?id=56040

  9. 581. Shortest Unsorted Continuous Subarray

      Given an integer array, you need to find one continuous subarray that if you only sort this subarr ...

  10. Django2.1在根据models生成数据库表时报 __init__() missing 1 required positional argument: 'on_delete'

    解决办法: a=models.ForeignKey('BookInfo',on_delete=models.CASCADE,) 即在外键值的后面加上 on_delete=models.CASCADE ...