mirror of
https://github.com/nextcloud/android.git
synced 2024-12-19 15:33:00 +03:00
fix size of overlay drawable
Signed-off-by: alperozturk <alper_ozturk@proton.me>
This commit is contained in:
parent
5d269d00f7
commit
866531259e
3 changed files with 16 additions and 23 deletions
|
@ -12,18 +12,15 @@ import android.graphics.Bitmap
|
||||||
import android.graphics.drawable.BitmapDrawable
|
import android.graphics.drawable.BitmapDrawable
|
||||||
import androidx.test.platform.app.InstrumentationRegistry
|
import androidx.test.platform.app.InstrumentationRegistry
|
||||||
import org.junit.After
|
import org.junit.After
|
||||||
import org.junit.Assert.fail
|
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Test
|
import org.junit.Test
|
||||||
|
|
||||||
class DrawableUtilTests {
|
class DrawableUtilTests {
|
||||||
|
|
||||||
private var sut: DrawableUtil? = null
|
|
||||||
private var context: Context? = null
|
private var context: Context? = null
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
fun setUp() {
|
fun setUp() {
|
||||||
sut = DrawableUtil()
|
|
||||||
context = InstrumentationRegistry.getInstrumentation().context
|
context = InstrumentationRegistry.getInstrumentation().context
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,18 +29,13 @@ class DrawableUtilTests {
|
||||||
val bitmap: Bitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888)
|
val bitmap: Bitmap = Bitmap.createBitmap(2, 2, Bitmap.Config.ARGB_8888)
|
||||||
val drawable = BitmapDrawable(context?.resources, bitmap)
|
val drawable = BitmapDrawable(context?.resources, bitmap)
|
||||||
|
|
||||||
val layerDrawable = sut?.addDrawableAsOverlay(drawable, drawable)
|
val layerDrawable = DrawableUtil.addDrawableAsOverlay(drawable, drawable)
|
||||||
|
|
||||||
if (layerDrawable == null) {
|
assert(layerDrawable.numberOfLayers == 2)
|
||||||
fail("Layer drawable expected to be not null")
|
|
||||||
}
|
|
||||||
|
|
||||||
assert(layerDrawable?.numberOfLayers == 2)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
fun destroy() {
|
fun destroy() {
|
||||||
sut = null
|
|
||||||
context = null
|
context = null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,12 +7,12 @@
|
||||||
*/
|
*/
|
||||||
package com.owncloud.android.utils
|
package com.owncloud.android.utils
|
||||||
|
|
||||||
import android.graphics.Rect
|
|
||||||
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
|
||||||
|
|
||||||
class DrawableUtil {
|
object DrawableUtil {
|
||||||
|
|
||||||
fun changeColor(source: Drawable, color: Int): Drawable {
|
fun changeColor(source: Drawable, color: Int): Drawable {
|
||||||
val drawable = DrawableCompat.wrap(source)
|
val drawable = DrawableCompat.wrap(source)
|
||||||
|
@ -21,13 +21,16 @@ class DrawableUtil {
|
||||||
}
|
}
|
||||||
|
|
||||||
fun addDrawableAsOverlay(backgroundDrawable: Drawable, overlayDrawable: Drawable): LayerDrawable {
|
fun addDrawableAsOverlay(backgroundDrawable: Drawable, overlayDrawable: Drawable): LayerDrawable {
|
||||||
val overlayBounds = Rect()
|
val overlaySizeFraction = 0.1f
|
||||||
val overlayIconSize = backgroundDrawable.intrinsicWidth / 2
|
val baseWidth = backgroundDrawable.intrinsicWidth
|
||||||
val topMargin = overlayIconSize.div(2)
|
val baseHeight = backgroundDrawable.intrinsicHeight
|
||||||
overlayBounds.set(overlayIconSize, overlayIconSize + topMargin, overlayIconSize, overlayIconSize)
|
val overlayWidth = (baseWidth * overlaySizeFraction).toInt()
|
||||||
|
val overlayHeight = (baseHeight * overlaySizeFraction).toInt()
|
||||||
|
|
||||||
val layerDrawable = LayerDrawable(arrayOf(backgroundDrawable, overlayDrawable))
|
val insetLeft = (baseWidth - overlayWidth) / 2
|
||||||
layerDrawable.setLayerInset(1, overlayBounds.left, overlayBounds.top, overlayBounds.right, overlayBounds.bottom)
|
val insetTop = (baseHeight - overlayHeight) / 2
|
||||||
return layerDrawable
|
|
||||||
|
val insetOverlay = InsetDrawable(overlayDrawable, insetLeft, overlayHeight + insetTop, insetLeft, insetTop)
|
||||||
|
return LayerDrawable(arrayOf(backgroundDrawable, insetOverlay))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -175,16 +175,14 @@ public final class MimeTypeUtil {
|
||||||
return folderLayerDrawable;
|
return folderLayerDrawable;
|
||||||
}
|
}
|
||||||
|
|
||||||
DrawableUtil drawableUtil = new DrawableUtil();
|
|
||||||
|
|
||||||
Drawable overlayDrawable = ContextCompat.getDrawable(context, overlayIconId);
|
Drawable overlayDrawable = ContextCompat.getDrawable(context, overlayIconId);
|
||||||
assert(overlayDrawable != null);
|
assert(overlayDrawable != null);
|
||||||
|
|
||||||
if (isDarkModeActive) {
|
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);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Reference in a new issue