【移动应用开发技术】android 安卓异步加载网络图片与viewpager结合使用示例_第1页
【移动应用开发技术】android 安卓异步加载网络图片与viewpager结合使用示例_第2页
【移动应用开发技术】android 安卓异步加载网络图片与viewpager结合使用示例_第3页
【移动应用开发技术】android 安卓异步加载网络图片与viewpager结合使用示例_第4页
【移动应用开发技术】android 安卓异步加载网络图片与viewpager结合使用示例_第5页
免费预览已结束,剩余1页可下载查看

付费下载

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】android安卓异步加载网络图片,与viewpager结合使用示例

【1】异步加载图片类AsyncImageLoaderpackage

com.example.testdddleapk.cus;

import

java.io.IOException;

import

java.lang.ref.SoftReference;

import

java.util.HashMap;

import

org.apache.http.HttpEntity;

import

org.apache.http.HttpResponse;

import

org.apache.http.HttpStatus;

import

org.apache.http.client.ClientProtocolException;

import

org.apache.http.client.HttpClient;

import

org.apache.http.client.methods.HttpGet;

import

org.apache.http.impl.client.DefaultHttpClient;

import

org.apache.http.params.CoreConnectionPNames;

import

android.graphics.drawable.Drawable;

import

android.os.Handler;

import

android.os.Message;

/**

*

异步加载图片

*/

public

class

AsyncImageLoader

{

//

软引用,使用内存做临时缓存

(程序退出,或内存不够则清除软引用)

private

HashMap<String,

SoftReference<Drawable>>

p_w_picpathCache;

public

AsyncImageLoader()

{

p_w_picpathCache

=

new

HashMap<String,

SoftReference<Drawable>>();

}

/**

*

定义回调接口

*/

public

interface

ImageCallback

{

public

void

p_w_picpathLoaded(Drawable

p_w_picpathDrawable,

String

p_w_picpathUrl);

}

/**

*

创建子线程加载图片

*

子线程加载完图片交给handler处理(子线程不能更新ui,而handler处在主线程,可以更新ui)

*

handler又交给p_w_picpathCallback,p_w_picpathCallback须要自己来实现,在这里可以对回调参数进行处理

*

@param

p_w_picpathUrl

:须要加载的图片url

*

@param

p_w_picpathCallback:

*

@return

*/

public

Drawable

loadDrawable(final

String

p_w_picpathUrl,final

ImageCallback

p_w_picpathCallback)

{

//如果缓存中存在图片

,则首先使用缓存

if

(p_w_picpathCache.containsKey(p_w_picpathUrl))

{

SoftReference<Drawable>

softReference

=

p_w_picpathCache.get(p_w_picpathUrl);

Drawable

drawable

=

softReference.get();

if

(drawable

!=

null)

{

System.out.println("loadDrawable");

p_w_picpathCallback.p_w_picpathLoaded(drawable,

p_w_picpathUrl);//执行回调

return

drawable;

}

}

/**

*

在主线程里执行回调,更新视图

*/

final

Handler

handler

=

new

Handler()

{

public

void

handleMessage(Message

message)

{

System.out.println("handleMessage");

p_w_picpathCallback.p_w_picpathLoaded((Drawable)

message.obj,

p_w_picpathUrl);

}

};

/**

*

创建子线程访问网络并加载图片

,把结果交给handler处理

*/

new

Thread()

{

@Override

public

void

run()

{

Drawable

drawable

=

loadImageFromUrl(p_w_picpathUrl);

//

下载完的图片放到缓存里

p_w_picpathCache.put(p_w_picpathUrl,

new

SoftReference<Drawable>(drawable));

Message

message

=

handler.obtainMessage(0,

drawable);

handler.sendMessage(message);

}

}.start();

return

null;

}

/**

*

下载图片

(注意HttpClient

和httpUrlConnection的区别)

*/

public

Drawable

loadImageFromUrl(String

url)

{

try

{

HttpClient

client

=

new

DefaultHttpClient();

client.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,

1000*15);

HttpGet

get

=

new

HttpGet(url);

HttpResponse

response;

response

=

client.execute(get);

if

(response.getStatusLine().getStatusCode()

==

HttpStatus.SC_OK)

{

HttpEntity

entity

=

response.getEntity();

Drawable

d

=

Drawable.createFromStream(entity.getContent(),"src");

return

d;

}

else

{

return

null;

}

}

catch

(ClientProtocolException

e)

{

e.printStackTrace();

}

catch

(IOException

e)

{

e.printStackTrace();

}

return

null;

}

//清除缓存

public

void

clearCache()

{

if

(this.p_w_picpathCache.size()

>

0)

{

this.p_w_picpathCache.clear();

}

}

}【2】pagerAdapter的instantiateItem方法@SuppressLint("NewApi")

@Override

public

Object

instantiateItem(final

ViewGroup

container,

final

int

position)

{

String

url=imgsUrls[position];

Drawable

cachedImage

=

asyncImageLoader.loadDrawable(url,

new

ImageCallback()

{

@SuppressLint("NewApi")

public

void

p_w_picpathLoaded(Drawable

p_w_picpathDrawable,String

p_w_picpathUrl)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论