Skip to content

Commit 6275c71

Browse files
authored
Merge pull request NativeScript#2266 from NativeScript/raikov/fix-animationsync
Fixed animation value sync issues
2 parents e2999ee + d810a77 commit 6275c71

File tree

7 files changed

+202
-255
lines changed

7 files changed

+202
-255
lines changed

tns-core-modules/ui/animation/animation.android.ts

Lines changed: 37 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import enums = require("ui/enums");
88
import styleModule = require("ui/styling/style");
99
import lazy from "utils/lazy";
1010
import { CacheLayerType } from "utils/utils";
11+
import dependencyObservable = require("ui/core/dependency-observable");
1112

1213
global.moduleMerge(common, exports);
1314

@@ -75,7 +76,7 @@ export class Animation extends common.Animation implements definition.Animation
7576
this._animatorSet.start();
7677
return animationFinishedPromise;
7778
}
78-
79+
7980
public cancel(): void {
8081
super.cancel();
8182
if (trace.enabled) {
@@ -188,29 +189,26 @@ export class Animation extends common.Animation implements definition.Animation
188189
}
189190
}
190191

191-
let valueSource = this._valueSource;
192+
let valueSource = this._valueSource !== undefined ? this._valueSource : dependencyObservable.ValueSource.Local;
192193

193194
switch (propertyAnimation.property) {
194195

195196
case common.Properties.opacity:
196197
originalValue1 = nativeView.getAlpha();
197198
nativeArray = (<any>Array).create("float", 1);
198199
nativeArray[0] = propertyAnimation.value;
199-
if (this._valueSource !== undefined) {
200-
propertyUpdateCallbacks.push(checkAnimation(() => {
201-
propertyAnimation.target.style._setValue(styleModule.opacityProperty, propertyAnimation.value, valueSource);
202-
}));
203-
}
204-
else {
205-
propertyUpdateCallbacks.push(checkAnimation(() => { propertyAnimation.target.opacity = propertyAnimation.value; }));
206-
}
207-
propertyResetCallbacks.push(checkAnimation(() => { nativeView.setAlpha(originalValue1); }));
200+
propertyUpdateCallbacks.push(checkAnimation(() => {
201+
propertyAnimation.target.style._setValue(styleModule.opacityProperty, propertyAnimation.value, valueSource);
202+
}));
203+
propertyResetCallbacks.push(checkAnimation(() => {
204+
propertyAnimation.target.style._setValue(styleModule.opacityProperty, originalValue1, valueSource);
205+
}));
208206
animators.push(android.animation.ObjectAnimator.ofFloat(nativeView, "alpha", nativeArray));
209207
break;
210208

211209
case common.Properties.backgroundColor:
212210
ensureArgbEvaluator();
213-
originalValue1 = nativeView.getBackground();
211+
originalValue1 = propertyAnimation.target.backgroundColor;
214212
nativeArray = (<any>Array).create(java.lang.Object, 2);
215213
nativeArray[0] = propertyAnimation.target.backgroundColor ? java.lang.Integer.valueOf((<color.Color>propertyAnimation.target.backgroundColor).argb) : java.lang.Integer.valueOf(-1);
216214
nativeArray[1] = java.lang.Integer.valueOf((<color.Color>propertyAnimation.value).argb);
@@ -222,16 +220,12 @@ export class Animation extends common.Animation implements definition.Animation
222220
}
223221
}));
224222

225-
if (this._valueSource !== undefined) {
226-
let valueSource = this._valueSource;
227-
propertyUpdateCallbacks.push(checkAnimation(() => {
228-
propertyAnimation.target.style._setValue(styleModule.backgroundColorProperty, propertyAnimation.value, valueSource);
229-
}));
230-
}
231-
else {
232-
propertyUpdateCallbacks.push(checkAnimation(() => { propertyAnimation.target.backgroundColor = propertyAnimation.value; }));
233-
}
234-
propertyResetCallbacks.push(checkAnimation(() => { nativeView.setBackground(originalValue1); }));
223+
propertyUpdateCallbacks.push(checkAnimation(() => {
224+
propertyAnimation.target.style._setValue(styleModule.backgroundColorProperty, propertyAnimation.value, valueSource);
225+
}));
226+
propertyResetCallbacks.push(checkAnimation(() => {
227+
propertyAnimation.target.style._setValue(styleModule.backgroundColorProperty, originalValue1, valueSource);
228+
}));
235229
animators.push(animator);
236230
break;
237231

@@ -251,22 +245,14 @@ export class Animation extends common.Animation implements definition.Animation
251245
originalValue1 = nativeView.getTranslationX();
252246
originalValue2 = nativeView.getTranslationY();
253247

254-
if (this._valueSource !== undefined) {
255-
propertyUpdateCallbacks.push(checkAnimation(() => {
256-
propertyAnimation.target.style._setValue(styleModule.translateXProperty, propertyAnimation.value.x, valueSource);
257-
propertyAnimation.target.style._setValue(styleModule.translateYProperty, propertyAnimation.value.y, valueSource);
258-
}));
259-
}
260-
else {
261-
propertyUpdateCallbacks.push(checkAnimation(() => {
262-
propertyAnimation.target.translateX = propertyAnimation.value.x;
263-
propertyAnimation.target.translateY = propertyAnimation.value.y;
264-
}));
265-
}
248+
propertyUpdateCallbacks.push(checkAnimation(() => {
249+
propertyAnimation.target.style._setValue(styleModule.translateXProperty, propertyAnimation.value.x, valueSource);
250+
propertyAnimation.target.style._setValue(styleModule.translateYProperty, propertyAnimation.value.y, valueSource);
251+
}));
266252

