/**
* Andorid设置手机屏幕的壁纸
*
* @description:
* @author ldm
* @date 2016-5-4 下午3:08:56
*/
public class SetWallpaperActivity extends Activity {
// WallpaperManager类:系统壁纸管理。通过它可以获得当前壁纸以及设置指定图片作为系统壁纸。
private WallpaperManager wallpaperManager;
// 壁纸对应的Drawable
private Drawable wallpaperDrawable;
// 展示样式的ImageView
private ImageView imageView;
// 随机生成图片的颜色 Button
private Button randomize;
// 设置壁纸
private Button setWallpaper;
// 暂定的一些颜色值
final static private int[] mColors = { Color.BLUE, Color.GREEN, Color.RED,
Color.LTGRAY, Color.MAGENTA, Color.CYAN, Color.YELLOW, Color.WHITE };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.set_wallpaper);
// 初始化WallpaperManager
wallpaperManager = WallpaperManager.getInstance(this);
// 获得当前系统的壁纸
wallpaperDrawable = wallpaperManager.getDrawable();
initViews();
initListeners();
}
private void initListeners() {
randomize.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
int mColor = (int) Math.floor(Math.random() * mColors.length);
// 给当前系统壁纸设置颜色
wallpaperDrawable.setColorFilter(mColors[mColor],
PorterDuff.Mode.MULTIPLY);// 取两层绘制交集
imageView.setImageDrawable(wallpaperDrawable);
// imageView.invalidate();
}
});
setWallpaper.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
try {
// 设置壁纸
wallpaperManager.setBitmap(imageView.getDrawingCache());
finish();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
private void initViews() {
imageView = (ImageView) findViewById(R.id.imageview);
imageView.setDrawingCacheEnabled(true);
imageView.setImageDrawable(wallpaperDrawable);
randomize = (Button) findViewById(R.id.randomize);
setWallpaper = (Button) findViewById(R.id.setwallpaper);
}
}
第二种
//点击设置壁纸按钮
mBtnConfirm.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
MediaLog.v(TAG, "mBtnConfirm onClick");
mDialog.show();
new Thread(new Runnable() {
@Override
public void run() {
//二次采样
Bitmap mBitmap = ImageSource.decodeSampledBitmapFromResource(
imagePath, 960, 640);
ImageSource.setwallpaper(getBaseContext(), mBitmap, 960,
640);
}
}).start();
}
});
//二次采样
public static Bitmap decodeSampledBitmapFromResource(String path,
int reqWith, int reqHeight) {
try {
Log.v("hh", "decodeSampledBitmapFromResource");
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, opts);
Log.d("hh", "decodeSampledBitmapFromResource:" + opts.outWidth
+ "," + opts.outHeight);
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
final int minSideLength = Math.min(reqWith, reqHeight);
opt.inSampleSize = computeSampleSize(opts, minSideLength, reqWith
* reqHeight);
Log.v("hh", "inSampleSize:" + opt.inSampleSize);
// 这里一定要将其设置回false,因为之前我们将其设置成了true
opt.inJustDecodeBounds = false;
Log.v("hh", "inSampleSize:" + opt.inSampleSize);
return BitmapFactory.decodeFile(path, opt);
} catch (Exception e) {
// TODO: handle exception
Log.v("hh", "decodeSampledBitmapFromResource:" + e);
return null;
}
}
public static void setwallpaper(final Context mContext,
final Bitmap mBitmap, final int w, final int h) {
Log.v("hh", "setwallpaper");
try {
WallpaperManager instance = WallpaperManager.getInstance(mContext);
instance.suggestDesiredDimensions(w, h);
instance.setBitmap(mBitmap);
SharedPreferences sp = mContext.getSharedPreferences("config", Context.MODE_PRIVATE);
Editor edit = sp.edit();
String picString = bitmapToString(mBitmap);
edit.putString("pic", picString);
edit.commit();
//销毁图片避免内存泄漏
//mBitmap.recycle();
// mBitmap1.recycle();
System.gc();
} catch (Exception e) {
// TODO Auto-generated catch block
Log.v("hh", "" + e.toString());
e.printStackTrace();
} finally {
}
}
//获取像素比例
public static int computeSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
int initialSize = computeInitialSampleSize(options, minSideLength,
maxNumOfPixels);
int roundedSize;
if (initialSize <= 8) {
roundedSize = 1;
while (roundedSize < initialSize) {
roundedSize <<= 1;
}
} else {
roundedSize = (initialSize + 7) / 8 * 8;
}
return roundedSize;
}
public static int computeInitialSampleSize(BitmapFactory.Options options,
int minSideLength, int maxNumOfPixels) {
double w = options.outWidth;
double h = options.outHeight;
int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math
.sqrt(w * h / maxNumOfPixels));
int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
Math.floor(w / minSideLength), Math.floor(h / minSideLength));
if (upperBound < lowerBound) {
return lowerBound;
}
if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
return 1;
} else if (minSideLength == -1) {
return lowerBound;
} else {
return upperBound;
}
}