【起航计划 013】2015 起航计划 Android APIDemo的魔鬼步伐 12 App->Activity->SetWallpaper 设置壁纸 WallpaperManager getDrawingCache使用
2015-03-03 14:47
639 查看
SetWallpaper介绍如何在Android获取当前Wallpaper,对Wallpaper做些修改,然后用修改后的图像重新设置Wallpaper。(即设置》显示》壁纸》壁纸的功能)
WallpaperManager用来管理Android的Wallpaper。下面代码通过WallpaperManager来取得当前Wallpaper然后显示在屏幕Layout的imageView上。
randomize对原壁纸进行颜色过滤:
其中mColors如下:
重新设置Wallpaper,也是通过WallpaperManager。
这里还使用了ImageView的getDrawingCache方法,DrawingCache使用,使用这个方法前必须setDrawingCacheEnabled为true,否则获取不到view的cache图片。
WallpaperManager用来管理Android的Wallpaper。下面代码通过WallpaperManager来取得当前Wallpaper然后显示在屏幕Layout的imageView上。
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(this); final Drawable wallpaperDrawable = wallpaperManager.getDrawable(); final ImageView imageView = (ImageView) findViewById(R.id.imageview); imageView.setDrawingCacheEnabled(true); imageView.setImageDrawable(wallpaperDrawable);
randomize对原壁纸进行颜色过滤:
Button randomize = (Button) findViewById(R.id.randomize); randomize.setOnClickListener(new OnClickListener() { public void onClick(View view) { int mColor = (int) Math.floor(Math.random() * mColors.length); wallpaperDrawable.setColorFilter(mColors[mColor], PorterDuff.Mode.MULTIPLY); imageView.setImageDrawable(wallpaperDrawable); imageView.invalidate(); } });
其中mColors如下:
final static private int[] mColors = {Color.BLUE, Color.GREEN, Color.RED, Color.LTGRAY, Color.MAGENTA, Color.CYAN, Color.YELLOW, Color.WHITE};
重新设置Wallpaper,也是通过WallpaperManager。
Button setWallpaper = (Button) findViewById(R.id.setwallpaper); setWallpaper.setOnClickListener(new OnClickListener() { public void onClick(View view) { try { wallpaperManager.setBitmap(imageView.getDrawingCache()); finish(); } catch (IOException e) { e.printStackTrace(); } } });
这里还使用了ImageView的getDrawingCache方法,DrawingCache使用,使用这个方法前必须setDrawingCacheEnabled为true,否则获取不到view的cache图片。
相关文章推荐
- 【起航计划 013】2015 起航计划 Android APIDemo的魔鬼步伐 12 App->Activity->SetWallpaper 设置壁纸 WallpaperManager getDrawingCache使用
- 【起航计划 005】2015 起航计划 Android APIDemo的魔鬼步伐 04 App->Activity->Custom Dialog Dialog形式的Activity,Theme的使用,Shape的使用
- 【起航计划 009】2015 起航计划 Android APIDemo的魔鬼步伐 08 App->Activity->QuickContactsDemo 联系人 ResourceCursorAdapter使用 QuickContactBad
- 【起航计划 005】2015 起航计划 Android APIDemo的魔鬼步伐 04 App->Activity->Custom Dialog Dialog形式的Activity,Theme的使用,Shape的使用
- 【起航计划 009】2015 起航计划 Android APIDemo的魔鬼步伐 08 App->Activity->QuickContactsDemo 联系人 ResourceCursorAdapter使用 QuickContactBadge使用
- 【起航计划 019】2015 起航计划 Android APIDemo的魔鬼步伐 18 App->Device Admin 设备管理器 DeviceAdminReceiver DevicePolicyManager PreferenceActivity的使用
- 【起航计划 004】2015 起航计划 Android APIDemo的魔鬼步伐 03 App->Activity->Animation Activity跳转动画 R.anim.×× overridePendingTransition ActivityOptions类
- 【起航计划 006】2015 起航计划 Android APIDemo的魔鬼步伐 05 App->Activity->Custom Title 自定义标题栏
- 【起航计划 007】2015 起航计划 Android APIDemo的魔鬼步伐 06 App->Activity->Forwarding Activity启动另外一个Activity finish()方法
- 【起航计划 008】2015 起航计划 Android APIDemo的魔鬼步伐 07 App->Activity->Persistent State 保存状态 SharedPreferences onPause onResume
- 【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect
- 【起航计划 011】2015 起航计划 Android APIDemo的魔鬼步伐 10 App->Activity->Reorder Activities 后退栈 Intent FLAG
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstanceState
- 【起航计划 014】2015 起航计划 Android APIDemo的魔鬼步伐 13 App->Activity->Translucent 半透明Activity Theme.Translucent
- 【起航计划 015】2015 起航计划 Android APIDemo的魔鬼步伐 14 App->Activity->Translucent Blur 模糊背景
- 【起航计划 016】2015 起航计划 Android APIDemo的魔鬼步伐 15 App->Activity->Wallpaper 系统壁纸作为当前Activity的背景
- 【起航计划 022】2015 起航计划 Android APIDemo的魔鬼步伐 21 App->Launcher Shortcuts 为某个非主Activity在Home Screen上建立一个快捷方式
- 【起航计划 023】2015 起航计划 Android APIDemo的魔鬼步伐 22 App->Menu->Inflate from XML 使用xml资源展示菜单
- 【起航计划 025】2015 起航计划 Android APIDemo的魔鬼步伐 24 App->Notification->Notifying Service Controller service中使用Notification
- 【起航计划 028】2015 起航计划 Android APIDemo的魔鬼步伐 27 App->Preferences->Launching preferences 其他activity获取Preference中的值