fix size of overlay drawable

Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
alperozturk 2024-11-15 16:37:08 +01:00 committed by Alper Öztürk
parent 5d269d00f7
commit 866531259e
3 changed files with 16 additions and 23 deletions

View file

@ -12,18 +12,15 @@ import android.graphics.Bitmap
import android.graphics.drawable.BitmapDrawable
import androidx.test.platform.app.InstrumentationRegistry
import org.junit.After
import org.junit.Assert.fail
import org.junit.Before
import org.junit.Test
class DrawableUtilTests {
private var sut: DrawableUtil? = null
private var context: Context? = null
@Before
fun setUp() {
sut = DrawableUtil()
context = InstrumentationRegistry.getInstrumentation().context
}
@ -32,18 +29,13 @@ class DrawableUtilTests {
val bitmap: Bitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888)
val drawable = BitmapDrawable(context?.resources, bitmap)
val layerDrawable = sut?.addDrawableAsOverlay(drawable, drawable)
val layerDrawable = DrawableUtil.addDrawableAsOverlay(drawable, drawable)
if (layerDrawable == null) {
fail("Layer drawable expected to be not null")
}
assert(layerDrawable?.numberOfLayers == 2)
assert(layerDrawable.numberOfLayers == 2)
}
@After
fun destroy() {
sut = null
context = null
}
}

View file

@ -7,12 +7,12 @@
*/
package com.owncloud.android.utils
import android.graphics.Rect
import android.graphics.drawable.Drawable
import android.graphics.drawable.InsetDrawable
import android.graphics.drawable.LayerDrawable
import androidx.core.graphics.drawable.DrawableCompat
class DrawableUtil {
object DrawableUtil {
fun changeColor(source: Drawable, color: Int): Drawable {
val drawable = DrawableCompat.wrap(source)
@ -21,13 +21,16 @@ class DrawableUtil {
}
fun addDrawableAsOverlay(backgroundDrawable: Drawable, overlayDrawable: Drawable): LayerDrawable {
val overlayBounds = Rect()
val overlayIconSize = backgroundDrawable.intrinsicWidth / 2
val topMargin = overlayIconSize.div(2)
overlayBounds.set(overlayIconSize, overlayIconSize + topMargin, overlayIconSize, overlayIconSize)
val overlaySizeFraction = 0.1f
val baseWidth = backgroundDrawable.intrinsicWidth
val baseHeight = backgroundDrawable.intrinsicHeight
val overlayWidth = (baseWidth * overlaySizeFraction).toInt()
val overlayHeight = (baseHeight * overlaySizeFraction).toInt()
val layerDrawable = LayerDrawable(arrayOf(backgroundDrawable, overlayDrawable))
layerDrawable.setLayerInset(1, overlayBounds.left, overlayBounds.top, overlayBounds.right, overlayBounds.bottom)
return layerDrawable
val insetLeft = (baseWidth - overlayWidth) / 2
val insetTop = (baseHeight - overlayHeight) / 2
val insetOverlay = InsetDrawable(overlayDrawable, insetLeft, overlayHeight + insetTop, insetLeft, insetTop)
return LayerDrawable(arrayOf(backgroundDrawable, insetOverlay))
}
}

View file

@ -175,16 +175,14 @@ public final class MimeTypeUtil {
return folderLayerDrawable;
}
DrawableUtil drawableUtil = new DrawableUtil();
Drawable overlayDrawable = ContextCompat.getDrawable(context, overlayIconId);
assert(overlayDrawable != null);
if (isDarkModeActive) {
overlayDrawable = drawableUtil.changeColor(overlayDrawable, R.color.dark);
overlayDrawable = DrawableUtil.INSTANCE.changeColor(overlayDrawable, R.color.dark);
}
return drawableUtil.addDrawableAsOverlay(folderDrawable, overlayDrawable);
return DrawableUtil.INSTANCE.addDrawableAsOverlay(folderDrawable, overlayDrawable);
}
/**