Skip to content

Commit

Permalink
fix: zoom resize method doesn't center wallpaper
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jan 12, 2024
1 parent dd928a9 commit 2cce007
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions app/src/main/java/com/bnyro/wallpaper/util/WallpaperHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import android.content.res.Resources
import android.graphics.Bitmap
import android.os.Build
import android.util.DisplayMetrics
import android.util.Log
import android.view.WindowManager
import androidx.annotation.RequiresApi
import com.bnyro.wallpaper.enums.ResizeMethod
Expand Down Expand Up @@ -81,25 +82,20 @@ object WallpaperHelper {
private fun getZoomedBitmap(bitmap: Bitmap, screenWidth: Int, screenHeight: Int): Bitmap {
val bitmapRatio = bitmap.height.toFloat() / bitmap.width.toFloat()
val screenRatio = screenHeight.toFloat() / screenWidth.toFloat()
val scaleRatio = (bitmapRatio / screenRatio)

val resizedBitmap = if (screenRatio > bitmapRatio) {
getResizedBitmap(bitmap, screenWidth, (screenWidth * bitmapRatio).toInt(), false)
var (newWidth, newHeight) = bitmap.width to bitmap.height
if (bitmapRatio > screenRatio) {
newHeight = (scaleRatio * bitmap.height).toInt()
} else {
getResizedBitmap(bitmap, (screenHeight / bitmapRatio).toInt(), screenHeight, false)
newWidth = (scaleRatio * bitmap.width).toInt()
}

val bitmapGapX = ((resizedBitmap.width - screenWidth) / 2).absoluteValue
val bitmapGapY = ((resizedBitmap.height - screenHeight) / 2).absoluteValue

return runCatching {
Bitmap.createBitmap(
resizedBitmap,
bitmapGapX,
bitmapGapY,
screenWidth,
screenHeight
)
}.getOrDefault(resizedBitmap)
val gapX = (bitmap.width - newWidth) / 2
val gapY = (bitmap.height - newHeight) / 2
val centeredBitmap = Bitmap.createBitmap(bitmap, gapX, gapY, newWidth, newHeight)

return getResizedBitmap(centeredBitmap, screenWidth, screenHeight)
}

private fun getBitmapFitWidth(bitmap: Bitmap, width: Int): Bitmap {
Expand Down

0 comments on commit 2cce007

Please sign in to comment.