几种对壁纸的设置。供大家参考。

1、别忘记在ApplicationManifest.xml 中加上权限的设置。

<uses-permission android:name = "android.permission.SET_WALLPAPER"/>

2、设置壁纸的方法总结。

壁纸设置方法有三种

第一 通过WallpaperManager方法中的setBitmap()

第二 通过WallpaperManager方法中的setResource()

第三 通过ContextWrapper 类中提供的setWallpaper()方法

由于 Activity 继承ContextThemeWrapper ,ContextThemeWrapper继承 ContextWrapper.

1)通过实例化WallpaperManager 类调用单例类中setBitmap()方法。

Java代码
  1. WallpaperManagerwallpaperManager=WallpaperManager.getInstance(this);
  2. Resourcesres=getResources();
  3. Bitmapbitmap=BitmapFactory.decodeResource(res,getResources().getIdentifier("wallpaper"+imagePosition,"drawable","com.ch"));
  4. wallpaperManager.setBitmap(bitmap);
  5. Toast.makeText(this,"设置成功",Toast.LENGTH_SHORT).show();

2)第二 通过WallpaperManager方法中的setResource()

Java代码
  1. WallpaperManagerwallpaperManager=WallpaperManager.getInstance(this);
  2. try{
  3. wallpaperManager.setResource(getResources().getIdentifier("wallpaper"+imagePosition,"drawable","com.ch"));
  4. Toast.makeText(this,"设置成功",Toast.LENGTH_SHORT).show();
  5. }catch(IOExceptione){
  6. e.printStackTrace();
  7. }
3) 第三 通过ContextWrapper 类中提供的setWallpaper()方法 Java代码
  1. //重写ContextWrapper中setWallpaper()方法
  2. publicvoidsetWallpaper(InputStreamparamInputStream)throwsIOException{
  3. super.setWallpaper(paramInputStream);
  4. Toast.makeText(this,"设置成功",1).show();
  5. }
  6. //设置壁纸代码
  7. ResourceslocalResources=getBaseContext().getResources();
  8. InputStreamlocalInputStream2=localResources
  9. .openRawResource(getResources().getIdentifier(
  10. "wallpaper"+imagePosition,"drawable","com.ch"));
  11. try{
  12. setWallpaper(localInputStream2);
  13. }catch(IOExceptione){
  14. e.printStackTrace();
  15. }
总结: 设置壁纸功能很简单,可以当成我们第一个app应用练手。其中可以加入ImageSwitcher 和gallery等控件。 真正完成一个可以发布的App应用

更多相关文章

  1. Android(安卓)api28 tablayout改变
  2. 设置布局默认为LinearLayout,却成了RelativeLayout
  3. Android(安卓)Studio的下载、安装与配置
  4. Android(安卓)RecyclerView 实现快速滑动
  5. 一步一步学android之布局管理器——LinearLayout
  6. Android:TextView显示富文本信息
  7. FAQ_10_EditText与parseInt方法
  8. Android基础控件(EditView、SeekBar等)的使用方法
  9. Android(安卓)倒计时工具类

随机推荐

  1. android解析生成xml文件
  2. android 获取通讯录中同一联系人的多个号
  3. Android实现更换皮肤功能
  4. ADB和Fastboot最新版的谷歌官方下载链接
  5. android检查网络连接状态
  6. Android开发系列教程——目录
  7. Android studio升级3.0.1之后原项目签名
  8. [置顶] Android(安卓)SQlite使用实践Demo
  9. build command in android
  10. android读取SD卡下面所有的TXT文件名,list