在 Android Honeycomb 中加速 Gallery 小部件的技巧
我正在寻找一种好方法来加速 Android Honeycomb 中的图库视图小部件。我目前正在使用它来显示一些大约 340 x 600 像素的相当大的图像,并且我希望它在滚动图像时能够像黄油一样光滑。
目前它的速度相当快,但它无法与使用 ImageView 加载 ScrollView 并滚动它相比。
这是我的自定义 BaseAdapter 中的 getView()
方法的简化版本:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = (ImageView) new ImageView(Main.this);
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
((ImageView) convertView).setImageBitmap(createReflection(BitmapFactory.decodeFile(ImageFile, options)));
convertView.setPadding(20, 0, 20, 0);
return convertView;
}
我一直在尝试延迟加载图像,但我不太喜欢结果。
I'm looking for a great way to speed up the Gallery view widget in Android Honeycomb. I'm currently using it to display some fairly large images at roughly 340 x 600 pixels, and I'd love for it to be smooth as butter when scrolling through the images.
It's fairly speedy at the moment, but it doesn't compare to loading a ScrollView with ImageViews and scrolling through that.
Here's a simplified version of my getView()
method from my custom BaseAdapter:
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = (ImageView) new ImageView(Main.this);
}
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
((ImageView) convertView).setImageBitmap(createReflection(BitmapFactory.decodeFile(ImageFile, options)));
convertView.setPadding(20, 0, 20, 0);
return convertView;
}
I've been experimenting with lazy loading the images, but I didn't really like the result.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用
Gallery
和以下内容之间的区别:在
ScrollView
场景中,您将预加载所有图像,而不是像在Gallery< 中那样动态加载它们/代码> 场景。
如果您的图像数量很少,并且您有足够的 RAM 来支持所有图像,那么只需使用
ScrollView
即可。除此之外,据我所知,你无能为力。您可以维护一个位图缓存,在其中继续解码
Gallery
中当前图像之前的一些图像,并从缓存中提取您的Adapter
。然而,这只能让你到目前为止——小滚动会很平滑,但超过你的缓存容量仍然会导致按需完成解码。这几乎是不可避免的。The difference between using a
Gallery
and:is that with the
ScrollView
scenario, you are pre-loading all of the images, rather than loading them on the fly as you are in theGallery
scenario.If your number of images is small, and you have enough RAM to support all of them, then just use your
ScrollView
.Beyond that, AFAIK there's not a ton you can do. You can maintain a bitmap cache where you continue decoding a few images ahead of the current ones in the
Gallery
and have yourAdapter
pull from the cache. However, that will only get you so far -- small scrolls will be smooth, but flings past your cache capacity will still result in the decoding being done on demand. That's pretty much unavoidable.Gallery暂时不支持convertView。对于convertView,您将始终收到null。这是一个已知问题,预计无法修复。
Gallery does not support the convertView at the moment. You will always receive null for convertView. This is a known issue and there's no ETA for a fix.
打开应用程序的硬件加速将会产生重大影响(至少在我的示例应用程序中如此)。
在 Android 清单的应用程序元素中添加 android:hardwareAccelerated="true"
http://developer.android.com/guide/topics/manifest /application-element.html#hwaccel
Turning on Hardware acceleration of your application will have a significant impact (at least had it on my example app).
Add android:hardwareAccelerated="true" in the application element of your android manifest
http://developer.android.com/guide/topics/manifest/application-element.html#hwaccel
文件 IO 是减慢图库视图速度的因素之一。
我正在开发一个幻灯片应用程序,其中有 1280x720 分辨率的照片。
每个文件的实际文件 I/O 需要 300-400 毫秒。
由于文件 I/O 通常在 UI 线程上运行,因此这将在任何正在进行的照片转换中导致非常明显的“凹凸感”。
为了避免这种情况,您应该:
imageView.setImageResource(R.drawable.my_loading_drawable);
imageView.setImageDrawable(drawable);
PS如果用户翻阅多张图片,上述方法通常会触发多个并行异步任务,这些任务都将使用文件 I/O。不利于性能并且可能会使您的应用程序崩溃。您可能应该采用更结构化的方法,一次只允许一个异步任务。
One of the elements which is slowing your Gallery view down is file IO.
I'm working on a slideshow app for which I have photos in 1280x720 resolution.
The actual file I/O takes 300-400 ms for each file.
Since the File I/O would normally run on the UI thread, this will cause a very visible "hick" in any ongoing photo transition.
In order to avoid this, you should:
imageView.setImageResource(R.drawable.my_loading_drawable);
imageView.setImageDrawable(drawable);
PS if the user flicks through multiple pictures, the above approach will typically trigger multiple parallell asynctask which all will be using file I/O. Not good for performance and may crash your app. You should probably have more structured approach where you only allow one async task at a time.