1

在我的应用程序中,我从我的数据库中加载一些 URL 并将它们存储ArrayList<Bitmap>在一个库中。它工作得很好,但速度很慢。问题是我ArrayList在显示图像之前加载了所有位图。

        try
        {
            JSONArray jArray = new JSONArray(result);
            for(int i=0;i<jArray.length();i++)
            {
                Temp = jArray.get(i).toString();

                HttpURLConnection connection = null;
                try 
                {
                    connection = (HttpURLConnection) new URL(Temp).openConnection();
                } 
                catch (MalformedURLException e) 
                {
                    e.printStackTrace();
                } 
                catch (IOException e) 
                {
                    e.printStackTrace();
                }
                connection.connect();
                InputStream input = connection.getInputStream();

                x = BitmapFactory.decodeStream(input);
                bitmapArray.add(x);
            }
        }
        return bitmapArray; // returning the array list with all the bitmaps inside

    protected void onPostExecute(ArrayList<Bitmap> donnees)
    {
        SetupInterface(); // This method sends the arraylist to an image adatper and display bitmaps to the screen.
    }

我的问题是SetupInterface()仅在数组已满并返回时才调用它。所以当我打开我的活动时,显示画廊大约需要 4-5 秒,因为在显示之前必须加载数组中的所有位图。如何更快地做到这一点?

4

2 回答 2

0

下载图像和解码图像需要时间。您已经知道 jArray 的内容(至少是这些图像的长度、名称......)。您应该将带有默认背景图像的数组直接发送到图像适配器,并使用 AsyncTask 一张一张地加载这些实际图像。

于 2013-01-16T23:35:45.833 回答
0

在活动开始时加载并仅显示第一张图像。在显示您的活动并完成所有初始化工作后,您将加载其余图像。你可以:

  1. 使用例如HandlerAsyncTask在后台线程中一一加载它们。第二个将更容易实现。
  2. 加载下一个图像,当它必须被加载时,例如用户点击“next”。它被称为延迟加载。
于 2013-01-16T23:35:54.940 回答