首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Android应用程序中加载和显示位图需要很长时间。

在Android应用程序中加载和显示位图需要很长时间。
EN

Stack Overflow用户
提问于 2014-05-30 15:18:14
回答 3查看 1.2K关注 0票数 0

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

代码语言:javascript
运行
复制
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;
}

}

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-05-30 15:27:01

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

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

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

票数 1
EN

Stack Overflow用户

发布于 2014-05-30 15:23:43

加载位图可能需要一段时间,因为您可以在这里阅读:Android文档。但是,您需要同步线程,因为在调用start()之后,该方法将立即返回。你需要从你的线程中得到一个“嗨,我完成了”,然后加载位图。

票数 0
EN

Stack Overflow用户

发布于 2014-05-30 15:24:12

你能把Mario_icon.png放在你的应用程序中而不是从一个URL中访问吗?这样,应用程序将减少检索图像的时间,并且允许应用程序需要更少的权限。

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23957951

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档