android怎么设置背景?android背景设置教程

时间:2024-10-13 01:20:32

1、public static final String TAG = "WallpaperActivity";/*** 背景图片名称*/private static final String FILE_NAME = "MM-320x480.png";/*** @see android.app.Activity#onCreate(android.os.Bundle)*/public void onCreate(Bundle cycle) {super.onCreate(cycle);super.setContentView(R.layout.wallpaper);

android怎么设置背景?android背景设置教程

2、// 取得背景图片Bitmap wallpaper = this.getWallpager();

android怎么设置背景?android背景设置教程

3、// 设置桌面背景this.putWallpaper(wallpaper);}

android怎么设置背景?android背景设置教程

4、/*** 取得SDCard中的背景图片*/private Bitmap getWallpager() {// SDCard的路径,也就是“/sdcard/”File root = Environment.getExternalStorageDirectory();// MM图片File wall = new File(root, FILE_NAME);// MM图片路径String path = wall.getAbsolutePath();Log.d(TAG, "MM文件路径为:" + path);return BitmapFactory.decodeFile(path);}

android怎么设置背景?android背景设置教程

5、/*** 设置桌面背景* www.cnxuwen.com*/private void putWa盟敢势袂llpaper(Bitmap bitmap) {try {WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);wallpaperManager.setBitmap(bitmap);} catch (IOException e) {String msg = "设置桌面背景发生异常:" + e.getLocalizedMessage();Log.e(TAG, "设置桌面背景发生异常!", e);Toast.makeText(this, msg, Toast.LENGTH_LONG).show();}}

android怎么设置背景?android背景设置教程

6、增加权限,在AndroidManifest.xml文件中增加设置桌面背景权限:<!--设置桌面背景的权限--><uses-permissionandroid:name="android.permission.SET_WALLPAPER"/>

android怎么设置背景?android背景设置教程
© 手抄报圈