image: fix composition aliasing

This commit is contained in:
Alexander Capehart
2026-01-24 19:39:45 -07:00
parent 7a0548185b
commit 0aa8aeabed
4 changed files with 64 additions and 14 deletions
@@ -398,6 +398,7 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
coverCollectionFanAngle(uidSeed), coverCollectionFanAngle(uidSeed),
coverCollectionTiltAngle(uidSeed), coverCollectionTiltAngle(uidSeed),
coverCollectionZOrder(uidSeed), coverCollectionZOrder(uidSeed),
coverCollectionBackgroundColor(),
), ),
context.getString(R.string.desc_artist_image, artist.name), context.getString(R.string.desc_artist_image, artist.name),
R.drawable.ic_artist_24, R.drawable.ic_artist_24,
@@ -417,7 +418,8 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
GalleryCoverCollection( GalleryCoverCollection(
genre.covers, genre.covers,
coverCollectionCornerRatio(), coverCollectionCornerRatio(),
coverCollectionZOrder(genre.uid.hashCode()), coverCollectionZOrder(genre.uid.toString().hashCode()),
coverCollectionBackgroundColor(),
), ),
context.getString(R.string.desc_genre_image, genre.name), context.getString(R.string.desc_genre_image, genre.name),
R.drawable.ic_genre_24, R.drawable.ic_genre_24,
@@ -435,7 +437,8 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
StackCoverCollection( StackCoverCollection(
playlist.covers, playlist.covers,
coverCollectionCornerRatio(), coverCollectionCornerRatio(),
coverCollectionZOrder(playlist.uid.hashCode()), coverCollectionZOrder(playlist.uid.toString().hashCode()),
coverCollectionBackgroundColor(),
), ),
context.getString(R.string.desc_playlist_image, playlist.name), context.getString(R.string.desc_playlist_image, playlist.name),
R.drawable.ic_playlist_24, R.drawable.ic_playlist_24,
@@ -518,6 +521,9 @@ constructor(context: Context, attrs: AttributeSet? = null, @AttrRes defStyleAttr
return random.nextFloat() * 20f - 10f // -10 to +10 degrees return random.nextFloat() * 20f - 10f // -10 to +10 degrees
} }
private fun coverCollectionBackgroundColor(): Int =
context.getColorCompat(R.color.sel_cover_bg).defaultColor
private fun resolveCornerBounds(): RectF? { private fun resolveCornerBounds(): RectF? {
val widthPx = resolveSizePx(width, measuredWidth, layoutParams?.width) val widthPx = resolveSizePx(width, measuredWidth, layoutParams?.width)
val heightPx = resolveSizePx(height, measuredHeight, layoutParams?.height) val heightPx = resolveSizePx(height, measuredHeight, layoutParams?.height)
@@ -28,6 +28,7 @@ import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode import android.graphics.PorterDuffXfermode
import android.graphics.Rect import android.graphics.Rect
import android.graphics.RectF import android.graphics.RectF
import androidx.annotation.ColorInt
import androidx.core.graphics.createBitmap import androidx.core.graphics.createBitmap
import androidx.core.graphics.drawable.toDrawable import androidx.core.graphics.drawable.toDrawable
import coil3.ImageLoader import coil3.ImageLoader
@@ -61,6 +62,7 @@ data class GalleryCoverCollection(
val covers: CoverCollection, val covers: CoverCollection,
val cornerRadiusRatio: Float, val cornerRadiusRatio: Float,
val zOrder: List<Int>, val zOrder: List<Int>,
@ColorInt val backgroundColor: Int,
) )
class GalleryComposeFetcher class GalleryComposeFetcher
@@ -106,6 +108,7 @@ private constructor(
outputSizePx = outputSize, outputSizePx = outputSize,
gapWidthPx = outputSize * ComposeCoverDefaults.GAP_RATIO, gapWidthPx = outputSize * ComposeCoverDefaults.GAP_RATIO,
cornerRadiusPx = cornerRadiusPx, cornerRadiusPx = cornerRadiusPx,
backgroundColor = data.backgroundColor,
zOrder = data.zOrder.validatedZOrder(), zOrder = data.zOrder.validatedZOrder(),
), ),
) )
@@ -128,6 +131,7 @@ private constructor(
val outputSizePx: Int, val outputSizePx: Int,
val gapWidthPx: Float, val gapWidthPx: Float,
val cornerRadiusPx: Float, val cornerRadiusPx: Float,
@ColorInt val backgroundColor: Int,
val zOrder: List<Int> = listOf(0, 1, 2, 3), val zOrder: List<Int> = listOf(0, 1, 2, 3),
) )
@@ -139,7 +143,19 @@ private constructor(
val result = createBitmap(config.outputSizePx, config.outputSizePx) val result = createBitmap(config.outputSizePx, config.outputSizePx)
val canvas = Canvas(result) val canvas = Canvas(result)
val imagePaint = Paint(Paint.ANTI_ALIAS_FLAG) canvas.drawColor(config.backgroundColor)
val gapPaint =
Paint(Paint.ANTI_ALIAS_FLAG).apply {
color = config.backgroundColor
style = Paint.Style.FILL
}
val imagePaint =
Paint(Paint.ANTI_ALIAS_FLAG).apply {
// Use the background color for the mask so anti-aliased edges blend
// toward it, avoiding dark/light fringing artifacts
color = config.backgroundColor
}
val totalSize = config.outputSizePx.toFloat() val totalSize = config.outputSizePx.toFloat()
val cardSize = totalSize * ComposeCoverDefaults.CARD_SIZE_PERCENT val cardSize = totalSize * ComposeCoverDefaults.CARD_SIZE_PERCENT
@@ -253,6 +269,8 @@ private constructor(
continue continue
} }
canvas.drawPath(visiblePath, gapPaint)
if (tile.innerRect.width() > 0 && tile.innerRect.height() > 0) { if (tile.innerRect.width() > 0 && tile.innerRect.height() > 0) {
val clipSave = canvas.save() val clipSave = canvas.save()
canvas.clipPath(visiblePath) canvas.clipPath(visiblePath)
@@ -305,7 +323,8 @@ private constructor(
class Keyer @Inject constructor() : CoilKeyer<GalleryCoverCollection> { class Keyer @Inject constructor() : CoilKeyer<GalleryCoverCollection> {
override fun key(data: GalleryCoverCollection, options: Options): String { override fun key(data: GalleryCoverCollection, options: Options): String {
val config = "${data.cornerRadiusRatio}.${data.zOrder.joinToString(".")}" val config =
"${data.cornerRadiusRatio}.${data.zOrder.joinToString(".")}.${data.backgroundColor}"
return "g:${data.covers.hashCode()}.${options.size.width}.${options.size.height}.$config" return "g:${data.covers.hashCode()}.${options.size.width}.${options.size.height}.$config"
} }
} }
@@ -28,6 +28,7 @@ import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode import android.graphics.PorterDuffXfermode
import android.graphics.Rect import android.graphics.Rect
import android.graphics.RectF import android.graphics.RectF
import androidx.annotation.ColorInt
import androidx.core.graphics.createBitmap import androidx.core.graphics.createBitmap
import androidx.core.graphics.drawable.toDrawable import androidx.core.graphics.drawable.toDrawable
import androidx.core.graphics.withRotation import androidx.core.graphics.withRotation
@@ -66,6 +67,7 @@ data class SmatteringCoverCollection(
val fanAngleDeg: Float, val fanAngleDeg: Float,
val tiltAngleDeg: Float, val tiltAngleDeg: Float,
val zOrder: List<Int>, val zOrder: List<Int>,
@ColorInt val backgroundColor: Int,
) )
class SmatteringComposeFetcher class SmatteringComposeFetcher
@@ -113,6 +115,7 @@ private constructor(
cornerRadiusPx = cornerRadiusPx, cornerRadiusPx = cornerRadiusPx,
fanAngleDeg = data.fanAngleDeg, fanAngleDeg = data.fanAngleDeg,
tiltAngleDeg = data.tiltAngleDeg, tiltAngleDeg = data.tiltAngleDeg,
backgroundColor = data.backgroundColor,
zOrder = data.zOrder.validatedZOrder(), zOrder = data.zOrder.validatedZOrder(),
), ),
) )
@@ -137,6 +140,7 @@ private constructor(
val cornerRadiusPx: Float, val cornerRadiusPx: Float,
val fanAngleDeg: Float, val fanAngleDeg: Float,
val tiltAngleDeg: Float, val tiltAngleDeg: Float,
@ColorInt val backgroundColor: Int,
val zOrder: List<Int> = listOf(0, 1, 2, 3), val zOrder: List<Int> = listOf(0, 1, 2, 3),
) )
@@ -148,11 +152,19 @@ private constructor(
val result = createBitmap(config.outputSizePx, config.outputSizePx) val result = createBitmap(config.outputSizePx, config.outputSizePx)
val canvas = Canvas(result) val canvas = Canvas(result)
val clearPaint = canvas.drawColor(config.backgroundColor)
val gapPaint =
Paint(Paint.ANTI_ALIAS_FLAG).apply { Paint(Paint.ANTI_ALIAS_FLAG).apply {
xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR) color = config.backgroundColor
style = Paint.Style.FILL
}
val imagePaint =
Paint(Paint.ANTI_ALIAS_FLAG).apply {
// Use the background color for the mask so anti-aliased edges blend
// toward it, avoiding dark/light fringing artifacts
color = config.backgroundColor
} }
val imagePaint = Paint(Paint.ANTI_ALIAS_FLAG)
val totalSize = config.outputSizePx.toFloat() val totalSize = config.outputSizePx.toFloat()
val cardSize = totalSize * ComposeCoverDefaults.CARD_SIZE_PERCENT val cardSize = totalSize * ComposeCoverDefaults.CARD_SIZE_PERCENT
@@ -242,7 +254,7 @@ private constructor(
} }
canvas.withRotation(rotation, centerX, centerY) { canvas.withRotation(rotation, centerX, centerY) {
drawPath(gapPath, clearPaint) drawPath(gapPath, gapPaint)
if (innerRect.width() > 0 && innerRect.height() > 0) { if (innerRect.width() > 0 && innerRect.height() > 0) {
val savedLayer = saveLayer(innerRect, null) val savedLayer = saveLayer(innerRect, null)
@@ -316,7 +328,7 @@ private constructor(
class Keyer @Inject constructor() : CoilKeyer<SmatteringCoverCollection> { class Keyer @Inject constructor() : CoilKeyer<SmatteringCoverCollection> {
override fun key(data: SmatteringCoverCollection, options: Options): String { override fun key(data: SmatteringCoverCollection, options: Options): String {
val config = val config =
"${data.cornerRadiusRatio}.${data.fanAngleDeg}.${data.tiltAngleDeg}.${data.zOrder.joinToString(".")}" "${data.cornerRadiusRatio}.${data.fanAngleDeg}.${data.tiltAngleDeg}.${data.zOrder.joinToString(".")}.${data.backgroundColor}"
return "m:${data.covers.hashCode()}.${options.size.width}.${options.size.height}.$config" return "m:${data.covers.hashCode()}.${options.size.width}.${options.size.height}.$config"
} }
} }
@@ -28,6 +28,7 @@ import android.graphics.PorterDuff
import android.graphics.PorterDuffXfermode import android.graphics.PorterDuffXfermode
import android.graphics.Rect import android.graphics.Rect
import android.graphics.RectF import android.graphics.RectF
import androidx.annotation.ColorInt
import androidx.core.graphics.createBitmap import androidx.core.graphics.createBitmap
import androidx.core.graphics.drawable.toDrawable import androidx.core.graphics.drawable.toDrawable
import coil3.ImageLoader import coil3.ImageLoader
@@ -61,6 +62,7 @@ data class StackCoverCollection(
val covers: CoverCollection, val covers: CoverCollection,
val cornerRadiusRatio: Float, val cornerRadiusRatio: Float,
val zOrder: List<Int>, val zOrder: List<Int>,
@ColorInt val backgroundColor: Int,
) )
class StackComposeFetcher class StackComposeFetcher
@@ -106,6 +108,7 @@ private constructor(
outputSizePx = outputSize, outputSizePx = outputSize,
gapWidthPx = outputSize * ComposeCoverDefaults.GAP_RATIO, gapWidthPx = outputSize * ComposeCoverDefaults.GAP_RATIO,
cornerRadiusPx = cornerRadiusPx, cornerRadiusPx = cornerRadiusPx,
backgroundColor = data.backgroundColor,
zOrder = data.zOrder.validatedZOrder(), zOrder = data.zOrder.validatedZOrder(),
), ),
) )
@@ -128,6 +131,7 @@ private constructor(
val outputSizePx: Int, val outputSizePx: Int,
val gapWidthPx: Float, val gapWidthPx: Float,
val cornerRadiusPx: Float, val cornerRadiusPx: Float,
@ColorInt val backgroundColor: Int,
val zOrder: List<Int> = listOf(0, 1, 2, 3), val zOrder: List<Int> = listOf(0, 1, 2, 3),
) )
@@ -139,11 +143,19 @@ private constructor(
val result = createBitmap(config.outputSizePx, config.outputSizePx) val result = createBitmap(config.outputSizePx, config.outputSizePx)
val canvas = Canvas(result) val canvas = Canvas(result)
val clearPaint = canvas.drawColor(config.backgroundColor)
val gapPaint =
Paint(Paint.ANTI_ALIAS_FLAG).apply { Paint(Paint.ANTI_ALIAS_FLAG).apply {
xfermode = PorterDuffXfermode(PorterDuff.Mode.CLEAR) color = config.backgroundColor
style = Paint.Style.FILL
}
val imagePaint =
Paint(Paint.ANTI_ALIAS_FLAG).apply {
// Use the background color for the mask so anti-aliased edges blend
// toward it, avoiding dark/light fringing artifacts
color = config.backgroundColor
} }
val imagePaint = Paint(Paint.ANTI_ALIAS_FLAG)
val totalSize = config.outputSizePx.toFloat() val totalSize = config.outputSizePx.toFloat()
val inset = 0f val inset = 0f
@@ -223,7 +235,7 @@ private constructor(
Path.Direction.CW, Path.Direction.CW,
) )
} }
canvas.drawPath(gapPath, clearPaint) canvas.drawPath(gapPath, gapPaint)
} }
if (innerRect.width() > 0 && innerRect.height() > 0) { if (innerRect.width() > 0 && innerRect.height() > 0) {
@@ -294,7 +306,8 @@ private constructor(
class Keyer @Inject constructor() : CoilKeyer<StackCoverCollection> { class Keyer @Inject constructor() : CoilKeyer<StackCoverCollection> {
override fun key(data: StackCoverCollection, options: Options): String { override fun key(data: StackCoverCollection, options: Options): String {
val config = "${data.cornerRadiusRatio}.${data.zOrder.joinToString(".")}" val config =
"${data.cornerRadiusRatio}.${data.zOrder.joinToString(".")}.${data.backgroundColor}"
return "s:${data.covers.hashCode()}.${options.size.width}.${options.size.height}.$config" return "s:${data.covers.hashCode()}.${options.size.width}.${options.size.height}.$config"
} }
} }