壁纸管理器即使尺寸与屏幕匹配,仍会缩放图像

7
我有一个应用程序,可以使用与屏幕尺寸相匹配的图像将壁纸应用到主屏幕上。然而,在Galaxy S3上应用后,壁纸会被放大,即使所使用的图像大小完全匹配屏幕尺寸!这是一张静态图片,即使在主屏幕页面之间切换时也不会滚动。奇怪的是,如果我使用内置的图库应用图像,则壁纸会正常应用,而没有缩放(它会出现“裁剪图像”屏幕,但裁剪区域本身与图像边缘匹配)。
我使用的代码在整个系列的手机上都运行良好(Galaxy Note、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
4个回答

9

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

好的,事实证明S3主屏幕的最小宽度不是720而是1280!您可以通过调用以下方法来查找所需的壁纸最小宽度和高度:

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

因此,为了将壁纸应用到屏幕中央,我必须即时创建一个大小为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);
}
}

以下是使用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();

        }
    });

}

1
非常感谢@AndroidNoob - 我曾经苦苦挣扎地尝试设置精确的壁纸 - 最终通过对你的代码进行微调,我终于得到了它。基本上需要设置一个Y偏移量。 - KurtCobain
你是我的救星!我被卡在这里半天了。非常感谢 :) - Ralphilius

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;

}


不错!我的代码假设覆盖位图的高度对于设备是正确的,因此不需要垂直居中。 - AndroidNoob
@Jason Goff - 是的 - 通过找出问题所在并检查了你的评论后,做了完全相同的事情。在 Y 偏移修复后运行良好。 - KurtCobain

2

您可能需要再次查看此内容:http://developer.android.com/reference/android/app/WallpaperManager.html#suggestDesiredDimensions(int, int)

来自开发者参考:

public void suggestDesiredDimensions (int minimumWidth, int minimumHeight)

自API Level 5起,仅供当前主屏幕应用程序使用,用于指定其想要使用的壁纸大小。这允许这些应用程序拥有虚拟壁纸,该壁纸比物理屏幕大,与其工作区域的大小相匹配。

请注意开发人员,他们似乎没有阅读这个。这是为主屏幕设计的,以告知它们想要的壁纸尺寸。没有其他人应该调用它!当然不是其他非主屏幕应用程序更改壁纸的应用程序。那些应用程序应该检索建议的大小,以便构造与之匹配的壁纸。


-1

您可以尝试使用suggestDesiredDimensions()方法


3
根据http://developer.android.com/reference/android/app/WallpaperManager.html#suggestDesiredDimensions%28int,%20int%29,该方法仅供启动器应用程序使用。 - gdw2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接