2011-10-20 45 views
3

我顯示使用的onDraw方法這樣的圖像:drawBitmap不全屏

public void onDraw(Canvas canvas) { 
    super.onDraw(canvas); 
    Bitmap background = BitmapFactory.decodeResource(getResources(), R.drawable.photo0); 
    canvas.drawColor(Color.BLACK); 
    canvas.drawBitmap(background, 0, 0, null); 

我想設置這個圖像作爲背景,但它是僅在畫面的一部分顯示。如何將其設置爲全屏?

有一種方法可以將圖像設置爲xml背景,並從onDraw方法在此圖像上繪製其他圖像?

回答

13

嘗試:

... 
Rect dest = new Rect(0, 0, getWidth(), getHeight()); 
Paint paint = new Paint(); 
paint.setFilterBitmap(true); 
canvas.drawBitmap(background, null, dest, paint); 

這使得該位圖的一部分(「NULL」意味着整個位圖)給由DEST指定(這是考慮到在這種情況下的整個區域)的屏幕區域。

請注意,這可能會改變寬高比,具體取決於背景,您可能需要修復此問題。

+1

這改變了圖像的質量。如果我將圖像設置爲xml背景,它看起來不錯,但是像這樣不清晰 – Gabrielle

+0

要修復質量,請創建一個Paint對象並調用setFilterBitmap(true)。這將取代默認的「最近鄰」採樣到「線性」。 –

+0

做到了這一點,但圖像仍然只填充較大畫布的一部分。 – teapeng