异步加载图像的问题

时间:2011-07-13 12:37:00

标签: android multithreading synchronization handler

我有一个应用程序,它使用来自Web的图像作为源。对于我的应用程序我首先在单独的线程中下载图像,并且只有当我在列表上显示的磁盘上存在图像时。 Downloader是一个单独的线程,但我是非常新的使用线程,现在其他东西让我感到困惑。除非我刷新视图,否则我的图像不会显示,这是我的列表。现在我需要显示我的图像,所以我在自定义视图中写了这个:

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错误。我如何解决这个问题或者是否有任何其他建议。你可以给我吗?感谢

2 个答案:

答案 0 :(得分:1)

使用AsyncTask。在onPostExecute()方法中,只需从活动中调用一个方法并刷新UI。线程完成后调用onPostExecute()。

http://developer.android.com/resources/articles/painless-threading.html

P.S。要调用活动的方法,只需使用Interface。

答案 1 :(得分:0)

相关问题