总结一下,android中设置壁纸几种方法


方法一: 获取WallpaperManager实例

 private void set_mytheme(int resid){      WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);         //   Resources res = getResources();                     try {                wallpaperManager.setResource(resid);            }            catch (IOException e) {                Toast.makeText(getApplicationContext(), "更换壁纸失败,强稍后重试", 200);          }       }

方法二:

 private void set_mytheme(int resid){     getWindow().setBackgroundDrawableResource(int resid);     }

方法三:

 private synchronized void selectWallpaper(int position) {               try {            InputStream stream = getResources().openRawResource(IMAGE_IDS[position]);            setWallpaper(stream);            setResult(RESULT_OK);                    } catch (IOException e) {            Log.e(LOG_TAG, "Failed to set wallpaper " + e);        }    }

方法四:
getWindow().setBackgroundDrawable(new ClippedDrawable(wallpaper));

/**     * When a drawable is attached to a View, the View gives the Drawable its dimensions     * by calling Drawable.setBounds(). In this application, the View that draws the     * wallpaper has the same size as the screen. However, the wallpaper might be larger     * that the screen which means it will be automatically stretched. Because stretching     * a bitmap while drawing it is very expensive, we use a ClippedDrawable instead.     * This drawable simply draws another wallpaper but makes sure it is not stretched     * by always giving it its intrinsic dimensions. If the wallpaper is larger than the     * screen, it will simply get clipped but it won't impact performance.     */    private class ClippedDrawable extends Drawable {        private final Drawable mWallpaper;        public ClippedDrawable(Drawable wallpaper) {            mWallpaper = wallpaper;        }        @Override        public void setBounds(int left, int top, int right, int bottom) {            super.setBounds(left, top, right, bottom);            // Ensure the wallpaper is as large as it really is, to avoid stretching it            // at drawing time            mWallpaper.setBounds(left, top, left + mWallpaper.getIntrinsicWidth(),                    top + mWallpaper.getIntrinsicHeight());        }        public void draw(Canvas canvas) {            mWallpaper.draw(canvas);        }        public void setAlpha(int alpha) {            mWallpaper.setAlpha(alpha);        }        public void setColorFilter(ColorFilter cf) {            mWallpaper.setColorFilter(cf);        }        public int getOpacity() {            return mWallpaper.getOpacity();        }    }





更多相关文章

  1. Android学习札记17:ImageView中的setImageBitmap()方法
  2. Anfdroid网络编程方法
  3. Android:解决列表滚动时背景色变黑的方法
  4. 把android平板USB上输出LOG方法
  5. Windows下Android开发环境配置的一种方法
  6. Android Studio 3.0找不到Android Device Monitor的解决方法
  7. android 5.1 usb调试默认关闭设置方法
  8. 手机壁纸设置相关
  9. Android--ListView滑动时出现黑屏解决方法(整合)

随机推荐

  1. android API之ActivityGroup
  2. Android8.0中获取U盘的各种信息
  3. Android框架Picasso介绍
  4. PopupWindow软键盘弹出遮挡输入框
  5. 图库/相册/播放器看不到迅雷下载的视频的
  6. Android(安卓)数据持久化
  7. [Android] 问题记录 - 如何把图像变成黑白
  8. android图片上传服务器
  9. Android平台中Wifi的初始化
  10. android是如何加载资源图片的