【问题标题】:How to load a high resolution Bitmap in Android?如何在 Android 中加载高分辨率位图?
【发布时间】:2014-03-02 16:28:45
【问题描述】:

我正在开发一个 android 应用程序,在资源文件夹中我有一个分辨率为 8000x400px 的图像。这是我在Sprite 课程中使用的.png 来模拟动物的运动。

我在SurfaceView 类中使用drawBitmap() 显示png 的部分。

Sprite 类、SurfaceView 和所有元素都可以完美运行,但是在处理大图像时它不会显示任何内容。

为了解决这个问题,我想知道。

  1. Bitmap 的最大分辨率限制是多少? 安卓?
  2. 我如何在onDraw() 中显示具有这种大小的 Sprite?

【问题讨论】:

    标签: android bitmap size resolution


    【解决方案1】:

    关于位图的显示/加载:

    您需要正确加载Bitmap 并根据您的需要调整Bitmap 的大小。在大多数情况下,加载比设备屏幕支持的分辨率更高的位图是没有意义的。

    此外,这种做法非常重要,以避免 OutOfMemoryErrors 在使用这么大的 Bitmaps 时。

    例如,大小为 8000 x 4000 的位图使用超过 100 兆字节 的 RAM(32 位颜色),这对于移动设备来说是一个巨大的数量,远远超过即使是高端设备也能应付。

    这是正确加载位图的方法:

    public abstract class BitmapResLoader {
    
        public static Bitmap decodeBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
    
            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeResource(res, resId, options);
    
            // Calculate inSampleSize
            options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return BitmapFactory.decodeResource(res, resId, options);
        }
    
        private static int calculateInSampleSize(
                    BitmapFactory.Options options, int reqWidth, int reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
    
            if (height > reqHeight || width > reqWidth) {
    
                // Calculate ratios of height and width to requested height and width
                final int heightRatio = Math.round((float) height / (float) reqHeight);
                final int widthRatio = Math.round((float) width / (float) reqWidth);
    
                // Choose the smallest ratio as inSampleSize value, this will guarantee
                // a final image with both dimensions larger than or equal to the
                // requested height and width.
                inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
            }
    
            return inSampleSize;
        }
    }
    

    代码中的示例用法:

    Bitmap b = BitmapResLoader.decodeBitmapFromResource(getResources(),
                                                          R.drawable.mybitmap, 500, 500);
    

    摘自此处的 Google Android 开发者指南: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

    关于最大位图尺寸:

    最大位图大小限制取决于底层的 OpenGL 实现。使用 OpenGL 时,可以通过(来源:Android : Maximum allowed width & height of bitmap)进行测试:

    int[] maxSize = new int[1];
    gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, maxSize, 0);
    

    例如对于 Galaxy S2,它是 2048x2048。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 2013-08-14
      • 2017-08-23
      • 2015-03-12
      • 2013-06-08
      • 2016-10-13
      相关资源
      最近更新 更多