异步加载图像的问题
我有一个使用来自 Web aa 源的图像的应用程序。对于我的应用程序,我首先在单独的线程中下载图像,并且仅当图像存在于磁盘上时我才会显示在列表中。下载器是一个单独的线程,但我对线程的使用非常陌生,现在还有其他事情让我感到困惑。除非刷新视图(这是我的列表),否则我的图像不会显示。现在我需要显示我的图像,所以我在自定义视图中写了这个:
private Runnable LoaderTask=new Runnable(){
public void run() {
if(cellIcon!=null){
if(iconCache.get(cellIcon)!=null){
icon.setImageBitmap(iconCache.get(cellIcon));
mHandler.postDelayed(this, 200);
}
}
}
};
private class ImageThread extends Thread{
@Override
public void run() {
// mHandler.removeCallbacks(LoaderTask);
mHandler.postDelayed(LoaderTask, 100);
}
}
我像这样触发我的线程,
ImageThread thread=new ImageThread();
thread.start();
处理程序在类的开头实例化。但当我这样做时,除非我刷新我的观点,否则什么都不会发生。我在我的一个线程中尝试了 (Activity).runOnUIThread 我收到了 stackoverflow 错误。我该如何解决这个问题或者还有其他建议吗?你可以给我吗?谢谢
I have an application that uses images from web a a source. for my application I'm first downloading images in seperate thread and only if image exists on disk I show on the list. Downloader is a seperate thread, but I'm very new using threads and now something else is confusing me. My images are not shown unless I refresh the view, that is my list. Now I needed to show my images so I wrote this in my custom view :
private Runnable LoaderTask=new Runnable(){
public void run() {
if(cellIcon!=null){
if(iconCache.get(cellIcon)!=null){
icon.setImageBitmap(iconCache.get(cellIcon));
mHandler.postDelayed(this, 200);
}
}
}
};
private class ImageThread extends Thread{
@Override
public void run() {
// mHandler.removeCallbacks(LoaderTask);
mHandler.postDelayed(LoaderTask, 100);
}
}
And I trigger my thread like this
ImageThread thread=new ImageThread();
thread.start();
the handler is instantiated in the beginning of the class. But when I do this n othing happen until I refresh my views. I tried (Activity).runOnUIThread inside one of my threads I got stackoverflow error. How do I solve that or are there any other advices.you can give me? Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用异步任务。在 onPostExecute() 方法中,只需从活动中调用一个方法并刷新 UI。当线程完成时,将调用 onPostExecute()。
http://developer.android.com/resources/articles/painless-threading.html
PS 要调用活动的方法只需使用接口。
Use AsyncTask. In onPostExecute() method just call a method from the activity and refresh the UI. The onPostExecute() is called when the thread is done.
http://developer.android.com/resources/articles/painless-threading.html
P.S. To call an activity's method just use Interface.
看看这个:
http://android-developers.blogspot .com/2010/07/multithreading-for-performance.html
Have a look at this:
http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html