2012-07-20 62 views
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); 

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

回答

1

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

rl.setBackgroundDrawable(new BitmapDrawable(bm)); 
+0

折旧。新代码是什么? – Si8 2014-02-05 01:44:03

+0

http://stackoverflow.com/questions/11947603/setbackground-vs-setbackgrounddrawable-android – PH7 2014-02-05 02:32:16

+0

这不告诉我任何事情。不管怎么说,还是要谢谢你 :) – Si8 2014-02-05 03:05:20