【问题标题】:How to set wallpaper in android that fit across all the screens without stretching, Zooming and cropping如何在不拉伸、缩放和裁剪的情况下在 android 中设置适合所有屏幕的壁纸
【发布时间】:2022-07-22 04:51:18
【问题描述】:

注意:请不要将其标记为重复,因为几年前提出了相同的问题,这在当时可能有效,但现在无效。

使用下面的代码,我的壁纸要么拉伸到屏幕 2 和 3,要么根据设备在屏幕底部裁剪。以下代码适用于三星设备,但不适用于其他制造商的设备。

我还尝试使用以下方法获取高度和宽度,但没有成功(壁纸缩放并拉伸到其他屏幕)。 getWallpaperDesiredMinimumHeight() 和 getWallpaperDesiredMinimumWidth()

public void setWallpaper() {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;

    WallpaperManager wm = WallpaperManager.getInstance(this);
    Bitmap bmap = BitmapFactory.decodeResource(getResources(), R.drawable.myPicture);
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmap, width, height, true);
    wm.setWallpaperOffsetSteps(1, 1);
    wm.suggestDesiredDimensions(width, height);
    try {
        wm.setBitmap(scaledBitmap);
       } catch (IOException e) {

    }

}

【问题讨论】:

  • 您打算如何在不“拉伸、缩放 [或] 裁剪”的情况下处理不同的纵横比?
  • @CommonsWare 我只打算在所有设备上以纵向模式使用壁纸。我不介意壁纸在单个屏幕中拉伸和调整并居中。 1.如果设备有3个虚拟屏幕,我希望我的壁纸适合第一个屏幕并居中,屏幕2和3上应该显示相同。2.我面临的问题是,如果壁纸延伸一点比屏幕一,图像没有居中并滚动到屏幕 2 和 3。
  • “我只打算在所有设备上以纵向模式使用壁纸”——这并没有改变在数以万计的 Android 设备型号中存在许多不同纵横比的事实。 “我不介意墙纸在单个屏幕中拉伸和调整并居中”-您的问题不同意。
  • @CommonsWare 我正在使用以代码编写的显示指标获取设备宽度和高度,并根据当前设备的宽度和高度缩放我的壁纸,并将缩放后的图像设置为壁纸。在问题中,我试图解释拉伸和缩放到屏幕 2 和 3,但字数限制不允许我这样做。我希望你现在明白我的问题。请建议我是否可以使用任何解决方案来解决此问题。

标签: android android-wallpaper wallpapermanager


【解决方案1】:

制造商有不同的壁纸设置方式,将壁纸设置为可移动或将壁纸设置为固定。

在安卓壁纸被放大时,当我们从主屏幕顶部拖动时,你可以看到壁纸被缩小了。所以我按如下方式缩放了壁纸的高度和宽度,这有助于我在设备上正确设置壁纸。 以下是更新后的代码。

public void setWallpaper() {
    DisplayMetrics displayMetrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    int height = displayMetrics.heightPixels;
    int width = displayMetrics.widthPixels;
        height = (int) (height * (95.0f / 100.0f));
        width = (int) (width * (85.0f / 100.0f));

    WallpaperManager wm = WallpaperManager.getInstance(this);
    Bitmap bmap = BitmapFactory.decodeResource(getResources(), R.drawable.myPicture);
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bmap, width, height, true);
    wm.setWallpaperOffsetSteps(1, 1);
    wm.suggestDesiredDimensions(width, height);
    try {
        wm.setBitmap(scaledBitmap);
       } catch (IOException e) {

    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-06
    • 2014-07-17
    • 2014-05-10
    • 2011-05-20
    • 1970-01-01
    • 1970-01-01
    • 2020-05-20
    • 1970-01-01
    相关资源
    最近更新 更多