android 设置为壁纸代码
16lz
2021-01-26
几种对壁纸的设置。供大家参考。
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代码
- WallpaperManagerwallpaperManager=WallpaperManager.getInstance(this);
- Resourcesres=getResources();
- Bitmapbitmap=BitmapFactory.decodeResource(res,getResources().getIdentifier("wallpaper"+imagePosition,"drawable","com.ch"));
- wallpaperManager.setBitmap(bitmap);
- Toast.makeText(this,"设置成功",Toast.LENGTH_SHORT).show();
2)第二 通过WallpaperManager方法中的setResource()
Java代码
- WallpaperManagerwallpaperManager=WallpaperManager.getInstance(this);
- try{
- wallpaperManager.setResource(getResources().getIdentifier("wallpaper"+imagePosition,"drawable","com.ch"));
- Toast.makeText(this,"设置成功",Toast.LENGTH_SHORT).show();
- }catch(IOExceptione){
- e.printStackTrace();
- }
- //重写ContextWrapper中setWallpaper()方法
- publicvoidsetWallpaper(InputStreamparamInputStream)throwsIOException{
- super.setWallpaper(paramInputStream);
- Toast.makeText(this,"设置成功",1).show();
- }
- //设置壁纸代码
- ResourceslocalResources=getBaseContext().getResources();
- InputStreamlocalInputStream2=localResources
- .openRawResource(getResources().getIdentifier(
- "wallpaper"+imagePosition,"drawable","com.ch"));
- try{
- setWallpaper(localInputStream2);
- }catch(IOExceptione){
- e.printStackTrace();
- }
更多相关文章
- Android(安卓)api28 tablayout改变
- 设置布局默认为LinearLayout,却成了RelativeLayout
- Android(安卓)Studio的下载、安装与配置
- Android(安卓)RecyclerView 实现快速滑动
- 一步一步学android之布局管理器——LinearLayout
- Android:TextView显示富文本信息
- FAQ_10_EditText与parseInt方法
- Android基础控件(EditView、SeekBar等)的使用方法
- Android(安卓)倒计时工具类