2

I am using an image of size 720X1136 as splash screen for my app targeted only for Samsung Galaxy Nexus phones. The actual file size is 513Kb(Found from Browser). When the activity calls the onCreate method and sets the content view the log mentions about a memory allocation of 13.5 MB.

This is the activity which I am loading with an image in background.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:orientation="vertical">

        <ImageView android:src="@drawable/splashscreen"
                android:id="@+id/splashscreen"
               android:layout_width="fill_parent"
               android:layout_height="fill_parent"
               />

        <WebView xmlns:android="http://schemas.android.com/apk/res/android"
                    android:visibility="invisible"
                 android:id="@+id/webview"
                 android:layout_width="fill_parent"
                 android:layout_height="fill_parent"
        />

</RelativeLayout>

How can I reduce this memory allocation?

Thanks

Zach
  • 9,989
  • 19
  • 70
  • 107

5 Answers5

2

Use @drawable/splashscreen this Image as 9-Patch image then it will reduce memory allocation

Ashwani Tyagi
  • 510
  • 1
  • 5
  • 15
1

Here are Step for sort out of your problem.

mRelativeLayout=(RelativeLayout)findViewById(R.id.relative_view);
        intializeScreenSize();

        mBitmap=decodeSampledBitmapFromResource(getResources(),R.drawable.splash_background,width,height);
        mDrawable=new BitmapDrawable(getResources(), mBitmap);
        mRelativeLayout.setBackgroundDrawable(mDrawable);




public static Bitmap decodeSampledBitmapFromResource(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);
    }



    public void intializeScreenSize(){

        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        height= displaymetrics.heightPixels;
        width= displaymetrics.widthPixels;
    }


    public 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) {
            if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
            } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
            }
        }
        return inSampleSize;
    }

Here are some link related to Load Bitmap in android. SO Answer Load Bitmap Efficiently

Community
  • 1
  • 1
Herry
  • 7,037
  • 7
  • 50
  • 80
0

13mb is fine and within the limits. Whenever this splash screen is gone, the bitmap will be recycled and the memory will be repurposed to the rest of your app.

Tricks you could use to try to minimise this memory foot print is:

. use a 9 patch to stretch the borders of the image (if the design allows)

. lower the quality of the image (it will look crap)

Budius
  • 39,391
  • 16
  • 102
  • 144
0

nobody needs pixel perfect splash screen, you may easily reduce the image size to the half of the original, this will save you 3/4 of the memory footprint.

on another note, i'd recommend to avoid splash screens at all, here's a nice essay on this topic: "Splash screens are evil, don’t use them!", otherwise you may search google for "splash screens are evil" and get more arguments against using them.

lenik
  • 23,228
  • 4
  • 34
  • 43
0

As Alex Orlov stated: "the size on disk has nothing to do with the size of bitmap in memory. In the first case the image is compressed, bitmap, on the other hand, is just a raw set of pixels." However even considering it is stored as a raw image, 20MB is stil way too much. If we consider 4B per pixel, that would be 5Mpix image, but that one you have posted is definittely smaller.

I had similar problem: I was loading an image that has 8MB in raw format, however, 32MB were allocated for it. I later found out this was caused by dpi scaling. If you have your image in "drawable" folder, it will be automatically scaled according to current screen dpi. I have an XHDPI screen, so my image was scaled 2x horizontally and 2x vertically, that's why it took 4x more memory.

You can find more about how dpi works here: http://developer.android.com/guide/practices/screens_support.html

If you don't want to use this automatic android feature and scale images yourself, simply rename your "drawable" folder to "drawable-nodpi". All images in "drawable" folder tagged as "nodpi" will be loaded as they are.

Bhavesh Patadiya
  • 25,740
  • 15
  • 81
  • 107