我正在编写一个应用程序,我需要在其中显示来自url的图像。现在假设我要展示100张图片,一张接一张,每张图片都需要从网上下载。
我使用gallery视图来显示图像,但问题是当我将url传递到getView函数时,它开始下载所有100个图像(仅在异步任务中是),但下载100个图像会减慢系统速度。我想要实现的是,当我向右移动时,我的程序应该选择URL并从Internet下载它。

public class ViewImage extends Activity {

private String albumID;
private ArrayList<MediaBO>galleryList;
private Context context;
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.localgallery);

    this.context = this;
    Intent intent = getIntent();
    Bundle extras = intent.getExtras();
    albumID = extras.getString("albumID");
    int position = extras.getInt("position"); //position of the image selected

    DataBaseOperation dataBaseOperation = new DataBaseOperation(this);
    galleryList = dataBaseOperation.queryAllPhoto(albumID); //this returns the list of Photos needs to be shown.it has the URL of the photos

    Gallery g = (Gallery) findViewById(R.id.localGallery);
    g.setAdapter(new ImageAdapter(this));
    g.setSelection(position);

}
public class ImageAdapter extends BaseAdapter{
    private Context mContext;
    int mGalleryItemBackground;
    public ImageAdapter(Context c) {
        mContext = c;
    }
    public int getCount(){
        return galleryList.size();
    }
    public Object getItem(int position){
        return position;
    }
    public long getItemId(int position) {
        return position;
    }
    public View getView(int position, View convertView, ViewGroup parent){

        ImageView i = new ImageView(mContext);

        View v = convertView;
        if (v == null) {
            LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(R.layout.localgallery, null);
        }
        String url = galleryList.get(position).getUrl();

        DOWNLOAD()//download the photo using Async Task
        v = i;
        return v;
    }

}
   }

现在这里的问题是,一旦为getview中存在的所有url加载活动,就会调用GalleyList,这会减慢系统的速度
@辛格斯:我想用你的答案。但现在确定如何使用这个..
我按照你的建议创建了executor对象
    Executor e = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE,
            TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

但是我无法找到如何使用它来调用异步任务(bitmapdownloadertask)
    BitmapDownloaderTask task = new BitmapDownloaderTask(con,showDialog,i,path);
    task.execute(url,null,null);

我的任务是
    private class BitmapDownloaderTask extends AsyncTask<String, Void, Bitmap> {
    }

最佳答案

asynctask的默认Executor是使用ThreadPoolExecutorLinkedBlockingQueue,容量限制为10个项。这意味着,一旦队列未处理的队列达到10个项目,Executor将开始添加线程,因为它无法向工作队列添加更多项目。如果您想防止这种情况,可以创建自己的Executor

// same values as the default implementation in AsyncTask
int CORE_POOL_SIZE = 5;
int MAX_POOL_SIZE = 128;
int KEEP_ALIVE = 1;

Executor e = new ThreadPoolExecutor(CORE_POOL_SIZE, MAX_POOL_SIZE, KEEP_ALIVE,
        TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>());

然后您可以调用AsyncTask.executeOnExecutor()。这将使用一个无界队列,这可能不是您想要的,也不适合所有可能的用例,但是它应该将工作线程的数量保持在5,并限制您看到的速度减慢。

关于android - Android在图库中显示图像,需要从Internet下载,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/6561202/

10-10 23:34