• 设置壁纸


     public class MainActivity extends Activity {
    
        
        private Bitmap mWallpaper;
        private TextView tv=null;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            tv=(TextView)this.findViewById(R.id.setWallPaper);
            setWallPaper();
            
        }
        
        public void setWallPaper(){
            WallpaperManager wpm = (WallpaperManager) this.getSystemService(Context.WALLPAPER_SERVICE);
            InputStream is = null;
            is = getStream("wallpapers/wallpaper02.jpg");
            if(wpm.getWallpaperInfo()!=null){
                mWallpaper = Tools.getImageFromInStream(is);
                try {
                    wpm.setBitmap(mWallpaper);
                    mWallpaper.recycle();
                    mWallpaper = null;
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
             
            }
        }
        public InputStream getStream(String filename) {
            InputStream stream = null;
            try {
                stream = this.getAssets().open(getFileForDpi(filename));
            } catch (IOException e) {
                try {
                    stream = this.getAssets().open("theme/" + filename);
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
            }
            return stream;
        }
        
        public String getFileForDpi(String filename) {
            float mScreenScale = this.getResources().getDisplayMetrics().density;
            if (mScreenScale <= 0.75f) {
                Log.d("launcher", "dpi=0.75");
                filename = "theme-ldpi/" + filename;
            } else if (mScreenScale <= 1f) {
                Log.d("launcher", "dpi=1");
                filename = "theme-mdpi/" + filename;
            } else if (mScreenScale <= 1.5f) {
                Log.d("launcher", "dpi=1.5");
                filename = "theme-hdpi/" + filename;
            } else
                filename = "theme-xhdpi/" + filename;
            return filename;
        }
    }

     注意:需要增加权限:<uses-permission android:name="android.permission.SET_WALLPAPER" />

  • 相关阅读:
    (原)在ubuntu 中安装 swi prolog 和 简单的使用
    (转) 新手入门:C/C++中的结构体
    (转) eclipse debug (调试) 学习心得
    flutter: 根视图、根元素与根渲染
    eggjs的参数校验模块egg-validate的使用和进一步定制化升级
    Vue源码中用到的工具函数
    21 项优化 React App 性能的技术
    react-redux 的使用
    编写 Vue.js 组件前需要知道的 10 件事
    Flutter实现抽屉动画效果
  • 原文地址:https://www.cnblogs.com/mogul/p/2975392.html
Copyright © 2020-2023  润新知