如何将壁纸设置为主屏幕全屏

How to set a wallpaper as the homescreen full screen

我有一张适合我 phone 尺寸的壁纸:

myWallpaper

我想通过以下方式将其设为主屏幕:

WallpaperManager myWallpaperManager = WallpaperManager.getInstance(this);
String imageFilePath = myWallpaperPath;
Bitmap myBitmap = BitmapFactory.decodeFile(imageFilePath);
if (myBitmap != null) {
    try {
        myWallpaperManager.setBitmap(myBitmap);
    } catch (IOException e) {}
} else {}

我的问题是 myWallpaper 被裁剪后设置为主屏幕。我想设置全尺寸和质量。

试试这个:

像这样将壁纸设置为全尺寸

在清单中添加权限

清单文件

<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>
<uses-permission android:name="android.permission.SET_WALLPAPER"/>

Java 文件

final WallpaperManager wallpaperManager = (WallpaperManager)getSystemService(
            Context.WALLPAPER_SERVICE);

Bitmap myBitmap = Bitmap.createScaledBitmap(
    Const.cropped_bitmap,
    wallpaperManager.getDesiredMinimumWidth(),
    wallpaperManager.getDesiredMinimumHeight(), 
    true);

wallpaperManager.suggestDesiredDimensions(
    wallpaperManager.getDesiredMinimumWidth(),
    wallpaperManager.getDesiredMinimumHeight());

try {
    wallpaperManager.setBitmap(myBitmap);
    Toast.makeText(CropImageActivity.this,
        CropImageActivity.this.getString(R.string.wallpaper_has_been_set), 0).show();
}
catch (IOException e) {
    e.printStackTrace();
    Toast.makeText(CropImageActivity.this, "Wallpaper not set", 0).show();
}

在您的视图中添加此代码点击

        GetScreenWidthHeight();

        SetBitmapSize();

        wallpaperManager = WallpaperManager.getInstance(MainActivity.this);

        try {
            wallpaperManager.setBitmap(bitmap2);
            wallpaperManager.suggestDesiredDimensions(width, height);
        }
        catch (IOException e) {
            e.printStackTrace();
        }
    }
});

public void GetScreenWidthHeight(){

    displayMetrics = new DisplayMetrics();

    getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

    width = displayMetrics.widthPixels;

    height = displayMetrics.heightPixels;
}

public void SetBitmapSize(){

    bitmap2 = Bitmap.createScaledBitmap(bitmap1, width, height, false);
}

将以下权限添加到您的项目:

<uses-permission android:name="android.permission.SET_WALLPAPER" />
<uses-permission android:name="android.permission.SET_WALLPAPER_HINTS"/>

如需完整实施,请查看 this link here