切换壁纸
添加权限
<uses-permission android:name = "android.permission.SET_WALLPAPER"/>
在activity中设置壁纸,在res下drawble下放入壁纸圖片,我这里是wallpaper_dog
public void setWallpaper(){
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
Resources res = getResources();
Bitmap bitmap= BitmapFactory.decodeResource(res, getResources().getIdentifier("wallpaper_" + "dog", "drawable", "com.change.themewallpaper"));//最后一个参数是自己包名
try {
wallpaperManager.setBitmap(bitmap);
Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
实现效果如图,没有做全尺寸圖片会有拉伸。
Screenshot_2020-08-20-14-58-01-212_com.miui.home.jpg
网友评论