zoukankan      html  css  js  c++  java
  • 设置壁纸

     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" />

  • 相关阅读:
    yii修改密码 弹出修改成功提示(八)
    yii模型规则的简单使用以及开启前端的验证(七)
    利用ab测试并发可靠么
    yii数据库链接(六)
    初始化collectionViewCell
    监听事件
    ControllerEvent
    代码构建视图
    DeveloperAppleHelp
    Xcode 小技巧
  • 原文地址:https://www.cnblogs.com/mogul/p/2975392.html
Copyright © 2011-2022 走看看