Oliver Dixon
Oliver Dixon

Reputation: 7405

Loading multiple high quality bitmaps and scaling them

I've got a serious performance issue in my app, when loading up bitmaps it seems to take up way to much memory.

I have a drawable folder which contains the bitmap sizes for all android devices, these bitmaps are of high quality. Basically it goes though each bitmap and makes a new one for the device depending on the size. (Decided to do it this way because it supports the correct orientation and any device). It works but it's taking up way to much memory and takes along time to load. Can anyone make any suggestions on the following code.

public Bitmap getBitmapSized(String name, int percentage, int screen_dimention, int frames, int rows, Object params)
{
    if(name != "null")
    {
        _tempInt = _context.getResources().getIdentifier(name, "drawable", _context.getPackageName());
        _tempBitmap = (BitmapFactory.decodeResource(_context.getResources(), _tempInt, _BM_options_temp));
    }
    else
    {
        _tempBitmap = (Bitmap) params;
    }

    _bmWidth = _tempBitmap.getWidth() / frames;
    _bmHeight = _tempBitmap.getHeight() / rows;

    _newWidth = (screen_dimention / 100.0f) * percentage;
    _newHeight = (_newWidth / _bmWidth) * _bmHeight;

    //Round up to closet factor of total frames (Stops juddering within animation)
    _newWidth = _newWidth * frames;

    //Output the created item
    /*
    Log.w(name, "Item");
    Log.w(Integer.toString((int)_newWidth), "new width");
    Log.w(Integer.toString((int)_newHeight), "new height");
    */

    //Create new item and recycle bitmap
    Bitmap newBitmap = Bitmap.createScaledBitmap(_tempBitmap, (int)_newWidth, (int)_newHeight, false);


    _tempBitmap.recycle();

    return newBitmap;
}

Upvotes: 0

Views: 550

Answers (2)

Michell Bak
Michell Bak

Reputation: 13242

There's an excellent guide over on the Android Training site:

http://developer.android.com/training/displaying-bitmaps/load-bitmap.html

It's about efficient loading of bitmap images - highly recommended!

Upvotes: 1

a54studio
a54studio

Reputation: 975

This will save space. If not using Alpha colors it would be better not to use one with the A channel.

        Options options = new BitmapFactory.Options();
        options.inScaled = false;
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    // or   Bitmap.Config.RGB_565 ;
    // or   Bitmap.Config.ARGB_4444 ;

        Bitmap newBitmap = Bitmap.createScaledBitmap(_tempBitmap, (int)_newWidth, (int)_newHeight, options);

Upvotes: 0

Related Questions