Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ open class ShadowLayout(context: Context) : FrameLayout(context) {

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
bitmap?.recycle()
bitmap = null
recycleShadowBitmap()
invalidateShadow = true
}

var isShadowed: Boolean = false
Expand Down Expand Up @@ -108,6 +108,7 @@ open class ShadowLayout(context: Context) : FrameLayout(context) {
if (isShadowed) {
if (invalidateShadow) {
if (bounds.width() != 0 && bounds.height() != 0) {
recycleShadowBitmap()
bitmap = Bitmap.createBitmap(bounds.width(), bounds.height(), Bitmap.Config.ARGB_8888).also {
mainCanvas.setBitmap(it)
invalidateShadow = false
Expand All @@ -132,4 +133,10 @@ open class ShadowLayout(context: Context) : FrameLayout(context) {

super.dispatchDraw(canvas)
}

private fun recycleShadowBitmap() {
mainCanvas.setBitmap(null)
bitmap?.recycle()
bitmap = null
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.reactnativenavigation.views.bottomtabs

import android.graphics.Bitmap
import android.graphics.Canvas
import android.view.View
import com.reactnativenavigation.BaseTest
import org.junit.Assert.assertNotSame
import org.junit.Assert.assertTrue
import org.junit.Test

class ShadowLayoutTest : BaseTest() {
@Test
fun regeneratingShadowRecyclesPreviousBitmap() {
val view = ShadowLayout(newActivity()).apply { isShadowed = true }
val output = Bitmap.createBitmap(40, 40, Bitmap.Config.ARGB_8888)

measureAndDraw(view, output)
val firstShadow = shadowBitmap(view)

view.requestLayout()
measureAndDraw(view, output)
val secondShadow = shadowBitmap(view)

assertNotSame(firstShadow, secondShadow)
assertTrue(firstShadow.isRecycled)
output.recycle()
}

private fun measureAndDraw(view: ShadowLayout, output: Bitmap) {
val size = View.MeasureSpec.makeMeasureSpec(40, View.MeasureSpec.EXACTLY)
view.measure(size, size)
view.layout(0, 0, 40, 40)
view.draw(Canvas(output))
}

private fun shadowBitmap(view: ShadowLayout): Bitmap {
val field = ShadowLayout::class.java.getDeclaredField("bitmap")
field.isAccessible = true
return field.get(view) as Bitmap
}
}