zoukankan      html  css  js  c++  java
  • Android 墙纸设置代码 详细说明

    使游戏图像列表。思考添加壁纸功能。我发了一些资料。

    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()方法。

    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
     Resources res = getResources();
     Bitmap bitmap=BitmapFactory.decodeResource(res, getResources().getIdentifier("wallpaper" + imagePosition, "drawable", "com.ch")); 
    wallpaperManager.setBitmap(bitmap);
    Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();

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

    WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
    try {
    wallpaperManager.setResource(getResources().getIdentifier("wallpaper" + imagePosition, "drawable", "com.ch"));
    Toast.makeText(this, "设置成功", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
    e.printStackTrace();
    }

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

    //重写ContextWrapper中setWallpaper()方法
    public void setWallpaper(InputStream paramInputStream) throws IOException {
    super.setWallpaper(paramInputStream);
    Toast.makeText(this, "设置成功", 1).show();
    }
    //设置壁纸代码
                            Resources localResources = getBaseContext().getResources();
    InputStream localInputStream2 = localResources
    .openRawResource(getResources().getIdentifier(
    "wallpaper" + imagePosition, "drawable", "com.ch"));
    try {
    setWallpaper(localInputStream2);
    } catch (IOException e) {
    e.printStackTrace();
    }

    就这三种。感觉还是第二章最好用,能够把网络图片转化为 Bitmap 类型。然后调用系统的 WallpaperManager 执行相应的方法调用。

    版权声明:本文博主原创文章,博客,未经同意不得转载。

  • 相关阅读:
    MvcScaffolding: OnetoMany Relationships
    未能从程序集“System.ServiceModel, Version=3.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089”中加载
    MVC中使用Scaffold生成代码
    项目开发的三个阶段 我的一点体会
    在Eclipse工作空间中创建新项目的方法
    Visual Studio 使用常见问题
    Matlab代码:为图像添加信噪比为SNR db的高斯噪声
    《Ogre 3D 游戏开发框架指南》配套光盘的一个小瑕疵
    如何阅读C++源代码
    在Eclipse工作空间中移除再添加项目的方法
  • 原文地址:https://www.cnblogs.com/zfyouxi/p/4827989.html
Copyright © 2011-2022 走看看