tyczj
tyczj

Reputation: 73721

Displaying a bitmap as a background

I have an image that I want to use as a background but I first need to scale it down to prevent OutOfMemoryExceptions

BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(getResources(), R.drawable.home_bkgrnd, options);
    int imageHeight = options.outHeight;
    int imageWidth = options.outWidth;
    String imageType = options.outMimeType;
    int sampleSize =1;

    Display display = getWindowManager().getDefaultDisplay(); 
    int width = display.getWidth();
    int height = display.getHeight();
    if(imageWidth > imageHeight){
        sampleSize = Math.round((float)imageHeight/(float)height);
    }else{
        sampleSize = Math.round((float)imageWidth/(float)width);
    }
    options.inJustDecodeBounds = false;
    Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.home_bkgrnd, options);


rl.setBackgroundDrawable(bm);

But how do I set the background of my layout to that scaled bitmap since it does not take a bitmap as an argument?

Upvotes: 0

Views: 139

Answers (1)

PH7
PH7

Reputation: 3916

Make bitmap drawable and set it as background.

   rl.setBackgroundDrawable(new BitmapDrawable(bm));

Upvotes: 1

Related Questions