首页 > 解决方案 > 如何减少获取位图所需的时间

问题描述

我在 Android 和 iOS 上有一个项目,有一个部分显示汽车的 360 度视图,在 iOS 中,我只从内部存储中获取 60 张图像,这些图像之前从互联网下载,这些图像在用户显示时显示开始向右或向左滑动汽车。这个过程只需要 200 毫秒就可以准备好与用户交互。

但在Android中我必须先将60张图像转换成位图,然后这个过程类似于iOS的过程。然而,这第一个过程,将它们全部转换为位图需要大约 3 秒或更多。我已经阅读了在许多信息源中在 Android 中显示图像的过程,并且似乎有必要为此任务使用位图,因此可以减少获取所有位图的时间吗?或者我可以保存位图以避免转换当用户再次打开相同的视图时,PNG 文件再次转换为新的位图?

我用来将 png 文件转换为位图对象的代码与 Android 开发人员文档推荐的代码相同:

https://developer.android.com/topic/performance/graphics/load-bitmap

我获取位图的源代码是:

   public static Bitmap decodeSampledBitmapFromFile(String filePath, int reqWidth, int reqHeight) {
    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(filePath, options);
    options.inSampleSize = 1;
    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap bitmap =BitmapFactory.decodeFile(filePath, options);
    Log.d("img360","bitmap res "+bitmap.getWidth()+ " h "+bitmap.getHeight());
    return bitmap;
}

我真的很感谢你的帮助和支持。

标签: androidperformancebitmapstorage

解决方案


如果你想从互联网上下载图片并在android上显示只需放一个imageview并使用picaso或glide库它更快更容易


推荐阅读