android - WallpaperManager 缩放图像,即使尺寸与屏幕匹配

标签 android wallpaper homescreen

我有一个应用程序可以将墙纸应用到主屏幕,图像与屏幕尺寸相匹配,但是在 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

最佳答案

编辑:添加了 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();

        }
    });

}

关于android - WallpaperManager 缩放图像,即使尺寸与屏幕匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11120012/

相关文章:

安卓 : Full Screen Activity with Title bar

android - 在 Jetpack Compose 上滚动时折叠导航底部栏

android - 在 Android 中打开启动应用程序之前如何避免加载主屏幕?

Android - 当我们点击应用程序的主屏幕小部件时,我们如何调用一些特定的功能?

android - 如何在android中永久设置壁纸

Android - 如何从头开始更换房屋?

android - Chrome DevTools 远程调试不显示连接的移动设备的 chrome 版本

android - BroadcastReceiver goAsync() 返回 null PendingResult

html - Unblur 模糊背景 div 中的内容

android - Android 上的壁纸是由启动器负责绘制的吗?