将位图显示为背景

时间:2012-07-20 00:54:27

标签: android bitmap

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

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

1 个答案:

答案 0 :(得分:1)

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

   rl.setBackgroundDrawable(new BitmapDrawable(bm));
相关问题