android 设置壁纸几种方法
总结一下,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();
}
}

浙公网安备 33010602011771号