Android:Gallery Widget 性能很糟糕,有什么原因吗?

标签 android android-gallery

我有一个 Android Gallery 小部件,它显示几个带有来自网络的图片的 ImageView。图像以 jpg 格式存储,并在添加到 ImageView 之前转换为位图。每个 jpg 为 8kb。我没有对图像进行任何缩放。

当我使用包含 1 或 2 张图片的图库时,它工作正常,滚动流畅等。使用 3,它开始变得有点不稳定,当包含 5 或 10 张图片时,应用程序几乎无法使用。

有人知道为什么性能如此糟糕吗?有人对替代方案有建议吗?谢谢-

@elevine:我从 jpg url 构造位图的方法:

private Bitmap getBitmapFromURL(String input) throws IOException {
        URL url = new URL(input);
        URLConnection conn = url.openConnection();
        conn.connect();
        InputStream is = conn.getInputStream();
        BufferedInputStream bis = new BufferedInputStream(is);
        Bitmap bm = BitmapFactory.decodeStream(bis);
        bis.close();
        is.close();
        return bm;
    }

这是我的 ImageAdapter 的 getView 方法。我开始怀疑这就是我的问题所在……我是不是抓取图像的次数太多了?感谢您的帮助!

public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imageView = new ImageView(mContext);

            Bitmap bm;
            try {
                imageView.setImageBitmap(getBitmapFromURL(urls.get(position)));
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

            //mageView.
            imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
            imageView.setLayoutParams(new Gallery.LayoutParams(100,100));
            return imageView;
        }

最佳答案

如果您从 Activity 调用 getBitmapFromURL,那么它可能会阻塞 UI 线程。确保此代码在单独的线程上或在 AsyncTask 中运行。您可能还想将位图缓存在 WeakHashmap 中。在从网络抓取图像之前检查缓存中的图像。

关于Android:Gallery Widget 性能很糟糕,有什么原因吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7128082/

相关文章:

android - 如何创建圆形画廊 View /图像切换器/ ImageView ...?

android - 如何解决 kitkat 及更高版本图库中任何照片的路径问题?

android - 在本地登录 Tigase 服务器设置时 Smack API 出错

android - 在 Eclipse 中构建 Android 项目时构建 C++ 共享对象

Android - 图库应用程序 NullPointerException - OnePlus One

android - 将图片上传到模拟器库

android - Flutter AndroidManifest 错误

android - Exoplayer 2.9.4 平滑流

android - 当我单击按钮时,无论我如何实现 onClick,我的应用程序都会崩溃

android - Android中如何将位图图片保存到Drawable文件夹中