【问题标题】:Bitmap is taking long to load and display in Android app位图在 Android 应用程序中加载和显示需要很长时间
【发布时间】:2014-05-30 15:18:14
【问题描述】:

现在我有一个LocationAdapter 类,它接收我的"Location" 对象并将每个Location 显示到ListView 中。我正在尝试将它传递给 Bitmap 图像,并让它在 ListView 行中显示该图像。它可以工作,但图像需要一段时间才能加载,即使在加载行中的文本之后也是如此。我正在实际设备上进行测试。这是我的活动代码:

public class MainActivity extends ActionBarActivity {

ArrayList<Location> arrayOfLocations;
LocationAdapter adapter;
private static Bitmap bitmap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    // Construct the data source
    arrayOfLocations = new ArrayList<Location>();

    // Create the adapter to convert the array to views
    adapter = new LocationAdapter(this, arrayOfLocations);

    Bitmap image = getBitmapFromID(1);

    adapter.add(new Location(image, "Place 1",
            "We have the freshest fruit in the whole world!", "2 miles",
            "8-5 mon-sat\nclosed sun"));
    adapter.add(new Location(image, "Place 2",
            "We have the freshest fruit in the whole world!", "2 miles",
            "8-5 mon-sat"));
    adapter.add(new Location(image, "Place 3",
            "We have the best cakes in the whole world!", "2 miles",
            "8-5 mon-fri\n10-1 sat\nclosed sun\nclosed Memorial Day"));
    adapter.add(new Location(image, "Fruit Stand",
            "Best place!", "2 miles",
            "8-5 mon-sat"));
    adapter.add(new Location(image, "Place 4",
            "Food!", "2 miles",
            "8-5 mon-sat"));
    adapter.add(new Location(image, "Place 5",
            "Toys!", "2 miles",
            "8-5 mon-sat"));

    // Attach the adapter to a ListView
    ListView listView = (ListView) findViewById(R.id.listView1);

    View header = (View) getLayoutInflater().inflate(
            R.layout.listview_header, null);
    listView.addHeaderView(header);

    listView.setAdapter(adapter);

}

public static Bitmap getBitmapFromID(int id) {

    //final Bitmap bitmap;

    Thread thread = new Thread(new Runnable() {
        @Override
        public void run() {

            try {
                bitmap = BitmapFactory
                        .decodeStream((InputStream) new URL(
                                "http://icons.iconarchive.com/icons/yellowicon/game-stars/256/Mario-icon.png")
                                .getContent());

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    });

    thread.start();
    return bitmap;
}

}

我做错了什么吗?加载需要一段时间正常吗?谢谢!

【问题讨论】:

    标签: java android listview bitmap


    【解决方案1】:

    您需要在后台线程中对您的工作执行 AsyncTask。然而,为了更好的方法,请看这里:

    https://stackoverflow.com/a/23941258/276949

    Android Volley 是来自 Google 的一个相当新的非详细库,它非常适合加载图像,并负责自动处理单独线程上的所有下载。

    【讨论】:

    • AsyncTask 是我采用的方式,现在可以使用。谢谢!
    【解决方案2】:

    位图的加载可能需要一段时间,您可以在此处阅读:Android doc。但是,您需要同步您的线程,因为在您调用 start() 后,该方法会立即返回。您需要从您的线程中收到“嘿,我完成了”,然后加载位图。

    【讨论】:

    • 好的,你是说我需要等到线程完成后才能返回位图?
    • 是的,你需要等待。
    • 您需要等待,但您不能在 UI 线程中等待,例如,您可以使用带有不确定微调器的警报对话框,直到线程结束或在适配器中使用临时图像,完成后更改图像并调用 notifyOnDataSetChange()。
    【解决方案3】:

    您能否将 Mario_icon.png 放置在您的应用程序内部,而不是从 URL 访问?这样一来,应用程序检索图像所需的时间就会减少,并且允许应用程序需要更少的权限

    当您需要连接到 URL 时,加载图像需要更长的时间。

    【讨论】:

    • 应用程序将访问一个数据库,该数据库通常会添加新的图像/位置。所以我不能将它们存储在应用程序中
    猜你喜欢
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    相关资源
    最近更新 更多