亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 系統 > Android > 正文

android上的一個網絡接口和圖片緩存框架enif簡析

2020-04-11 12:38:04
字體:
來源:轉載
供稿:網友
1.底層網絡接口采用apache的httpclient連接池框架;
2.圖片緩存采用基于LRU的算法;
3.網絡接口采用監聽者模式;
4.包含圖片的OOM處理(及時回收處理技術的應用);

圖片核心處理類:CacheView.java
復制代碼 代碼如下:

package xiaogang.enif.image;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.concurrent.RejectedExecutionException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import xiaogang.enif.utils.HttpManager;
import xiaogang.enif.utils.IOUtils;
import xiaogang.enif.utils.LogUtils;
import xiaogang.enif.utils.LruCache;
import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.BitmapFactory.Options;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.widget.ImageView;
public class CacheView extends ImageView {
private static final int DEFAULT_RES_ID = 0;
private int mDefaultImage = DEFAULT_RES_ID;
private static LruCache<String, Bitmap> mLruCache;
private static HashMap<Integer, SoftReference<Bitmap>> mResImage;
private Context mContext;
private LogUtils mLog = LogUtils.getLog(CacheView.class);
public CacheView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
public CacheView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public CacheView(Context context) {
super(context);
init(context);
}
private void init(Context context) {
mContext = context;
if (mLruCache == null) {
final int cacheSize = getCacheSize(context);
mLruCache = new LruCache<String, Bitmap>(cacheSize) {
@Override
protected int sizeOf(String key, Bitmap bitmap) {
// The cache size will be measured in bytes rather than
// number of items.
return bitmap.getRowBytes() * bitmap.getHeight();
}
@Override
protected void entryRemoved(boolean evicted, String key, Bitmap oldValue,
Bitmap newValue) {
if (evicted && oldValue != null && !oldValue.isRecycled()) {
oldValue.recycle();
oldValue = null;
}
}
};
}
if (mResImage == null) {
mResImage = new HashMap<Integer, SoftReference<Bitmap>>();
}
}
@Override
protected void onDraw(Canvas canvas) {
BitmapDrawable drawable = (BitmapDrawable)getDrawable();
if (drawable == null) {
setDefaultImage();
} else {
if (drawable.getBitmap() == null || drawable.getBitmap().isRecycled()) {
setDefaultImage();
}
}
try {
super.onDraw(canvas);
} catch(RuntimeException ex) {
}
}
public void setImageUrl(String url, int resId) {
setTag(url);
Bitmap bitmap = getBitmapFromCache(url);
if (bitmap == null || bitmap.isRecycled()) {
mDefaultImage = resId;
setDefaultImage();
try {
new DownloadTask().execute(url);
} catch (RejectedExecutionException e) {
// do nothing, just keep not crash
}
} else {
setImageBitmap(bitmap);
}
}
private void setDefaultImage() {
if (mDefaultImage != DEFAULT_RES_ID) {
setImageBitmap(getDefaultBitmap(mContext));
}
}
private Bitmap getDefaultBitmap(Context context) {
SoftReference<Bitmap> loading = mResImage.get(mDefaultImage);
if (loading == null || loading.get() == null || loading.get().isRecycled()) {
loading = new SoftReference<Bitmap>(BitmapFactory.decodeResource(
context.getResources(), mDefaultImage));
mResImage.put(mDefaultImage, loading);
}
return loading.get();
}
private class DownloadTask extends AsyncTask<String, Void, Bitmap> {
private String mParams;
@Override
public Bitmap doInBackground(String... params) {
mParams = params[0];
final Bitmap bm = download(mParams);
addBitmapToCache(mParams, bm);
return bm;
}
@Override
public void onPostExecute(Bitmap bitmap) {
String tag = (String)getTag();
if (!TextUtils.isEmpty(tag) && tag.equals(mParams)) {
if (bitmap != null) {
setImageBitmap(bitmap);
}
}
}
};
/*
* An InputStream that skips the exact number of bytes provided, unless it
* reaches EOF.
*/
static class FlushedInputStream extends FilterInputStream {
public FlushedInputStream(InputStream inputStream) {
super(inputStream);
}
@Override
public long skip(long n) throws IOException {
long totalBytesSkipped = 0L;
while (totalBytesSkipped < n) {
long bytesSkipped = in.skip(n - totalBytesSkipped);
if (bytesSkipped == 0L) {
int b = read();
if (b < 0) {
break; // we reached EOF
} else {
bytesSkipped = 1; // we read one byte
}
}
totalBytesSkipped += bytesSkipped;
}
return totalBytesSkipped;
}
}
private Bitmap download(String url) {
InputStream in = null;
HttpEntity entity = null;
Bitmap bmp = null;
try {
final HttpGet get = new HttpGet(url);
final HttpResponse response = HttpManager.execute(mContext, get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
entity = response.getEntity();
in = entity.getContent();
try {
bmp = getDecodeBitmap(in, url);
} catch (OutOfMemoryError err) {
Runtime.getRuntime().gc();
bmp = getDecodeBitmap(in, url);
}
} else {
get.abort();
return bmp;
}
addBitmapToCache(url, bmp);
} catch (IOException e) {
return bmp;
} finally {
IOUtils.closeStream(in);
}
return bmp;
}
private final Bitmap getDecodeBitmap(InputStream in, String url) {
Options options = new Options();
options.inPurgeable = true;
options.inInputShareable = true;
return BitmapFactory.decodeStream(new FlushedInputStream(in), null, options);
}
private final void addBitmapToCache(String url, Bitmap bitmap) {
if (bitmap != null) {
mLruCache.put(url, bitmap);
Runtime.getRuntime().gc();
}
}
private final Bitmap getBitmapFromCache(String url) {
return mLruCache.get(url);
}
private int getCacheSize(Context context) {
// According to the phone memory, set a proper cache size for LRU cache
// dynamically.
final ActivityManager am = (ActivityManager)context
.getSystemService(Context.ACTIVITY_SERVICE);
final int memClass = am.getMemoryClass();
int cacheSize;
if (memClass <= 24) {
cacheSize = (memClass << 20) / 24;
} else if (memClass <= 36) {
cacheSize = (memClass << 20) / 18;
} else if (memClass <= 48) {
cacheSize = (memClass << 20) / 12;
} else {
cacheSize = (memClass << 20) >> 3;
}
mLog.debug("cacheSize == "+cacheSize);
System.out.println("cacheSize == "+cacheSize);
return cacheSize;
}
public static void recycle() {
if (mLruCache != null && !mLruCache.isEmpty()) {
mLruCache.evictAll();
mLruCache = null;
}
if (mResImage != null) {
for (SoftReference<Bitmap> reference : mResImage.values()) {
Bitmap bitmap = reference.get();
if (bitmap != null && !bitmap.isRecycled()) {
bitmap.recycle();
bitmap = null;
}
}
mResImage = null;
}
}
}

