2012-06-20 39 views
7

我有一款應用將壁紙應用於主屏幕的圖像與屏幕尺寸相匹配,但是在銀河s3上,當我應用它時,屏幕會放大,即使使用的圖像與屏幕尺寸完全一致!這是一個靜態圖像,當您在主屏幕上切換頁面時,甚至不會滾動。奇怪的是,如果我使用內置圖庫應用圖像,則壁紙可以在沒有變焦的情況下應用得很好(它出現在「裁切圖像」屏幕上,但裁剪區域本身與圖像的邊緣相匹配)即使尺寸與屏幕匹配,WallpaperManager也會放大圖像

我使用的代碼在整個手機陣列(星系筆記,ace 2,s2等)上工作良好,但不在s3上;我想知道是否有任何事情可以強制壁紙正確填充屏幕?當前的代碼我使用應用壁紙:

WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); 
wallpaperManager.setWallpaperOffsets(wallpaperViewer.getApplicationWindowToken(), 0, 0); 
wallpaperManager.setBitmap(BitmapFactory.decodeFile(file.getPath()));//file is jpg on sd card 

回答

9

編輯:新增Y軸偏移修正 - 感謝@Jason高夫!

好,所以事實證明,s3的主屏幕的最小寬度不是720但實際上是1280!你可以通過調用

wallpaperManager.getDesiredMinimumWidth();//returned 1280 on s3 
wallpaperManager.getDesiredMinimumHeight();//also returned 1280 on s3 

找出壁紙所需的最小寬度和高度所以爲了牆紙應用到屏幕的中心,我不得不動態創建一個空白的位圖1280x1280,然後覆蓋我的壁紙到空白位圖的中心,我創建了一個靜態BitmapHelper類,用於調整位圖的方法,下面是創建位圖和覆蓋在牆紙圖像的方法:用我的BitmapHelper

public class BitmapHelper { 

public static Bitmap overlayIntoCentre(Bitmap bmp1, Bitmap bmp2) { 
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig()); 
    Canvas canvas = new Canvas(bmOverlay); 
    canvas.drawBitmap(bmp1, new Matrix(), null);//draw background bitmap 

    //overlay the second in the centre of the first 
    //(may experience issues if the first bitmap is smaller than the second, but then why would you want to overlay a bigger one over a smaller one?!) 
    //EDIT: added Y offest fix - thanks @Jason Goff! 
    canvas.drawBitmap(bmp2, (bmp1.getWidth()/2)-(bmp2.getWidth()/2), (bmp1.getHeight()/2)-(bmp2.getHeight/2), null); 

    return bmOverlay; 
} 

public static Bitmap createNewBitmap(int width, int height) 
{ 
      //create a blanks bitmap of the desired width/height 
    return Bitmap.createBitmap(width, height, Config.ARGB_8888); 
} 
} 

和繼承人我的代碼的其餘部分:

private void applyWallpaperFromFile(final File file) 
{ 
    Bitmap wallpaperImage = BitmapFactory.decodeFile(file.getPath()); 
    try { 
     if((wallpaperManager.getDesiredMinimumWidth()>0)&&(wallpaperManager.getDesiredMinimumHeight()>0)) 
     { 
      Bitmap blank = BitmapHelper.createNewBitmap(wallpaperManager.getDesiredMinimumWidth(), wallpaperManager.getDesiredMinimumHeight()); 
      Bitmap overlay = BitmapHelper.overlayIntoCentre(blank, wallpaperImage); 

      wallpaperManager.setBitmap(overlay); 

     } 
     else 
     { 
      wallpaperManager.setBitmap(wallpaperImage); 
     } 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    this.runOnUiThread(new Runnable() { 

     @Override 
     public void run() { 
      Toast.makeText(WallpaperActivity.this,"Wallpaper set to:"+file.getName(), Toast.LENGTH_SHORT).show(); 

     } 
    }); 

} 
+0

啊解決了我如何從這麼多小時修復圖像的大小問題... – boltsfrombluesky

+1

由於一噸@AndroidNoob - 掙扎了一會兒,設置準確的壁紙 - 終於得到了與對你的代碼進行微小的調整。基本上不得不設置Y偏移量。 – KurtCobain

+0

你是我的生命保護!我一直堅持了半天。非常感謝你:) – Ralphilius

-1

你可以嘗試使用suggestDesiredDimensions()方法

+2

每個http://developer.android.com/reference/android/app/WallpaperManager.html#suggestDesiredDimensions%28int,%20int%29,該方法只供發射器應用程序使用。 – gdw2

2

您可能希望在此再看看:http://developer.android.com/reference/android/app/WallpaperManager.html#suggestDesiredDimensions(int, int)

從開發者參考:

公共無效suggestDesiredDimensions(INT最小寬度, int minimumHeight)

因爲:API級別5 僅供當前家庭應用程序使用,至 指定它想要使用的壁紙大小。這允許這樣的應用程序具有大於 物理屏幕的虛擬壁紙,其與他們的工作空間的大小相匹配。

請注意開發人員,他們似乎沒有閱讀過此內容。這是爲家庭 屏幕來告訴他們想要什麼大小的壁紙。其他人 應該這樣稱呼!當然不是其他非主屏幕應用程序,更改牆紙。這些應用程序應該檢索建議的大小,以便它們可以構建與其匹配的壁紙。

3

感謝您的文章。我發現我的壁紙卡在設備屏幕的頂部,所以我稍微更改了overlayIntoCentre方法,用第二個drawBitmap調用中的0代替以下高度計算。

public static Bitmap overlayIntoCentre(Bitmap bmp1, Bitmap bmp2) { 
    Bitmap bmOverlay = Bitmap.createBitmap(bmp1.getWidth(), bmp1.getHeight(), bmp1.getConfig()); 
    Canvas canvas = new Canvas(bmOverlay); 
    canvas.drawBitmap(bmp1, new Matrix(), null);//draw background bitmap 

    //overlay the second in the centre of the first 
    //(may experience issues if the first bitmap is smaller than the second, but then why would you want to overlay a bigger one over a smaller one?!) 
    canvas.drawBitmap(bmp2, (bmp1.getWidth()/2)-(bmp2.getWidth()/2), (bmp1.getHeight()/2)-(bmp2.getHeight/2), null); 

return bmOverlay; 

}

+0

好景點!我的代碼假設覆蓋位圖的高度對於設備是正確的,因此不需要垂直居中 – AndroidNoob

+0

@Jason Goff - 是的 - 通過找出錯誤發生了同樣的事情 - 然後檢查你的評論。在Y偏移修正後很好地工作。 – KurtCobain