Skip to content

Commit

Permalink
Merge pull request #18 from edusperoni/android-r-fix
Browse files Browse the repository at this point in the history
fix: reduce lag when overriding animator
  • Loading branch information
JoshDSommer authored Apr 7, 2019
2 parents 1dd5a91 + 60d386a commit fdc9faf
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions lib/src/nativescript-ngx-shadow/common/shadow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,22 @@ if (isAndroid) {
PlainShadow = android.graphics.drawable.GradientDrawable.extend({});
}

const classCache: { [id: string]: { class: any, fieldCache: { [id: string]: number } } } = {};
// https://github.com/NativeScript/android-runtime/issues/1330
function getAndroidR(rtype: string, field: string): number {
const className = "android.R$" + rtype;
if (!classCache.hasOwnProperty(className)) {
classCache[className] = {
class: java.lang.Class.forName(className),
fieldCache: {}
};
}
if(!classCache[className].fieldCache.hasOwnProperty(field)) {
classCache[className].fieldCache[field] = +classCache[className].class.getField(field).get(null);
}
return classCache[className].fieldCache[field];
}

export class Shadow {
static DEFAULT_SHAPE = ShapeEnum.RECTANGLE;
static DEFAULT_BGCOLOR = '#FFFFFF';
Expand Down Expand Up @@ -138,7 +154,7 @@ export class Shadow {

const ObjectAnimator = android.animation.ObjectAnimator;
const AnimatorSet = android.animation.AnimatorSet;
const shortAnimTime = android.R.integer.config_shortAnimTime;
const shortAnimTime = getAndroidR("integer", "config_shortAnimTime");

const buttonDuration =
nativeView.getContext().getResources().getInteger(shortAnimTime) / 2;
Expand Down Expand Up @@ -169,10 +185,10 @@ export class Shadow {
]));

sla.addState(
[android.R.attr.state_pressed, android.R.attr.state_enabled],
[getAndroidR("attr", "state_pressed"), getAndroidR("attr", "state_enabled")],
pressedSet,
);
sla.addState([android.R.attr.state_enabled], notPressedSet);
sla.addState([getAndroidR("attr", "state_enabled")], notPressedSet);
sla.addState([], defaultSet);
nativeView.setStateListAnimator(sla);
}
Expand All @@ -197,7 +213,7 @@ export class Shadow {
nativeView.layer.shouldRasterize = data.rasterize;
nativeView.layer.rasterizationScale = screen.mainScreen.scale;
let shadowPath = null;
if(data.useShadowPath) {
if (data.useShadowPath) {
shadowPath = UIBezierPath.bezierPathWithRoundedRectCornerRadius(nativeView.bounds, nativeView.layer.shadowRadius).cgPath;
}
nativeView.layer.shadowPath = shadowPath;
Expand Down

0 comments on commit fdc9faf

Please sign in to comment.