說明:
1)entryRemoved在做bitmap recycle的時候的3個條件缺一不可;
2)onDraw里面判斷圖片是否被回收,如果回收,需要設置默認圖片;
3)add bitmap到cache的時候Runtime.getRuntime().gc();的調用;
4)getCacheSize可以根據手機具體的內存來動態設置我們實際需要的緩存大小;
5)退出時,記得調用recycle()方法;
網絡接口核心類:WSAPI.java, WSCfg.java, WSTask.java
復制代碼 代碼如下:

<STRONG>package xiaogang.enif.net;
import java.util.ArrayList;
import org.apache.http.message.BasicNameValuePair;
/**
* web service configuration file
* */
public class WSCfg {
public static final int USER_LOGIN = 0;//action
public static final int USER_LOGOUT = 1;//action
public static ArrayList<BasicNameValuePair> sValuePairs;//common vps
static {
sValuePairs = new ArrayList<BasicNameValuePair>();
sValuePairs.add(new BasicNameValuePair("v", "1.0"));
sValuePairs.add(new BasicNameValuePair("format", "json"));
}
}</STRONG>

復制代碼 代碼如下:

<STRONG>package xiaogang.enif.net;
import java.util.ArrayList;
import java.util.concurrent.RejectedExecutionException;
import org.apache.http.message.BasicNameValuePair;
import xiaogang.enif.net.WSTask.TaskListener;
import android.content.Context;
public class WSAPI {
private WSAPI() {
}
public static void execute(Context context, TaskListener listener, int action,
ArrayList<BasicNameValuePair> vp) {
try {
new WSTask(context, listener, action, vp).execute();
} catch (RejectedExecutionException e) {
// do nothing, just keep not crashing.
}
}
}
</STRONG>

