精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-11-23
之前发表过一篇文章:
又优化了一下 Android ListView 异步加载图片 package cindy.android.util; import android.os.Handler; import android.os.Looper; import android.os.Message; public class RunInOtherThread { private static final String LOG_TAG = "RunInOtherThread"; private LooperThread localThread = new LooperThread(); private boolean isRunning = true; public Handler getHandler(){ return localThread.getHandler(); } private class LooperThread extends Thread { private Handler mHandler; public void run() { Looper.prepare(); mHandler = new Handler() { public void handleMessage(Message msg) { onReceiveMessage(msg.what); } }; Looper.loop(); } Handler getHandler(){ return mHandler; } } public void start(){ localThread.start(); } public void quit(){ localThread.getHandler().getLooper().quit(); } public void sendMessage(int what){ getHandler().sendEmptyMessage(what); } public Thread getThread(){ return localThread; } public void onReceiveMessage(int what){}; }
package cindy.android.util; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.lang.ref.SoftReference; import java.net.URL; import java.util.HashMap; import cindy.android.debug.DebugUtil; import android.graphics.drawable.Drawable; import android.os.Environment; import android.os.Handler; public class SyncImageLoader { private Object lock = new Object(); private boolean mAllowLoad = true; private boolean firstLoad = true; private int mStartLoadLimit = 0; private int mStopLoadLimit = 0; final Handler handler = new Handler(); private HashMap<String, SoftReference<Drawable>> imageCache = new HashMap<String, SoftReference<Drawable>>(); RunInOtherThread runInOutherThread; public SyncImageLoader() { super(); runInOutherThread = new RunInOtherThread(); runInOutherThread.start(); } public interface OnImageLoadListener { public void onImageLoad(Integer t, Drawable drawable); public void onError(Integer t); } public void setLoadLimit(int startLoadLimit, int stopLoadLimit) { if (startLoadLimit > stopLoadLimit) { return; } mStartLoadLimit = startLoadLimit; mStopLoadLimit = stopLoadLimit; } public void restore() { mAllowLoad = true; firstLoad = true; } public void lock() { mAllowLoad = false; firstLoad = false; } public void unlock() { mAllowLoad = true; synchronized (lock) { lock.notifyAll(); } } public void loadImage(Integer t, String imageUrl, OnImageLoadListener listener) { final OnImageLoadListener mListener = listener; final String mImageUrl = imageUrl; final Integer mt = t; runInOutherThread.getHandler().post(new Runnable() { @Override public void run() { if (!mAllowLoad) { synchronized (lock) { try { DebugUtil.debug("wait start....."); lock.wait(); DebugUtil.debug("wait end....."); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } if (mAllowLoad && firstLoad) { loadImage(mImageUrl, mt, mListener); } if (mAllowLoad && mt <= mStopLoadLimit && mt >= mStartLoadLimit) { loadImage(mImageUrl, mt, mListener); } } }); } private void loadImage(final String mImageUrl, final Integer mt, final OnImageLoadListener mListener) { if (imageCache.containsKey(mImageUrl)) { SoftReference<Drawable> softReference = imageCache.get(mImageUrl); final Drawable d = softReference.get(); if (d != null) { handler.post(new Runnable() { @Override public void run() { if (mAllowLoad) { mListener.onImageLoad(mt, d); } } }); return; } } try { final Drawable d = loadImageFromUrl(mImageUrl); if (d != null) { imageCache.put(mImageUrl, new SoftReference<Drawable>(d)); } handler.post(new Runnable() { @Override public void run() { if (mAllowLoad) { mListener.onImageLoad(mt, d); } } }); } catch (IOException e) { handler.post(new Runnable() { @Override public void run() { mListener.onError(mt); } }); e.printStackTrace(); } } public static Drawable loadImageFromUrl(String url) throws IOException { //DebugUtil.debug(url); if (Environment.getExternalStorageState().equals( Environment.MEDIA_MOUNTED)) { File f = new File(Environment.getExternalStorageDirectory() + "/TestSyncListView/" + MD5.getMD5(url)); if (f.exists()) { FileInputStream fis = new FileInputStream(f); Drawable d = Drawable.createFromStream(fis, "src"); return d; } URL m = new URL(url); InputStream i = (InputStream) m.getContent(); DataInputStream in = new DataInputStream(i); FileOutputStream out = new FileOutputStream(f); byte[] buffer = new byte[1024]; int byteread = 0; while ((byteread = in.read(buffer)) != -1) { out.write(buffer, 0, byteread); } in.close(); out.close(); Drawable d = Drawable.createFromStream(i, "src"); return loadImageFromUrl(url); } else { URL m = new URL(url); InputStream i = (InputStream) m.getContent(); Drawable d = Drawable.createFromStream(i, "src"); return d; } } } 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2012-11-30
感谢楼主的奉献精神!
为什么这么多浏览量,却没有一个回复呢 |
|
返回顶楼 | |
发表时间:2012-12-03
vicky517 写道 感谢楼主的奉献精神!
为什么这么多浏览量,却没有一个回复呢 游客居多吧。 |
|
返回顶楼 | |
发表时间:2012-12-11
支持一下,感慨楼主的共享精神
|
|
返回顶楼 | |
发表时间:2012-12-17
我也来填块砖吧
|
|
返回顶楼 | |
发表时间:2012-12-18
好贴必回!支持楼主的分享精神!
|
|
返回顶楼 | |
发表时间:2013-01-16
01-16 06:11:07.822: E/dalvikvm(475): Could not find class 'cindy.android.test.synclistview.RunInOtherThread$LooperThread', referenced from method cindy.android.test.synclistview.RunInOtherThread.<init>
我替换了楼主的改进方案之后运行时出现了如上的错误。 |
|
返回顶楼 | |
发表时间:2013-01-16
maomao19861109 写道 01-16 06:11:07.822: E/dalvikvm(475): Could not find class 'cindy.android.test.synclistview.RunInOtherThread$LooperThread', referenced from method cindy.android.test.synclistview.RunInOtherThread.<init>
我替换了楼主的改进方案之后运行时出现了如上的错误。 clean一下程序啊,只要不报错就会重新编译的。 |
|
返回顶楼 | |
发表时间:2013-07-03
发帖规则的绝对bug
正确填写提交后还要继续填单,这点跳转功能都没。。。 |
|
返回顶楼 | |
发表时间:2013-08-06
感谢楼主,对这块还是懵懵懂懂的,希望能够向楼主多多请教,求联系方式。QQ:378528284
|
|
返回顶楼 | |