在 Android Honeycomb 中加速 Gallery 小部件的技巧

发布于 2024-11-28 09:56:09 字数 778 浏览 6 评论 0原文

我正在寻找一种好方法来加速 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浮世清欢 2024-12-05 09:56:09

使用 Gallery 和以下内容之间的区别:

使用 ImageView 加载 ScrollView 并滚动它

ScrollView 场景中,您将预加载所有图像,而不是像在 Gallery< 中那样动态加载它们/代码> 场景。

如果您的图像数量很少,并且您有足够的 RAM 来支持所有图像,那么只需使用 ScrollView 即可。

除此之外,据我所知,你无能为力。您可以维护一个位图缓存,在其中继续解码 Gallery 中当前图像之前的一些图像,并从缓存中提取您的 Adapter。然而,这只能让你到目前为止——小滚动会很平滑,但超过你的缓存容量仍然会导致按需完成解码。这几乎是不可避免的。

The difference between using a Gallery and:

loading a ScrollView with ImageViews and scrolling through that

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 the Gallery 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 your Adapter 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.

深海夜未眠 2024-12-05 09:56:09

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.

寂寞花火° 2024-12-05 09:56:09

打开应用程序的硬件加速将会产生重大影响(至少在我的示例应用程序中如此)。

在 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

金橙橙 2024-12-05 09:56:09

文件 IO 是减慢图库视图速度的因素之一。
我正在开发一个幻灯片应用程序,其中有 1280x720 分辨率的照片。
每个文件的实际文件 I/O 需要 300-400 毫秒。

由于文件 I/O 通常在 UI 线程上运行,因此这将在任何正在进行的照片转换中导致非常明显的“凹凸感”。

为了避免这种情况,您应该:

  1. 设置一个已经缓存的临时加载可绘制对象
    imageView.setImageResource(R.drawable.my_loading_drawable);
  2. 创建一个 AsyncTask 其中
    • 在 doInBackground 中加载可绘制对象(即文件 I/O)
    • 在 onPostExecute 中更新 imageView
      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:

  1. Set a temporary loading drawable that is already cached
    imageView.setImageResource(R.drawable.my_loading_drawable);
  2. Create an AsyncTask which
    • loads the drawable in doInBackground(i.e. the File I/O)
    • Updates the imageView in onPostExecute
      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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文