2011-12-04 98 views
3

我有一個尺寸爲960x800的圖像,我試圖讓它填滿屏幕。在Android中創建縮放位圖時保持圖像質量

我一直在做的方式是加載完整的960x800位圖並使用源和目標Rect對象。到目前爲止,我的目標矩形是480x320(我的屏幕尺寸),源矩形將是960x800。

background = BitmapFactory.decodeResource(getResources(), R.drawable.background, null); 
destRect = new Rect(0,0, screenWidth, screenHeight); 
sourceRect = new Rect(0,0, background.getWidth(), background.getHeight()); 
... 
c.drawBitmap(background, sourceRect, destRect, backgroundPaint); 

如果我再使用Paint對象,並試圖背景畫布時設置抖動真實,圖像看起來絕對完美。

Paint backgroundPaint = new Paint(); 
backgroundPaint .setDither(true); 

我得出結論,這可能不是非常有效率,導致每次平局呼叫都會造成不必要的工作。爲了解決這個問題,我想嘗試並做到以下幾點:

background = BitmapFactory.decodeResource(getResources(), R.drawable.background, null); 
background = Bitmap.createScaledBitmap(background, display.getWidth(), display.getHeight(), true); 

從而創造一個已經大小的位圖,elminating任何Rect對象的需要。但是,當我這樣做時,我無法保留與上述方法相同的圖像質量。實際上,如果我沒有使用前述方法使用Paint對象,則圖像看起來類似於可以看到的圖像。在縮放的位圖上使用繪畫對象看起來沒有效果。

爲了讓我的圖像看起來像一個例子,它類似於this畫面操作(雖然沒有那麼嚴重)

有什麼我可以做的就是相同的圖像質量?

回答

0

這是條件,如果你想你的形象是寬度140nyour高度將得到自動調整

Bitmap originalBitmap = BitmapFactory.decodeFile(getResources(), R.drawable.background, null); 
    Bitmap bitmap=originalBitmap; 
    if (bitmap !=null) { 
     int h = bitmap.getHeight(); 
     int w = bitmap.getWidth(); 
     h = 140*h/w; 
     w = 140; 
     bitmap = Bitmap.createScaledBitmap(bitmap, w, h, true); 
     setIconImage(bitmap); 
    }