編輯:新增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();
}
});
}
啊解決了我如何從這麼多小時修復圖像的大小問題... – boltsfrombluesky
由於一噸@AndroidNoob - 掙扎了一會兒,設置準確的壁紙 - 終於得到了與對你的代碼進行微小的調整。基本上不得不設置Y偏移量。 – KurtCobain
你是我的生命保護!我一直堅持了半天。非常感謝你:) – Ralphilius