2012-07-20 46 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