267253
propertyResetCallbacks.push(checkAnimation(() => {
268-
nativeView.setTranslationX(originalValue1);
269-
nativeView.setTranslationY(originalValue2);
254+
propertyAnimation.target.style._setValue(styleModule.translateXProperty, originalValue1, valueSource);
255+
propertyAnimation.target.style._setValue(styleModule.translateYProperty, originalValue2, valueSource);
270256
}));
271257

272258
animatorSet = new android.animation.AnimatorSet();
@@ -291,22 +277,14 @@ export class Animation extends common.Animation implements definition.Animation
291277
originalValue1 = nativeView.getScaleX();
292278
originalValue2 = nativeView.getScaleY();
293279

294-
if (this._valueSource !== undefined) {
295-
propertyUpdateCallbacks.push(checkAnimation(() => {
296-
propertyAnimation.target.style._setValue(styleModule.scaleXProperty, propertyAnimation.value.x, valueSource);
297-
propertyAnimation.target.style._setValue(styleModule.scaleYProperty, propertyAnimation.value.y, valueSource);
298-
}));
299-
}
300-
else {
301-
propertyUpdateCallbacks.push(checkAnimation(() => {
302-
propertyAnimation.target.scaleX = propertyAnimation.value.x;
303-
propertyAnimation.target.scaleY = propertyAnimation.value.y;
304-
}));
305-
}
280+
propertyUpdateCallbacks.push(checkAnimation(() => {
281+
propertyAnimation.target.style._setValue(styleModule.scaleXProperty, propertyAnimation.value.x, valueSource);
282+
propertyAnimation.target.style._setValue(styleModule.scaleYProperty, propertyAnimation.value.y, valueSource);
283+
}));
306284

307285
propertyResetCallbacks.push(checkAnimation(() => {
308-
nativeView.setScaleY(originalValue1);
309-
nativeView.setScaleY(originalValue2);
286+
propertyAnimation.target.style._setValue(styleModule.scaleXProperty, originalValue1, valueSource);
287+
propertyAnimation.target.style._setValue(styleModule.scaleYProperty, originalValue2, valueSource);
310288
}));
311289

312290
animatorSet = new android.animation.AnimatorSet();
@@ -319,15 +297,12 @@ export class Animation extends common.Animation implements definition.Animation
319297
originalValue1 = nativeView.getRotation();
320298
nativeArray = (<any>Array).create("float", 1);
321299
nativeArray[0] = propertyAnimation.value;
322-
if (this._valueSource !== undefined) {
323-
propertyUpdateCallbacks.push(checkAnimation(() => {
324-
propertyAnimation.target.style._setValue(styleModule.rotateProperty, propertyAnimation.value, valueSource);
325-
}));
326-
}
327-
else {
328-
propertyUpdateCallbacks.push(checkAnimation(() => { propertyAnimation.target.rotate = propertyAnimation.value; }));
329-
}
330-
propertyResetCallbacks.push(checkAnimation(() => { nativeView.setRotation(originalValue1); }));
300+
propertyUpdateCallbacks.push(checkAnimation(() => {
301+
propertyAnimation.target.style._setValue(styleModule.rotateProperty, propertyAnimation.value, valueSource);
302+
}));
303+
propertyResetCallbacks.push(checkAnimation(() => {
304+
propertyAnimation.target.style._setValue(styleModule.rotateProperty, originalValue1, valueSource);
305+
}));
331306
animators.push(android.animation.ObjectAnimator.ofFloat(nativeView, "rotation", nativeArray));
332307
break;
333308

@@ -358,7 +333,7 @@ export class Animation extends common.Animation implements definition.Animation
358333
if (propertyAnimation.curve !== undefined) {
359334
animators[i].setInterpolator(propertyAnimation.curve);
360335
}
361-
336+
362337
if (trace.enabled) {
363338
trace.write("Animator created: " + animators[i], trace.categories.Animation);
364339
}
@@ -372,7 +347,7 @@ export class Animation extends common.Animation implements definition.Animation
372347
private static _getAndroidRepeatCount(iterations: number): number {
373348
return (iterations === Number.POSITIVE_INFINITY) ? android.view.animation.Animation.INFINITE : iterations - 1;
374349
}
375-
350+
376351
private _enableHardwareAcceleration() {
377352
for (let i = 0, length = this._propertyAnimations.length; i < length; i++) {
378353
let cache = <CacheLayerType>this._propertyAnimations[i].target._nativeView;
@@ -381,7 +356,7 @@ export class Animation extends common.Animation implements definition.Animation
381356
if (layerType !== android.view.View.LAYER_TYPE_HARDWARE) {
382357
cache.layerType = layerType;
383358
cache.setLayerType(android.view.View.LAYER_TYPE_HARDWARE, null);
384-
}
359+
}
385360
}
386361
}
387362
}

0 commit comments

Comments
 (0)