復制代碼 代碼如下:

<STRONG>package xiaogang.enif.net;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import xiaogang.enif.utils.HttpManager;
import xiaogang.enif.utils.IOUtils;
import xiaogang.enif.utils.LogUtils;
import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.text.TextUtils;
public class WSTask extends AsyncTask<Void, Void, Object> {
private int mAction;
private String mErrorCode;
private Object mParameter;
private Context mContext;
private TaskListener mTaskListener;
private Exception mReason;
private final LogUtils mLog = LogUtils.getLog(WSTask.class);
public WSTask(Context context, TaskListener listener, int action, Object paramObject) {
mContext = context;
mTaskListener = listener;
mParameter = paramObject;
mAction = action;
}
@Override
public Object doInBackground(Void... arg0) {
Object result = null;
try {
@SuppressWarnings("unchecked")
ArrayList<BasicNameValuePair> vps = (ArrayList<BasicNameValuePair>)mParameter;
final String jsonString = request(mContext, "your url", vps);
mLog.debug(jsonString);
result = parseJson(jsonString);
if (result != null && result instanceof String
&& TextUtils.isDigitsOnly((String)result)) {
mErrorCode = (String)result;
return null;
}
} catch (Exception e) {
mReason = e;
mLog.error(e.getMessage());
return null;
}
return result;
}
@Override
public void onPostExecute(Object result) {
if (mContext== null) {
clearTask();
return;
}
if (mContext instanceof Activity && ((Activity) mContext).isFinishing()) {
clearTask();
return;
}
if (result == null || mReason != null) {
mTaskListener.onFailed(mAction, mErrorCode, mReason);
} else {
mTaskListener.onSuccess(mAction, result);
}
clearTask();
}
private String request(Context context, String url, ArrayList<BasicNameValuePair> vp)
throws IOException {
final HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(vp, "UTF_8"));
InputStream in = null;
HttpEntity entity = null;
try {
final HttpResponse response = HttpManager.execute(context, post);
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
entity = response.getEntity();
if (entity != null) {
in = entity.getContent();
return IOUtils.stream2String(in);
}
} else {
post.abort();
mLog.error("http code: " + response.getStatusLine().getStatusCode());
}
return null;
} catch (IOException ex) {
post.abort();
throw ex;
} catch (RuntimeException ex) {
post.abort();
throw ex;
} finally {
if(entity!=null) {
entity.consumeContent();
}
IOUtils.closeStream(in);
}
}
private Object parseJson(String jsonString) throws IOException {
try {
JSONObject jobj = new JSONObject(jsonString);
if (jobj.has("errorcode")) {
return jobj.optString("errorcode");
}
if (jobj.has("resultlist")) {
ArrayList<HashMap<String, String>> arrList;
arrList = new ArrayList<HashMap<String, String>>();
JSONArray jsonArray = jobj.optJSONArray("resultlist");
final int len = jsonArray.length();
for (int i = 0; i < len; i++) {
final JSONObject obj = (JSONObject)jsonArray.opt(i);
arrList.add(parse2Map(obj));
}
return arrList;
} else {
return parse2Map(jobj);
}
} catch (JSONException e) {
IOException ioe = new IOException("Invalid json String...");
ioe.initCause(e);
throw ioe;
}
}
private HashMap<String, String> parse2Map(JSONObject jsonObj) throws IOException {
final HashMap<String, String> hashMap = new HashMap<String, String>();
@SuppressWarnings("unchecked")
final Iterator<String> keyIter = jsonObj.keys();
String key, value;
while (keyIter != null && keyIter.hasNext()) {
key = keyIter.next();
value = jsonObj.optString(key);
hashMap.put(key, value);
}
return hashMap;
}
private void clearTask() {
mTaskListener = null;
mParameter = null;
mContext = null;
}
public interface TaskListener {
public void onSuccess(int action, Object result);
public void onFailed(int action, String errcode, Exception ex);
}
}
</STRONG>

