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))就可以达到延迟加载的目的,而如果不对其进行封装,过程将非常复杂。


完整代码 下载

工程运行示例图


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的远程下载并加载动态链接库的示例代码,包括下载器部分。请注意,这只是一个示例代码,仅供参考。 下载器部分: ```java public class Downloader { public static void download(String url, String localPath) throws Exception { URLConnection connection = new URL(url).openConnection(); int contentLength = connection.getContentLength(); InputStream input = new BufferedInputStream(connection.getInputStream()); OutputStream output = new FileOutputStream(localPath); byte[] data = new byte[1024]; int count; int progress = 0; while ((count = input.read(data)) != -1) { output.write(data, 0, count); progress += count; System.out.println("Downloaded " + progress + "/" + contentLength + " bytes"); } output.flush(); output.close(); input.close(); } } ``` 加载动态链接库部分: ```java public class NativeLibraryLoader { private static final String LIB_NAME = "mylibrary"; private static final String LIB_URL = "https://example.com/mylibrary.so"; static { try { File tempDir = new File(System.getProperty("java.io.tmpdir")); File libFile = new File(tempDir, LIB_NAME); if (!libFile.exists()) { System.out.println("Downloading library..."); Downloader.download(LIB_URL, libFile.getAbsolutePath()); } System.load(libFile.getAbsolutePath()); } catch (Exception e) { e.printStackTrace(); } } public static native void myNativeMethod(); } ``` 在这个示例,我们首先定义了要下载的动态链接库的名称和远程URL。在加载本地库之前,我们检查本地目录是否已经有了这个库,如果没有,就使用`Downloader`类从远程URL下载它。然后我们使用`System.load()`方法加载本地库。 请注意,为了使本地库能够正确加载,你需要确保它与Android设备的CPU架构相匹配。例如,如果你的设备使用ARM架构,则需要下载和加载ARM版本的库。 当然,还有很多其他的细节需要考虑,例如错误处理、下载进度更新、库的存储位置等等,但这个示例应该可以为你提供一个大致的思路。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值