【起航计划 013】2015 起航计划 Android APIDemo的魔鬼步伐 12 App->Activity->SetWallpaper 设置壁纸 WallpaperManager getDrawingCache使用
2016-01-03 19:55
741 查看
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图片。
相关文章推荐
- 【起航计划 012】2015 起航计划 Android APIDemo的魔鬼步伐 11 App->Activity->Save & Restore State onSaveInstanceState onRestoreInstance
- 【起航计划 011】2015 起航计划 Android APIDemo的魔鬼步伐 10 App->Activity->Reorder Activities 后退栈 Intent FLAG
- 【起航计划 010】2015 起航计划 Android APIDemo的魔鬼步伐 09 App->Activity->Redirection 根据shared preferences是否有值决定是否redirect
- 【起航计划 009】2015 起航计划 Android APIDemo的魔鬼步伐 08 App->Activity->QuickContactsDemo 联系人 ResourceCursorAdapter使用 QuickContactBad
- 【起航计划 008】2015 起航计划 Android APIDemo的魔鬼步伐 07 App->Activity->Persistent State 保存状态 SharedPreferences onPause onResume
- 【起航计划 007】2015 起航计划 Android APIDemo的魔鬼步伐 06 App->Activity->Forwarding Activity启动另外一个Activity finish()方法
- 【起航计划 006】2015 起航计划 Android APIDemo的魔鬼步伐 05 App->Activity->Custom Title 自定义标题栏
- 【起航计划 005】2015 起航计划 Android APIDemo的魔鬼步伐 04 App->Activity->Custom Dialog Dialog形式的Activity,Theme的使用,Shape的使用
- 【起航计划 004】2015 起航计划 Android APIDemo的魔鬼步伐 03 App->Activity->Animation Activity跳转动画 R.anim.×× overridePendingTran
- 【起航计划 003】2015 起航计划 Android APIDemo的魔鬼步伐 02 SimpleAdapter,ListActivity,PackageManager参考
- 【起航计划 002】2015 起航计划 Android APIDemo的魔鬼步伐 01
- 【Android 界面效果46】自定义view常处理的回调方法
- 【Android 界面效果45】ViewPager源码分析
- 【Android 界面效果44】Android之圆头像实例
- 【Android Studio使用教程 7】AndroidStudio问题汇总
- 【Android Studio使用教程6】Execution failed for task ':×××:compileReleaseAidl'
- 【Mood-13】Android --如何从初级工程师进化为高级工程师
- 【Android车载系统 News | Tech 5】车载设计开发
- 【Android车载系统 News | Tech 4】知乎--车载话题链接
- 【Android车载系统 News | Tech 3】News 从手机征战到汽车 Android Auto对比CarPlay 2014-12-29