gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:55:23 25 4
gpt4 key购买 nike

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

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;
}

}

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

最佳答案

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

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

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

关于java - 位图在 Android 应用程序中加载和显示需要很长时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23957951/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com