调整位图大小以适合锁定屏幕,并保持宽高比

时间:2018-09-10 17:31:55

标签: java android android-bitmap lockscreen wallpapermanager

我有一个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);

有人知道解决方案吗?

0 个答案:

没有答案