Skip to content

Commit fdc9faf

Browse files
authored
Merge pull request #18 from edusperoni/android-r-fix
fix: reduce lag when overriding animator
2 parents 1dd5a91 + 60d386a commit fdc9faf

File tree

1 file changed

+20
-4
lines changed
  • lib/src/nativescript-ngx-shadow/common

1 file changed

+20
-4
lines changed

lib/src/nativescript-ngx-shadow/common/shadow.ts

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,22 @@ if (isAndroid) {
2121
PlainShadow = android.graphics.drawable.GradientDrawable.extend({});
2222
}
2323

24+
const classCache: { [id: string]: { class: any, fieldCache: { [id: string]: number } } } = {};
25+
// https://github.com/NativeScript/android-runtime/issues/1330
26+
function getAndroidR(rtype: string, field: string): number {
27+
const className = "android.R$" + rtype;
28+
if (!classCache.hasOwnProperty(className)) {
29+
classCache[className] = {
30+
class: java.lang.Class.forName(className),
31+
fieldCache: {}
32+
};
33+
}
34+
if(!classCache[className].fieldCache.hasOwnProperty(field)) {
35+
classCache[className].fieldCache[field] = +classCache[className].class.getField(field).get(null);
36+
}
37+
return classCache[className].fieldCache[field];
38+
}
39+
2440
export class Shadow {
2541
static DEFAULT_SHAPE = ShapeEnum.RECTANGLE;
2642
static DEFAULT_BGCOLOR = '#FFFFFF';
@@ -138,7 +154,7 @@ export class Shadow {
138154

139155
const ObjectAnimator = android.animation.ObjectAnimator;
140156
const AnimatorSet = android.animation.AnimatorSet;
141-
const shortAnimTime = android.R.integer.config_shortAnimTime;
157+
const shortAnimTime = getAndroidR("integer", "config_shortAnimTime");
142158

143159
const buttonDuration =
144160
nativeView.getContext().getResources().getInteger(shortAnimTime) / 2;
@@ -169,10 +185,10 @@ export class Shadow {
169185
]));
170186

171187
sla.addState(
172-
[android.R.attr.state_pressed, android.R.attr.state_enabled],
188+
[getAndroidR("attr", "state_pressed"), getAndroidR("attr", "state_enabled")],
173189
pressedSet,
174190
);
175-
sla.addState([android.R.attr.state_enabled], notPressedSet);
191+
sla.addState([getAndroidR("attr", "state_enabled")], notPressedSet);
176192
sla.addState([], defaultSet);
177193
nativeView.setStateListAnimator(sla);
178194
}
@@ -197,7 +213,7 @@ export class Shadow {
197213
nativeView.layer.shouldRasterize = data.rasterize;
198214
nativeView.layer.rasterizationScale = screen.mainScreen.scale;
199215
let shadowPath = null;
200-
if(data.useShadowPath) {
216+
if (data.useShadowPath) {
201217
shadowPath = UIBezierPath.bezierPathWithRoundedRectCornerRadius(nativeView.bounds, nativeView.layer.shadowRadius).cgPath;
202218
}
203219
nativeView.layer.shadowPath = shadowPath;

0 commit comments

Comments
 (0)