我有一个Bitmap
,需要将其设置为 Android锁定屏幕图像
我可以使用以下方式将Bitmap
设置为锁定屏幕:
WallpaperManager wpm = WallpaperManager.getInstance(getApplicationContext());
//method signature: setBitmap (Bitmap fullImage, Rect visibleCropHint, boolean allowBackup, int which)
wpm.setBitmap(theBitmap, null, true, FLAG_LOCK);
问题是Bitmap
在锁定屏幕上被拉长,无法保持宽高比。
我尝试将Bitmap
调整为设备屏幕尺寸,但这也不起作用:
DisplayMetrics dimension = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dimension);
int screenWidth = dimension.widthPixels;
int screenHeight = dimension.heightPixels;
int imageWidth = bitmap.getWidth();
int imageHeight = bitmap.getHeight();
float scaleWidth = ((float) screenWidth) / imageWidth;
float scaleHeight = ((float) screenHeight) / imageHeight;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap newBitmap = Bitmap.createBitmap(bitmap, 0, 0, imageWidth, imageHeight, matrix, false);
有人知道解决方案吗?