fix addDrawableAsOverlay

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-11-15 17:29:09 +01:00 committed by Alper Öztürk
parent d137b71024
commit 64443475cb

View file

@ -7,8 +7,8 @@
*/ */
package com.owncloud.android.utils package com.owncloud.android.utils
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable import android.graphics.drawable.Drawable
import android.graphics.drawable.InsetDrawable
import android.graphics.drawable.LayerDrawable import android.graphics.drawable.LayerDrawable
import androidx.core.graphics.drawable.DrawableCompat import androidx.core.graphics.drawable.DrawableCompat
@ -21,16 +21,24 @@ object DrawableUtil {
} }
fun addDrawableAsOverlay(backgroundDrawable: Drawable, overlayDrawable: Drawable): LayerDrawable { fun addDrawableAsOverlay(backgroundDrawable: Drawable, overlayDrawable: Drawable): LayerDrawable {
val overlaySizeFraction = 0.1f val containerDrawable = LayerDrawable(arrayOf(backgroundDrawable, overlayDrawable))
val baseWidth = backgroundDrawable.intrinsicWidth
val baseHeight = backgroundDrawable.intrinsicHeight
val overlayWidth = (baseWidth * overlaySizeFraction).toInt()
val overlayHeight = (baseHeight * overlaySizeFraction).toInt()
val insetLeft = (baseWidth - overlayWidth) / 2 val overlayWidth = overlayDrawable.intrinsicWidth
val insetTop = (baseHeight - overlayHeight) / 2 val overlayHeight = overlayDrawable.intrinsicHeight
val backgroundWidth = backgroundDrawable.intrinsicWidth
val backgroundHeight = backgroundDrawable.intrinsicHeight
val insetOverlay = InsetDrawable(overlayDrawable, insetLeft, overlayHeight + insetTop, insetLeft, insetTop) val scaleFactor = 2f / maxOf(overlayWidth, overlayHeight)
return LayerDrawable(arrayOf(backgroundDrawable, insetOverlay)) val scaledOverlayWidth = (overlayWidth * scaleFactor).toInt()
val scaledOverlayHeight = (overlayHeight * scaleFactor).toInt()
val left = (backgroundWidth - scaledOverlayWidth) / 2
val top = (backgroundHeight - scaledOverlayHeight) / 2
val topMargin = 2
containerDrawable.setLayerInset(1, left, top + topMargin, left, top)
(overlayDrawable as? BitmapDrawable)?.setBounds(0, 0, scaledOverlayWidth, scaledOverlayHeight)
return containerDrawable
} }
} }