fix: all scaling modes now produce exact-canvas-sized bitmaps

retrieveBitmap previously returned bitmaps sized to the image content (e.g.
1994x2992 for FIT on a portrait image), leaving the launcher to scale/position
them — which silently broke the chosen scaling mode:

  FIT   → narrower than canvas → launcher scaled up → cropped height again
  FILL  → taller than canvas  → launcher behaviour undefined / distorted
  NONE  → smaller than canvas → launcher stretched it

New finalizeToCanvas() step ensures every path returns a bitmap of exactly
canvasW×canvasH pixels:
  FILL   → center-crop the fill-scaled source to canvas size;
           upscale first when BitmapFactory fallback returns a small source
  FIT    → center the fit-scaled source on a black canvas (letterbox/pillarbox)
  NONE   → center the original-size source on a black canvas
  STRETCH → source was decoded to exact canvas dimensions; no-op

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Anthonyy232andClaude Sonnet 4.6 committed 2026-03-28 15:06:05 -07:00
1 parent 8d805ad17d
commit 6884559b6b
1 file changed
+74 -1
@@ -254,7 +254,80 @@ fun retrieveBitmap(
}
// Only apply EXIF orientation for the BitmapFactory path — ImageDecoder already handles it
return if (decodedWithImageDecoder) bitmap else bitmap?.let { applyExifOrientation(it, wallpaperUri, context) }
val oriented = if (decodedWithImageDecoder) bitmap else bitmap?.let { applyExifOrientation(it, wallpaperUri, context) }
// Composite onto an exact (width × height) canvas so WallpaperManager.setBitmap() receives
// a bitmap that already matches the desired canvas dimensions. Without this step the launcher
// still has to scale/position the bitmap, which undoes the scaling mode the user chose:
// FILL → decoded bitmap may be taller than canvas; center-crop to canvas size.
// FIT → decoded bitmap may be narrower/shorter than canvas; center on black canvas.
// NONE → original-size image may be smaller than canvas; center on black canvas.
// STRETCH → decoder was told the exact canvas size; no-op.
return oriented?.let { finalizeToCanvas(it, width, height, scaling) }
}
/**
* Produce a bitmap of exactly [canvasW] × [canvasH] pixels according to [scaling]:
*
* - FILL: the source already fills (or overflows) the canvas because [retrieveBitmap]
* decoded it at fill-scale. Center-crop any overflow.
* - FIT: the source fits within the canvas but may leave empty margins.
* Draw it centered on a black [canvasW]×[canvasH] bitmap.
* - STRETCH: [retrieveBitmap] decoded to the exact canvas size; nothing to do.
* - NONE: the source is at most canvas-sized (possibly smaller).
* Draw it centered on a black [canvasW]×[canvasH] bitmap.
*/
private fun finalizeToCanvas(source: Bitmap, canvasW: Int, canvasH: Int, scaling: ScalingType): Bitmap {
val sw = source.width
val sh = source.height
return when (scaling) {
ScalingType.FILL -> {
// Primary path (ImageDecoder): source was decoded at fill-scale so it is
// >= canvasW wide AND >= canvasH tall. Center-crop to exact canvas size.
// Fallback path (BitmapFactory): source may be smaller if the image needed
// upscaling — scale up first, then center-crop.
val fillSrc = if (sw < canvasW || sh < canvasH) {
val scale = maxOf(canvasW.toFloat() / sw, canvasH.toFloat() / sh)
val scaledW = (sw * scale).fastRoundToInt()
val scaledH = (sh * scale).fastRoundToInt()
val scaled = Bitmap.createScaledBitmap(source, scaledW, scaledH, true)
if (scaled !== source) source.recycle()
scaled
} else source
val fsw = fillSrc.width
val fsh = fillSrc.height
if (fsw == canvasW && fsh == canvasH) return fillSrc
val x = ((fsw - canvasW) / 2).coerceAtLeast(0)
val y = ((fsh - canvasH) / 2).coerceAtLeast(0)
val cropped = Bitmap.createBitmap(fillSrc, x, y, canvasW, canvasH)
if (cropped !== fillSrc) fillSrc.recycle()
cropped
}
ScalingType.FIT, ScalingType.NONE -> {
// FIT / NONE: source fits within the canvas; center it on a black background.
if (sw == canvasW && sh == canvasH) return source
compositeCenter(source, canvasW, canvasH)
}
ScalingType.STRETCH -> source // decoder was given exact canvas dimensions
}
}
/**
* Draw [source] centered on a new [canvasW]×[canvasH] black bitmap and return the result.
* [source] is recycled unless it is the same object as the returned bitmap.
*/
private fun compositeCenter(source: Bitmap, canvasW: Int, canvasH: Int): Bitmap {
val canvas = Bitmap.createBitmap(canvasW, canvasH, Bitmap.Config.ARGB_8888)
val c = Canvas(canvas)
c.drawColor(Color.BLACK)
val offsetX = ((canvasW - source.width) / 2f).coerceAtLeast(0f)
val offsetY = ((canvasH - source.height) / 2f).coerceAtLeast(0f)
c.drawBitmap(source, offsetX, offsetY, null)
source.recycle()
return canvas
}
/**