我正在开发一款适用于Android的音乐播放器应用程序,并试图找到处理图像的最佳方式。我正在加载来自网络的艺术家图像,以及来自MediaStore或网络的专辑图像,具体取决于具体情况。我需要在不同的时间向listViews添加任意一组图像(艺术家或专辑)。
将图像存储在内存(HashTable)中会使加载速度非常快,但如果用户拥有非常大的音乐库,则可能会变得不切实际。
但是我无法在“按需”的基础上加载它们,因为我需要将它们绑定到listView。另外,我有一个通知,显示艺术家图像并提供导航控件。如果我等待用户单击下一个按钮时加载图像,则可能会在通知视图更新之前加载它。
是第一次加载时连接到网络的最佳方法,然后在本地保存文件,然后根据需要加载它们?如果我这样做,图像是否会足够快地加载以跟上用户快速点击下一个按钮并刷新艺术家图像视图?
谢谢,
答案 0 :(得分:1)
使用listview或gridview显示图像。
您可以使用https://github.com/nostra13/Android-Universal-Image-Loader Universal Image Loader从MediaStore或网络加载图片。使用缓存。基于延迟加载但有更多配置选项。
在你的适配器构造函数
中File cacheDir = StorageUtils.getOwnCacheDirectory(a, "UniversalImageLoader/Cache");
// Get singletone instance of ImageLoader
imageLoader = ImageLoader.getInstance();
// Create configuration for ImageLoader (all options are optional)
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a)
// You can pass your own memory cache implementation
.discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation
.discCacheFileNameGenerator(new HashCodeFileNameGenerator())
.enableLogging()
.build();
// Initialize ImageLoader with created configuration. Do it once.
imageLoader.init(config);
options = new DisplayImageOptions.Builder()
.showStubImage(R.drawable.stub_id)//dummy image
.cacheInMemory()
.cacheOnDisc()
.displayer(new RoundedBitmapDisplayer(20))
.build();
在你的getView()
中 ImageView image=(ImageView)vi.findViewById(R.id.imageview);
imageLoader.displayImage(imageurl, image,options);
您也可以使用https://github.com/thest1/LazyList。也使用缓存。
还可以在listview或grdiview中使用ViewHolder。 http://developer.android.com/training/improving-layouts/smooth-scrolling.html
答案 1 :(得分:0)
从MediaStore
getView()
或ListView's
的{{1}}功能中加载来自本地文件存储或ListAdapter
的图片。只有在SimpleListAdapter
所需的项目需要时,适配器才会调用getView()
。您的系统将足够快,以便在需要时以这种方式加载图像。
您必须在较早时间从网络上获取所有图像并将其存储在本地文件系统中,以便position
显示它们。