下面列出了android.app.WallpaperManager#setBitmap ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void echo(String image, Boolean base64, Context context)
{
try
{
AssetManager assetManager = context.getAssets();
Bitmap bitmap;
if(base64) //Base64 encoded
{
byte[] decoded = android.util.Base64.decode(image, android.util.Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(decoded, 0, decoded.length);
}
else //normal path
{
InputStream instr = assetManager.open("www/" + image);
bitmap = BitmapFactory.decodeStream(instr);
}
WallpaperManager myWallpaperManager = WallpaperManager.getInstance(context);
myWallpaperManager.setBitmap(bitmap);
Log.d("console", "homescreen wallpaper set");
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void handleCrop(int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
mImageView.setImageURI(Crop.getOutput(result));
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try {
Bitmap mBitmap = getImageBitmap();
myWallpaperManager.setBitmap(mBitmap);
Toast.makeText(DetailActivity.this, "Wallpaper set",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(DetailActivity.this,
"Error setting wallpaper", Toast.LENGTH_SHORT)
.show();
}
} else if (resultCode == Crop.RESULT_ERROR) {
Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
}
}
private void handleCrop(int resultCode, Intent result) {
if (resultCode == RESULT_OK) {
mImageView.setImageURI(Crop.getOutput(result));
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());
try {
Bitmap mBitmap = getImageBitmap();
myWallpaperManager.setBitmap(mBitmap);
Toast.makeText(DetailActivity.this, "Wallpaper set",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(DetailActivity.this,
"Error setting wallpaper", Toast.LENGTH_SHORT)
.show();
}
} else if (resultCode == Crop.RESULT_ERROR) {
Toast.makeText(this, Crop.getError(result).getMessage(), Toast.LENGTH_SHORT).show();
}
}
public void setWallpaper(View v) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getApplicationContext());
try {
wallpaperManager.setBitmap(wallpaper);
Toast toast = Toast.makeText(this, "wallpaper set", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 使用Bitmap设置壁纸
* 直接设置为壁纸,不会有任何界面和弹窗出现
* 壁纸切换,会有动态的渐变切换
*
* @param view
*/
public void onSetWallpaperForBitmap(View view) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
try {
Bitmap wallpaperBitmap = BitmapFactory.decodeResource(getResources(), R.raw.girl);
wallpaperManager.setBitmap(wallpaperBitmap);
// 已过时的Api
// setWallpaper(wallpaperBitmap);
// setWallpaper(getResources().openRawResource(R.raw.girl));
} catch (IOException e) {
e.printStackTrace();
}
}
protected void onHandleIntent(final Intent intent) {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.astrobee_logo_modified_background);
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
Rect cropHints = new Rect(630, 0, 2250, 1620);
try {
manager.setBitmap(bitmap, cropHints, false);
} catch (IOException e) {
e.printStackTrace();
}
}
public static void setWallPaper(Context context,File file){
WallpaperManager manager = WallpaperManager.getInstance(context);
try {
manager.setBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected Boolean doInBackground(Bitmap... params) {
Bitmap bitmap = params[0];
try {
if (bitmap != null) {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(mContext);
wallpaperManager.setBitmap(bitmap);
return true;
}
} catch (Exception e) {
e.printStackTrace();
}
return false;
}
@SuppressLint("WrongConstant")
public static void setBitmap(final Bitmap bitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(xdrip.getAppContext());
final Bitmap wallpaper = BitmapUtil.getTiled(bitmap, getScreenWidth(), getScreenHeight(), isLockScreenBitmapTiled(), Pref.getString(NumberWallPreview.ViewModel.PREF_numberwall_background, null));
wallpaperManager.setBitmap(wallpaper, null, false, FLAG_LOCK);
wallpaper.recycle();
} catch (Exception e) {
UserError.Log.e(TAG, "Failed to set wallpaper: " + e);
}
}
}
@SuppressLint("WrongConstant")
public static void setBitmap(final Bitmap bitmap) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
try {
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(xdrip.getAppContext());
final Bitmap wallpaper = BitmapUtil.getTiled(bitmap, getScreenWidth(), getScreenHeight(), isLockScreenBitmapTiled(), Pref.getString(NumberWallPreview.ViewModel.PREF_numberwall_background, null));
wallpaperManager.setBitmap(wallpaper, null, false, FLAG_LOCK);
wallpaper.recycle();
} catch (Exception e) {
UserError.Log.e(TAG, "Failed to set wallpaper: " + e);
}
}
}
private void setWallpaper () {
WallpaperManager manager = WallpaperManager.getInstance(this);
boolean canSetWallpaper = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
canSetWallpaper &= manager.isWallpaperSupported();
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
canSetWallpaper &= manager.isSetWallpaperAllowed();
}
if (canSetWallpaper) {
Song song = PlayManager.getInstance(this).getCurrentSong();
if (song != null) {
Album album = song.getAlbumObj();
if (album == null) {
return;
}
File source = new File(album.getAlbumArt());
if (source.exists()) {
Bitmap bmp = BitmapFactory.decodeFile(source.getAbsolutePath());
try {
manager.setBitmap(bmp);
} catch (IOException e) {
e.printStackTrace();
} finally {
bmp.recycle();
}
}
}
}
}
public void setlockwp(String image, Boolean base64, Context context)
{
try
{
AssetManager assetManager = context.getAssets();
Bitmap bitmap;
if(base64) //Base64 encoded
{
byte[] decoded = android.util.Base64.decode(image, android.util.Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(decoded, 0, decoded.length);
}
else //normal path
{
InputStream instr = assetManager.open("www/" + image);
bitmap = BitmapFactory.decodeStream(instr);
}
if (android.os.Build.VERSION.SDK_INT>=24) {
WallpaperManager ujWallpaperManager = WallpaperManager.getInstance(context);
ujWallpaperManager.setBitmap(bitmap, null, true, WallpaperManager.FLAG_LOCK);
Log.d("console", "lockscreen wallpaper set");
}
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected Void doInBackground(Void... params) {
try {
final WallpaperManager wallpaperManager = WallpaperManager.getInstance(activity);
Bitmap bitmap = MediaStore.Images.Media.getBitmap(activity.getContentResolver(), Uri.fromFile(new File(url)));
wallpaperManager.setBitmap(bitmap);
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
/**
* Set wallpaper
*/
private void setWallpaperNow(Bitmap source) throws IOException
{
DisplayMetrics dm = CommonUtils.getDisplayMetrics(this);
int status_bar_height = CommonUtils.getStatusBarHeight(this);
getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
int screenHeight = dm.heightPixels - status_bar_height;
int bitmapWidth = source.getWidth();
int bitmapHeight = source.getHeight();
float scaleX = (screenWidth + 0.0f) / bitmapWidth;
float scaleY = (screenHeight + 0.0f) / bitmapHeight;
Log.D(TAG, "[scaleX=" + scaleX + ", scaleY=" + scaleY + "]");
Bitmap tempBitmap = null;
Bitmap targetBitmap = null;
try
{
// 按比例缩放达到适配手机屏幕
// targetBitmap = Bitmap.createBitmap(screenWidth, screenHeight,
// Config.ARGB_8888);
Matrix matrix = new Matrix();
matrix.postScale(scaleX, scaleY);
tempBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
Log.D(TAG, "TargetBitmap: [width=" + tempBitmap.getWidth() + ", height=" + tempBitmap.getHeight() + "]");
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
targetBitmap = Bitmap.createBitmap(screenWidth * 3, screenHeight, Config.ARGB_8888);
Canvas canvas = new Canvas(targetBitmap);
canvas.drawColor(Color.RED);
canvas.drawBitmap(tempBitmap, 0, 0, null);
canvas.drawBitmap(tempBitmap, screenWidth, 0, null);
canvas.drawBitmap(tempBitmap, screenWidth * 2, 0, null);
wallpaperManager.setBitmap(targetBitmap);
showToastOnUiThread(R.string.hint_wallpaper_ok);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (tempBitmap != null && !tempBitmap.isRecycled())
{
tempBitmap.recycle();
tempBitmap = null;
}
if (targetBitmap != null && !targetBitmap.isRecycled())
{
targetBitmap.recycle();
targetBitmap = null;
}
}
}