2013-03-07 127 views
0

我正在编写Android中的应用程序,并面临以下问题。在java中与fork()类似的功能

我不得不重写一个内置函数Fragment getItem(int position)来返回图像的位图,但由于图像非常大,需要大量的时间来加载。所以,UI的响应速度有点慢。 要解决这个问题,我认为它可以解决像在下面的代码。它基本上立即返回一个空白图像,当真实图像加载时,它会返回正确的图像。

@Override 
    public Fragment getItem(final int position) { 
     pid = fork(); //NOT POSSIBLE/ERROR 
     if (!pid) 
      return (code for empty image); 
     return new ScreenSlidePageFragment(files[position].getAbsolutePath(), width, height); 
    } 

但是,我不知道类似于Java fork()函数。 您可以请帮我解决这个问题,谢谢

回答

1

在Java中,你会使用线程类为该。但是,如果你正在为Android开发,你可以考虑使用的AsyncTask,并参考了

Processing Bitmaps Off the UI Thread官方Android开发人员的培训。

0

而不是分叉新进程,在Java中你打开这样的情况下的线程。有关如何做到这一点的详细信息,请参阅this question

0

您可以定义一个AsynTask和实施时,他的功能,这样做

public class imageLoader extends AsyncTask<Void, Void, Void>{ 

protected preExecute(){ 
    //load the blank image 
    //this function is executed on the UI thread 
} 

protected doInBackground(){ 
    //load the real one 
    //that one is not executed on the UI thread 
} 

protected postExecute(){ 
    //set the new image 
    //this one is executed on the UI thread 
} 
}