From 866531259e9ecd5119d18ff623c7a140156ad1d6 Mon Sep 17 00:00:00 2001 From: alperozturk Date: Fri, 15 Nov 2024 16:37:08 +0100 Subject: [PATCH] fix size of overlay drawable Signed-off-by: alperozturk --- .../android/utils/DrawableUtilTests.kt | 12 ++--------- .../owncloud/android/utils/DrawableUtil.kt | 21 +++++++++++-------- .../owncloud/android/utils/MimeTypeUtil.java | 6 ++---- 3 files changed, 16 insertions(+), 23 deletions(-) diff --git a/app/src/androidTest/java/com/owncloud/android/utils/DrawableUtilTests.kt b/app/src/androidTest/java/com/owncloud/android/utils/DrawableUtilTests.kt index 5958ff4ef6..d750163270 100644 --- a/app/src/androidTest/java/com/owncloud/android/utils/DrawableUtilTests.kt +++ b/app/src/androidTest/java/com/owncloud/android/utils/DrawableUtilTests.kt @@ -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 } } diff --git a/app/src/main/java/com/owncloud/android/utils/DrawableUtil.kt b/app/src/main/java/com/owncloud/android/utils/DrawableUtil.kt index c8aa15fb5a..d7469b0979 100644 --- a/app/src/main/java/com/owncloud/android/utils/DrawableUtil.kt +++ b/app/src/main/java/com/owncloud/android/utils/DrawableUtil.kt @@ -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)) } } diff --git a/app/src/main/java/com/owncloud/android/utils/MimeTypeUtil.java b/app/src/main/java/com/owncloud/android/utils/MimeTypeUtil.java index 4f8bbd860d..a9c835ffe1 100644 --- a/app/src/main/java/com/owncloud/android/utils/MimeTypeUtil.java +++ b/app/src/main/java/com/owncloud/android/utils/MimeTypeUtil.java @@ -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); } /**