Android中的延遲加載系列5 (綜合案例 含完整代碼及工程下載)

本文給出Android延遲加載綜合案例,描述ListView和ImageView的分頁延遲加載,已經若干有用的封裝技術,來結束本系列文章。

本文將在ListView延遲加載示例工程的基礎上進行修改,加入圖片延遲加載的功能。

在行佈局中加入圖片,

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout android:id="@+android:id/iconpref"
	xmlns:android="http://schemas.android.com/apk/res/android" android:layout_margin="30dip"
	android:layout_width="fill_parent" android:layout_height="wrap_content"
	android:gravity="center_vertical" android:paddingRight="?android:attr/scrollbarSize">
   
    <ImageView style="@style/imageView_Small"
		android:id="@+id/icon" android:layout_alignParentLeft="true"
		android:layout_centerVertical="true"></ImageView>
	
	<TextView android:id="@+id/title" android:layout_marginLeft="10dip"
		android:textAppearance="?android:attr/textAppearanceSmall"
		android:layout_height="wrap_content" android:layout_width="wrap_content"
		android:textStyle="bold" 
		android:layout_toRightOf="@+id/icon"/>
	
	<TextView android:id="@+id/description"
		android:textAppearance="?android:attr/textAppearanceSmall"
		android:layout_marginLeft="10dip" android:maxLines="2"
		android:layout_height="wrap_content" android:layout_width="wrap_content"
		android:layout_below="@+id/title" android:text=""
		android:layout_toRightOf="@+id/icon"></TextView>

</RelativeLayout>

Row.java繼承LazyImage

package com.whyonly.bean;

import com.whyonly.core.bean.LazyImage;



public class Row extends LazyImage{
	
	private String title;
	private String description;
	
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}

}

在UIData.java中,加入網絡圖片,此方法只加入本人csdn的頭像以及來自google的一張圖片。具體的工程應用請大家自行修改成行圖片相關的代碼。

	public static List<Row> getListRows(int startIndex, int endIndex) {
		List<Row> rows = new ArrayList<Row>();
		for(int i=startIndex;i<=endIndex;i++){
			Row row = new Row();
			row.setImage_url(i%2==0 ? "http://google-maps-icons.googlecode.com/files/airport.png"
					: "http://avatar.csdn.net/7/8/2/3_whyonly.jpg");
			row.setTimestamp(0);
			row.setTitle("Title "+i);
			row.setDescription("Title description "+i);
			rows.add(row);
		}
		return rows;
	}

在LazyLoadingActivity.java中,引入圖片延遲加載類ImageLoader。R.drawable.nohead是空頭像,是顯示下載圖片之前的默認圖片。如果調用new ImageLoader(this,0); 則表示不顯示默認圖片。

private ImageLoader imageLoader = new ImageLoader(this,R.drawable.nohead);

再在updateItem()方法中,從row.xml獲取ImageView對象,並通過imageLoader.displayImage(bean, imageView);進行賦值。

private void updateItem(final View vi,final Row bean){
		if (bean != null) {
			TextView title = (TextView) vi.findViewById(R.id.title);
			TextView description = (TextView) vi.findViewById(R.id.description);
			ImageView imageView = (ImageView) vi.findViewById(R.id.icon);
			imageLoader.displayImage(bean, imageView);
			title.setText(bean.getTitle());
			description.setText(bean.getDescription());
			
        }
	}

最後,在Activity銷燬的時候,記得釋放加載器和停止加載線程。

	@Override
    public void onDestroy()
    {
        imageLoader.stopThread();
        imageLoader.clearMemoryCache();
        super.onDestroy();
    }
}



至此,圖片的延遲加載已經結束。由此可以看出,只需要兩個步驟,即定義加載器(ImageLoader)和顯示圖片(imageLoader.displayImage(bean, imageView))就可以達到延遲加載的目的,而如果不對其進行封裝,過程將非常複雜。


完整代碼 下載

工程運行示例圖


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章