即使尺寸与屏幕匹配,WallpaperManager也会缩放图像

And*_*oob 7 android wallpaper homescreen

我有一个应用程序将壁纸应用到主屏幕上,其图像与屏幕尺寸相匹配,但是在我应用它时,壁纸会放大,即使使用的图像与屏幕尺寸完全匹配!它是静态图像,在主屏幕上切换页面时甚至不滚动.奇怪的是,如果我使用内置图库应用图像,壁纸将在没有缩放的情况下正常应用(它会显示"裁剪图像"屏幕,但裁剪区域本身与图像的边缘匹配)

我使用的代码适用于整个手机阵列(星系注释,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
Run Code Online (Sandbox Code Playgroud)

And*_*oob 9

编辑:添加Y偏移修复 - 谢谢@Jason Goff!

好吧,事实证明,s3上主屏幕的最小宽度不是720,而是1280!你可以通过电话找到所需的壁纸最小宽度和高度

wallpaperManager.getDesiredMinimumWidth();//returned 1280 on s3
wallpaperManager.getDesiredMinimumHeight();//also returned 1280 on s3
Run Code Online (Sandbox Code Playgroud)

因此,为了将壁纸应用到屏幕中心,我必须动态创建一个空白位图1280x1280,然后将我的壁纸覆盖到空白位图的中心,我创建了一个静态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);
}
}
Run Code Online (Sandbox Code Playgroud)

并使用我的BitmapHelper继承我的其余代码:

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();

        }
    });

}
Run Code Online (Sandbox Code Playgroud)