From 8da610832945a937f34b772d2944422b37d8f372 Mon Sep 17 00:00:00 2001 From: SOUP Date: Mon, 16 Nov 2020 00:36:16 +0900 Subject: [PATCH] Library: Reduce items jank in RecyclerView (#25) --- .../soup/neumorphism/NeumorphShapeDrawable.kt | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/neumorphism/src/main/java/soup/neumorphism/NeumorphShapeDrawable.kt b/neumorphism/src/main/java/soup/neumorphism/NeumorphShapeDrawable.kt index 3fe7ec0..50cc5e8 100644 --- a/neumorphism/src/main/java/soup/neumorphism/NeumorphShapeDrawable.kt +++ b/neumorphism/src/main/java/soup/neumorphism/NeumorphShapeDrawable.kt @@ -237,7 +237,10 @@ class NeumorphShapeDrawable : Drawable { } override fun invalidateSelf() { - dirty = true + // To reduce jank in RecyclerView. + if (isVisibleChanging.not()) { + dirty = true + } super.invalidateSelf() } @@ -265,6 +268,15 @@ class NeumorphShapeDrawable : Drawable { && strokePaint.strokeWidth > 0) } + private var isVisibleChanging = false + + override fun setVisible(visible: Boolean, restart: Boolean): Boolean { + isVisibleChanging = true + return super.setVisible(visible, restart).apply { + isVisibleChanging = false + } + } + override fun onBoundsChange(bounds: Rect) { dirty = true super.onBoundsChange(bounds) @@ -324,7 +336,12 @@ class NeumorphShapeDrawable : Drawable { CornerFamily.ROUNDED -> { path.addRoundRect( left, top, right, bottom, - shapeAppearanceModel.getCornerRadii(min(bounds.width() / 2f, bounds.height() / 2f)), + shapeAppearanceModel.getCornerRadii( + min( + bounds.width() / 2f, + bounds.height() / 2f + ) + ), Path.Direction.CW ) }