說明:
1)根據你的服務器接口實際情況,去修改parseJson方法;
2)WSCfg里面可以定義接口的action;
sample:
復制代碼 代碼如下:

package xiaogang.enif.ui;
import java.util.ArrayList;
import org.apache.http.message.BasicNameValuePair;
import xiaogang.enif.R;
import xiaogang.enif.image.CacheView;
import xiaogang.enif.net.WSAPI;
import xiaogang.enif.net.WSCfg;
import xiaogang.enif.net.WSTask.TaskListener;
import xiaogang.enif.widget.ListsApdater;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends Activity implements TaskListener {
ListView mList;
ListsApdater mAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupViews();
}
private void setupViews() {
mList = (ListView)findViewById(R.id.list);
mAdapter = new ListsApdater(this, mUrls);
mList.setAdapter(mAdapter);
final ArrayList<BasicNameValuePair> vp = new ArrayList<BasicNameValuePair>();
vp.addAll(WSCfg.sValuePairs);
vp.add(new BasicNameValuePair("imei", "123"));
vp.add(new BasicNameValuePair("imsi", "123"));
WSAPI.execute(this, this, WSCfg.USER_LOGIN, vp);
}
@Override
protected void onDestroy() {
super.onDestroy();
mAdapter.recycle();
CacheView.recycle();
}
private String[] mUrls = {
"http://a3.twimg.com/profile_images/670625317/aam-logo-v3-twitter.png",
"http://a3.twimg.com/profile_images/740897825/AndroidCast-350_normal.png",
"http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
"http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
"http://a1.twimg.com/profile_images/97470808/icon_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook.png",
"http://a3.twimg.com/profile_images/768060227/ap4u_normal.jpg",
"http://a1.twimg.com/profile_images/74724754/android_logo_normal.png",
"http://a3.twimg.com/profile_images/681537837/SmallAvatarx150_normal.png",
"http://a1.twimg.com/profile_images/63737974/2008-11-06_1637_normal.png",
"http://a3.twimg.com/profile_images/548410609/icon_8_73.png",
"http://a1.twimg.com/profile_images/612232882/nexusoneavatar_normal.jpg",
"http://a1.twimg.com/profile_images/213722080/Bugdroid-phone_normal.png",
"http://a1.twimg.com/profile_images/645523828/OT_icon_090918_android_normal.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/121630227/Droid.jpg",
"http://a1.twimg.com/profile_images/97470808/icon_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet.png",
"http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
"http://a1.twimg.com/profile_images/957149154/twitterhalf_normal.jpg",
"http://a1.twimg.com/profile_images/97470808/icon.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
"http://a3.twimg.com/profile_images/768060227/ap4u_normal.jpg",
"http://a1.twimg.com/profile_images/74724754/android_logo.png",
"http://a3.twimg.com/profile_images/681537837/SmallAvatarx150_normal.png",
"http://a1.twimg.com/profile_images/63737974/2008-11-06_1637_normal.png",
"http://a3.twimg.com/profile_images/548410609/icon_8_73_normal.png",
"http://a1.twimg.com/profile_images/612232882/nexusoneavatar_normal.jpg",
"http://a1.twimg.com/profile_images/213722080/Bugdroid-phone_normal.png",
"http://a1.twimg.com/profile_images/645523828/OT_icon_090918_android.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/349012784/android_logo_small_normal.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a1.twimg.com/profile_images/850960042/elandroidelibre-logo_300x300_normal.jpg",
"http://a1.twimg.com/profile_images/655119538/andbook_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a3.twimg.com/profile_images/956404323/androinica-avatar_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/121630227/Droid_normal.jpg",
"http://a1.twimg.com/profile_images/957149154/twitterhalf.jpg",
"http://a1.twimg.com/profile_images/97470808/icon_normal.png",
"http://a3.twimg.com/profile_images/511790713/AG_normal.png",
"http://a1.twimg.com/profile_images/909231146/Android_Biz_Man_normal.png",
"http://a3.twimg.com/profile_images/72774055/AndroidHomme-LOGO_normal.jpg",
"http://a1.twimg.com/profile_images/841338368/ea-twitter-icon_normal.png",
"http://a3.twimg.com/profile_images/64827025/android-wallpaper6_2560x160_normal.png",
"http://a3.twimg.com/profile_images/77641093/AndroidPlanet_normal.png"
};
@Override
public void onSuccess(int action, Object result) {
switch (action) {
case WSCfg.USER_LOGIN:
break;
case WSCfg.USER_LOGOUT:
break;
}
}
@Override
public void onFailed(int action, String errcode, Exception ex) {
switch (action) {
case WSCfg.USER_LOGIN:
break;
case WSCfg.USER_LOGOUT:
break;
}
}
}

