2013-01-25 168 views
0

我試圖將我的位圖圖像設置爲壁紙!設置壁紙

我目前正在使用此代碼來完成它。

  WallpaperManager wpm = WallpaperManager.getInstance(this); 

      float minH = wpm.getDesiredMinimumHeight(); 
      float minW = wpm.getDesiredMinimumWidth(); 
      Log.d("seb", minH + " = Min Height"); 
      Log.d("seb", minW + " = Min Width"); 
      targetBitmap = Bitmap.createScaledBitmap(targetBitmap,(int)minW, (int)minH, false);   
      wpm.setBitmap(targetBitmap); 

它的工作原理!手機會自動調整位圖以適應屏幕,但無論我的位圖是多麼小,它總是水平裁剪並放大。

有誰知道如何解決這個問題?

(一個補丁修復將是把黑色邊框,讓他們代替裁剪實際的圖片,雖然即時猜測有一個更好的選擇)

編輯

This is original picture

這是代碼中的原始圖片。

下面的圖片是我的意思與裁剪時設置爲壁紙:

Cropped picture

,這將是即使我調整圖像以同樣的方式,因爲系統會自動放大圖像,以適應整個屏幕

+0

Horizo​​ntaly裁剪?你可以提供一個例子,裁剪圖像可以完美 –

+0

@Milos編輯的問題! – Sebastian

+0

謝謝,在第二張圖像中,圖像是裁剪不在屏幕中間,邊框是白色還是裁剪後的圖像填滿了整個屏幕? –

回答

0

好的,所以在我們討論「評論」後,這裏有一個可以工作的解決方案。

試一下:

public Bitmap getResizedBitmap(Bitmap bm, int newHeight, int newWidth) { 

    int width = bm.getWidth(); 

    int height = bm.getHeight(); 

    float scaleWidth = ((float) newWidth)/width; 

    float scaleHeight = ((float) newHeight)/height; 

// create a matrix for the manipulation 

    Matrix matrix = new Matrix(); 

    // resize the bit map 

    matrix.postScale(scaleWidth, scaleHeight); 

// recreate the new Bitmap 

    Bitmap resizedBitmap = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, false); 

    return resizedBitmap; 

} 

試試吧,如果你有相同(或其他)的問題,讓我知道,我們會嘗試修復它。