0

I have a small that has few activities and each activity has an image as background.

As each image is pretty big (full HD) it eats up memory fast thus making the whole application sluggish or even crash it due to out of memory exceptions.

How do you deal with something like this?

Alex
  • 5,510
  • 8
  • 35
  • 54

4 Answers4

2

Why are you trying to load a Full HD image as lower android devices won't be able to even load it. And loading a Full HD image will eat up your whole memory leaving the app sluggish as you indicated.

Best Solution would be :

  1. Crop the image using approximate or hit and trail method using any design tool like Photoshop, sketch etc. (Recommended hdpi: 480x800 px)
  2. Then compressing the image without making it lossy. Use TinyPNG best in class.
  3. Finally create a folder named drawable-nodpi in res, if it doesn't already exist. And place the formatted image in drawable-nodpi.

drawable-nodpi has Resources for all densities. These are density-independent resources. The system does not scale resources tagged with this qualifier, regardless of the current screen's density.

Reference :

Community
  • 1
  • 1
Vipul Asri
  • 8,903
  • 3
  • 46
  • 71
0

try to import your photos into your development environment using import functions, eg. Here is how you import an image asset using Android Studio. Right click on Drawable, select new, select image asset, import image, and android will create 5 different images that are optimized for Android Devices, be sure to name your image names with lower cases. best of luck

ianmanda
  • 61
  • 4
0

use images as per the following scaling in android Check this link for more specification

  xxxhdpi: 1280x1920 px
  xxhdpi: 960x1600 px
  xhdpi: 640x960 px
  hdpi: 480x800 px
  mdpi: 320x480 px
  ldpi: 240x320 px
Punit Sharma
  • 2,951
  • 1
  • 20
  • 35
0

There are several ways you can do it.

Option 1:

Create different perfect images for different dpi and place them in related drawable folder. Then set

android:background="@drawable/your_image

option 2:

create a scaled Bitmap which fits your layout..

    /* adapt the image to the size of the display */
    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    Bitmap bmp = Bitmap.createScaledBitmap(BitmapFactory.decodeResource(
      getResources(),R.drawable.background),size.x,size.y,true);

    BitmapDrawable background = new BitmapDrawable(bmp );
   layout.setBackgroundDrawable(background);

Option 3:

dont need to create bitmat image every time. you can put it in Cache memory after first time creation. then try to fetch from Cache if not found then process your drawable image again as a bitmap and put it into Cache and use it. it will be faster process..

there is a link for more detail.. Caching Bitmaps

Tanim reja
  • 2,120
  • 1
  • 16
  • 23