From e501273d166b0e6f8b3746d508e97099525cea0a Mon Sep 17 00:00:00 2001 From: Martin Guillon Date: Fri, 29 Jan 2021 21:21:47 +0100 Subject: [PATCH] perf(android): faster background color setter (#9120) --- packages/core/ui/styling/background.android.ts | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/packages/core/ui/styling/background.android.ts b/packages/core/ui/styling/background.android.ts index dff881522..f63c967d6 100644 --- a/packages/core/ui/styling/background.android.ts +++ b/packages/core/ui/styling/background.android.ts @@ -44,8 +44,12 @@ export namespace ad { const constantState = drawable.getConstantState(); androidView._cachedDrawable = constantState || drawable; } - - if (isSetColorFilterOnlyWidget(nativeView) && drawable && !background.hasBorderWidth() && !background.hasBorderRadius() && !background.clipPath && !background.image && background.color) { + const isBorderDrawable = drawable instanceof org.nativescript.widgets.BorderDrawable; + const onlyColor = !background.hasBorderWidth() && !background.hasBorderRadius() && !background.clipPath && !background.image && !!background.color; + if (drawable instanceof android.graphics.drawable.ColorDrawable && onlyColor) { + drawable.setColor(background.color.android); + drawable.invalidateSelf(); + } else if (isSetColorFilterOnlyWidget(nativeView) && drawable && onlyColor) { if (drawable instanceof org.nativescript.widgets.BorderDrawable && androidView._cachedDrawable) { if (!(androidView._cachedDrawable instanceof android.graphics.drawable.Drawable.ConstantState)) { return; @@ -60,9 +64,12 @@ export namespace ad { drawable.setColorFilter(backgroundColor, android.graphics.PorterDuff.Mode.SRC_IN); drawable.invalidateSelf(); // Make sure the drawable is invalidated. Android forgets to invalidate it in some cases: toolbar (drawable).backgroundColor = backgroundColor; + } else if (!isBorderDrawable && onlyColor) { + // this is the fastest way to change only background color + nativeView.setBackgroundColor(background.color.android); } else if (!background.isEmpty()) { let backgroundDrawable = drawable as org.nativescript.widgets.BorderDrawable; - if (!(drawable instanceof org.nativescript.widgets.BorderDrawable)) { + if (!isBorderDrawable) { backgroundDrawable = new org.nativescript.widgets.BorderDrawable(layout.getDisplayDensity(), view.toString()); refreshBorderDrawable(view, backgroundDrawable); nativeView.setBackground(backgroundDrawable);