仅设置 Android 主屏幕壁纸
Posted
技术标签:
【中文标题】仅设置 Android 主屏幕壁纸【英文标题】:Setting Android HomeScreen wallpaper only 【发布时间】:2021-03-16 01:35:08 【问题描述】:如何以编程方式为以下选项设置 android 的壁纸:
仅设置为主屏幕 仅设置为锁屏 设置为主屏和锁屏以下是我目前所拥有的,但我不知道如何将其设置为仅用于主屏幕。
private void setWallpaper(final int flagSystem)
Glide.with(this)
.asBitmap()
.load(wallPaper)
.into(new SimpleTarget<Bitmap>()
@Override
public void onResourceReady(@NonNull Bitmap bitmap, @Nullable Transition<? super Bitmap> transition)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
try
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getContext());
int wallpaperHeight = Resources.getSystem().getDisplayMetrics().heightPixels;
int wallpaperWidth = Resources.getSystem().getDisplayMetrics().widthPixels;
Point start = new Point(0, 0);
Point end = new Point(bitmap.getWidth(), bitmap.getHeight());
if (bitmap.getWidth() > wallpaperWidth)
start.x = (bitmap.getWidth() - wallpaperWidth) / 2;
end.x = start.x + wallpaperWidth;
if (bitmap.getHeight() > wallpaperHeight)
start.y = (bitmap.getHeight() - wallpaperHeight) / 2;
end.y = start.y + wallpaperHeight;
if (flagSystem == 0)
wallpaperManager.setBitmap(bitmap, null, true);
else if (flagSystem == 1)
wallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK);
else
wallpaperManager.setBitmap(bitmap, new Rect(start.x, start.y, end.x, end.y), false);
Toast.makeText(getContext(), "Wallpaper set successfully", Toast.LENGTH_SHORT).show();
catch (IOException e)
e.printStackTrace();
);
【问题讨论】:
这能回答你的问题吗? How to set android lock screen image 【参考方案1】:使用WallpaperManager.FLAG_SYSTEM
标志
manager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_SYSTEM);
【讨论】:
WallpaperManager.FLAG_SYSTEM 设置主屏幕和锁定屏幕。我只想为主屏幕设置。以上是关于仅设置 Android 主屏幕壁纸的主要内容,如果未能解决你的问题,请参考以下文章