Android中的延迟加载系列5 (综合案例 含完整代码及工程下载)
2013-04-20 21:49
441 查看
本文给出Android延迟加载综合案例,描述ListView和ImageView的分页延迟加载,已经若干有用的封装技术,来结束本系列文章。
本文将在ListView延迟加载示例工程的基础上进行修改,加入图片延迟加载的功能。
在行布局中加入图片,
[html]
view plaincopyprint?
<?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
[java]
view plaincopyprint?
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的一张图片。具体的工程应用请大家自行修改成行图片相关的代码。
[java]
view plaincopyprint?
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); 则表示不显示默认图片。
[java]
view plaincopyprint?
private ImageLoader imageLoader = new ImageLoader(this,R.drawable.nohead);
再在updateItem()方法中,从row.xml获取ImageView对象,并通过imageLoader.displayImage(bean, imageView);进行赋值。
[java]
view plaincopyprint?
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销毁的时候,记得释放加载器和停止加载线程。
[java]
view plaincopyprint?
@Override
public void onDestroy()
{
imageLoader.stopThread();
imageLoader.clearMemoryCache();
super.onDestroy();
}
至此,图片的延迟加载已经结束。由此可以看出,只需要两个步骤,即定义加载器(ImageLoader)和显示图片(imageLoader.displayImage(bean, imageView))就可以达到延迟加载的目的,而如果不对其进行封装,过程将非常复杂。
完整代码 下载
工程运行示例图
本文将在ListView延迟加载示例工程的基础上进行修改,加入图片延迟加载的功能。
在行布局中加入图片,
[html]
view plaincopyprint?
<?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>
<?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
[java]
view plaincopyprint?
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; } }
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的一张图片。具体的工程应用请大家自行修改成行图片相关的代码。
[java]
view plaincopyprint?
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;
}
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); 则表示不显示默认图片。
[java]
view plaincopyprint?
private ImageLoader imageLoader = new ImageLoader(this,R.drawable.nohead);
private ImageLoader imageLoader = new ImageLoader(this,R.drawable.nohead);
再在updateItem()方法中,从row.xml获取ImageView对象,并通过imageLoader.displayImage(bean, imageView);进行赋值。
[java]
view plaincopyprint?
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()); } }
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销毁的时候,记得释放加载器和停止加载线程。
[java]
view plaincopyprint?
@Override
public void onDestroy()
{
imageLoader.stopThread();
imageLoader.clearMemoryCache();
super.onDestroy();
}
@Override public void onDestroy() { imageLoader.stopThread(); imageLoader.clearMemoryCache(); super.onDestroy(); } }
至此,图片的延迟加载已经结束。由此可以看出,只需要两个步骤,即定义加载器(ImageLoader)和显示图片(imageLoader.displayImage(bean, imageView))就可以达到延迟加载的目的,而如果不对其进行封装,过程将非常复杂。
完整代码 下载
工程运行示例图
相关文章推荐
- Android中的延迟加载系列5 (综合案例 含完整代码及工程下载)
- Android中的延迟加载系列(ListView 3 含完整代码及工程下载)
- Android中的延迟加载系列(ListView 3 含完整代码及工程下载)
- Android Camera开发:给摄像头预览界面加个ZoomBar(附完整代码下载)
- android官方最新以及2.2、2.3.5源代码(完整)下载以及用source insight查看源代码
- Android应用.三星i9000系列(2).工程代码,网络锁与锁三键
- Java文件上与下载代码-完整代码案例
- 设计模式系列代码下载--VC++可编译工程
- Android欢迎界面的创建及引导完整代码(判断是否是第一次下载注册)
- kaggle数据挖掘竞赛初步--Titanic<数据变换> 完整代码: https://github.com/cindycindyhi/kaggle-Titanic 特征工程系列: Titanic
- 初始化开发环境以及下载android-4.4源代码(kitkat)
- android典型代码系列(二十二)------按键使上面的EditText抖动
- windows下载android 完整源码
- 使用android SDK + NDK的方法创建含本地代码的工程
- android代码下载
- java获取http:图片下载代码——android基础编
- android上传下载系列:一般性知识解读
- Android学习系列(6)--App模块化及工程扩展
- Android进阶图片处理内存优化系列之图片墙案例