1

I'm working on an android app, and in resources folder I have an image which is 8000x400px resolution. It is a .png that I'm using at my Sprite class to simulate the movement of an animal.

I display the portion of the png using drawBitmap() in my SurfaceView class.

Sprite class, SurfaceView and all the elements work perfect, but when working with that large images it doesnt display anything.

To fix this problem I would like to know.

  1. What is the limit of maximum resolution of a Bitmap allowed in Android?
  2. How could I display in onDraw() a Sprite with that size?
Master
  • 2,945
  • 5
  • 34
  • 65

1 Answers1

2

Concerning the displaying / loading of Bitmaps:

You need to load the Bitmap properly and adjust the size of the Bitmap to your needs. In most cases, it makes no sense to load a Bitmap with higher resolution than the screen of the device supports.

Furthermore, this practice is very important to avoid OutOfMemoryErrors when working with Bitmaps that large.

As an example, a Bitmap with the size of 8000 x 4000 uses more than 100 Megabytes of RAM (in 32 Bit color), which is an enormous amount for a mobile device and much more than even high end devices are capable of handling.

This is how to load a Bitmap properly:

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;
    }
}

Example usage in code:

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

Taken from the Google Android Developer guidelines here: http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

Concerning the maximum Bitmap size:

The maximum Bitmap size limit depends on the unterlying OpenGL implementation. When using OpenGL, this can be tested via (source: Android : Maximum allowed width & height of bitmap):

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

e.g. for the Galaxy S2, it is 2048x2048.

Community
  • 1
  • 1
Philipp Jahoda
  • 50,880
  • 24
  • 180
  • 187