0

我有一张想用作背景的图像,但我首先需要将其缩小以防止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);

但是,由于它不将位图作为参数,如何将布局的背景设置为该缩放位图?

4

1 回答 1

1

使位图可绘制并将其设置为背景。

   rl.setBackgroundDrawable(new BitmapDrawable(bm));
于 2012-07-20T02:05:16.693 回答