復制代碼 代碼如下:

package xiaogang.enif.widget;
import xiaogang.enif.R;
import xiaogang.enif.image.CacheView;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
public class ListsApdater extends BaseAdapter {
private String[] mUrls;
private LayoutInflater mInflater;
public ListsApdater(Context context, String[] urls) {
mUrls = urls;
mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return mUrls.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (null == convertView) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
holder.view = (CacheView)convertView.findViewById(R.id.image);
holder.text = (TextView)convertView.findViewById(R.id.text);
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
holder.text.setText("item "+position);
holder.view.setImageUrl(mUrls[position], R.drawable.stub);
return convertView;
}
public void recycle() {
mUrls = null;
mInflater = null;
}
private class ViewHolder {
CacheView view;
TextView text;
}
}

main.xml和item.xml
復制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>

復制代碼 代碼如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<xiaogang.enif.image.CacheView
android:id="@+id/image"
android:layout_width="50dip"
android:layout_height="50dip"
android:scaleType="centerCrop"
android:src="@drawable/stub" />
<TextView
android:id="@+id/text"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="10dip"
android:layout_weight="1"
android:textSize="20dip" />
</LinearLayout>

例子的效果圖如下
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
国产日产久久高清欧美一区| 日本免费一区二区三区视频观看| 欧美视频在线免费看| 狠狠色噜噜狠狠狠狠97| 91麻豆桃色免费看| 亚洲男人天堂2024| 国产精品久久久久久av下载红粉| 精品中文字幕在线| 国产精品视频中文字幕91| 日韩中文字幕av| 久久人91精品久久久久久不卡| 成人免费淫片视频软件| 超在线视频97| 黄网站色欧美视频| 欧美日韩在线观看视频| 欧美激情性做爰免费视频| 久久精品亚洲94久久精品| 日韩大陆欧美高清视频区| 久久天天躁狠狠躁夜夜躁2014| 国产亚洲美女久久| 日韩电影在线观看免费| 亚洲色图偷窥自拍| 欧美久久久精品| 日韩美女中文字幕| 69国产精品成人在线播放| 91av在线免费观看| 久久免费少妇高潮久久精品99| 欧美成人午夜激情视频| 色视频www在线播放国产成人| 亚洲天堂av在线免费观看| 91日本在线观看| 国产成人啪精品视频免费网| 懂色av一区二区三区| 久久五月天色综合| 正在播放欧美一区| 另类美女黄大片| 精品性高朝久久久久久久| 亚洲综合中文字幕在线观看| 久久亚洲精品网站| 亚洲激情在线观看视频免费| 国产女人18毛片水18精品| 国语自产精品视频在免费| 国产一区二区三区在线免费观看| 日韩精品在线第一页| 国产一区视频在线播放| 欧美日韩成人在线观看| 久久成人国产精品| 日韩成人av网址| 国产成人91久久精品| **欧美日韩vr在线| 国产成一区二区| 久久久国产精品亚洲一区| 成人中心免费视频| 国产精品18久久久久久首页狼| 久久久久国产精品免费网站| 亚洲国产精品成人av| 91欧美精品成人综合在线观看| 亚洲伊人成综合成人网| 亚洲激情小视频| 日韩电影中文字幕在线观看| 亚洲国产精品网站| 久热精品视频在线观看| 久久久av免费| 精品国产一区二区三区久久久狼| 日韩精品极品在线观看播放免费视频| 国产人妖伪娘一区91| 日韩亚洲在线观看| 国产高清视频一区三区| 欧美日韩国产综合新一区| 国产精品电影网站| 亚洲国产精品久久久久秋霞不卡| 欧美美女15p| 91老司机精品视频| 日韩高清电影免费观看完整版| 韩国三级电影久久久久久| 久久久免费高清电视剧观看| 深夜福利一区二区| 日韩美女免费视频| 日本精品一区二区三区在线| 午夜精品在线观看| 日韩av一区在线观看| 在线播放日韩精品| 91久久久在线| 97视频在线观看亚洲| 亚洲第一福利在线观看| 欧美国产日产韩国视频| 亚洲综合在线播放| 亚洲精品国产综合久久| 欧美乱大交做爰xxxⅹ性3| 亚洲区一区二区| 欧美一级片在线播放| 亚洲精品ady| 奇米4444一区二区三区| 日韩欧美精品在线观看| 欧洲美女7788成人免费视频| 在线观看久久av| 911国产网站尤物在线观看| 成人激情综合网| 影音先锋欧美在线资源| 国产精品日韩欧美大师| 精品国产视频在线| 一个人看的www久久| 国产一区二区美女视频| 亚洲乱码av中文一区二区| 国产精品无码专区在线观看| 亚洲精品美女在线观看| 国产精品偷伦视频免费观看国产| 成人动漫网站在线观看| 日韩欧美精品网址| 欧美韩日一区二区| 成人在线视频网站| 日韩欧美精品网址| 韩日欧美一区二区| 日韩av免费看| 欧美日产国产成人免费图片| 国产成人综合精品在线| 亚洲美女免费精品视频在线观看| 国产欧美婷婷中文| 国产精品美女无圣光视频| 亚洲精品aⅴ中文字幕乱码| www.日本久久久久com.| 久久影视三级福利片| 精品久久久久久| 国产精品久久久久久久久久ktv| 欧美日韩在线第一页| 成人免费在线视频网址| 久久夜精品香蕉| 久久久亚洲精品视频| 亚洲黄色有码视频| 欧美日韩国产限制| 中文字幕亚洲国产| 亚洲娇小xxxx欧美娇小| 91wwwcom在线观看| 亚洲一区美女视频在线观看免费| 91免费高清视频| 国产精彩精品视频| 日韩欧美在线视频| 国产精品高潮呻吟久久av野狼| 欧美一级淫片videoshd| 怡红院精品视频| 久久免费福利视频| 俺去了亚洲欧美日韩| 久久亚洲精品一区二区| 成人性生交大片免费看视频直播| 亚洲成人1234| 亚洲一区二区久久| 国产99久久精品一区二区 夜夜躁日日躁| 国产亚洲一区二区在线| 日韩激情av在线免费观看| 欧美国产日韩一区二区在线观看| 欧美亚洲激情在线| 久久免费视频网站| 欧美成人在线网站| 一区二区三区高清国产| 亚洲精品国产综合区久久久久久久| 尤物精品国产第一福利三区| 国产91精品青草社区| 亚洲欧洲国产伦综合| 亚洲精品黄网在线观看| 国产精品99蜜臀久久不卡二区| 九九热视频这里只有精品| 亚洲欧美一区二区三区情侣bbw| 日韩欧美国产高清91| 日韩精品中文字幕在线|