【移動(dòng)應(yīng)用開發(fā)技術(shù)】android 安卓異步加載網(wǎng)絡(luò)圖片與viewpager結(jié)合使用示例_第1頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】android 安卓異步加載網(wǎng)絡(luò)圖片與viewpager結(jié)合使用示例_第2頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】android 安卓異步加載網(wǎng)絡(luò)圖片與viewpager結(jié)合使用示例_第3頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】android 安卓異步加載網(wǎng)絡(luò)圖片與viewpager結(jié)合使用示例_第4頁
【移動(dòng)應(yīng)用開發(fā)技術(shù)】android 安卓異步加載網(wǎng)絡(luò)圖片與viewpager結(jié)合使用示例_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡介

【移動(dòng)應(yīng)用開發(fā)技術(shù)】android安卓異步加載網(wǎng)絡(luò)圖片,與viewpager結(jié)合使用示例

【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

{

//

軟引用,使用內(nèi)存做臨時(shí)緩存

(程序退出,或內(nèi)存不夠則清除軟引用)

private

HashMap<String,

SoftReference<Drawable>>

p_w_picpathCache;

public

AsyncImageLoader()

{

p_w_picpathCache

=

new

HashMap<String,

SoftReference<Drawable>>();

}

/**

*

定義回調(diào)接口

*/

public

interface

ImageCallback

{

public

void

p_w_picpathLoaded(Drawable

p_w_picpathDrawable,

String

p_w_picpathUrl);

}

/**

*

創(chuàng)建子線程加載圖片

*

子線程加載完圖片交給handler處理(子線程不能更新ui,而handler處在主線程,可以更新ui)

*

handler又交給p_w_picpathCallback,p_w_picpathCallback須要自己來實(shí)現(xiàn),在這里可以對(duì)回調(diào)參數(shù)進(jìn)行處理

*

@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);//執(zhí)行回調(diào)

return

drawable;

}

}

/**

*

在主線程里執(zhí)行回調(diào),更新視圖

*/

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);

}

};

/**

*

創(chuàng)建子線程訪問網(wǎng)絡(luò)并加載圖片

,把結(jié)果交給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的區(qū)別)

*/

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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論