mirror of
https://github.com/NativeScript/NativeScript.git
synced 2025-08-18 13:51:27 +08:00

Switch Activity / Fragment / FragmentManager implementation from native framework to support library APIs BREAKING CHANGE: NativeScript core framework now extends support library APIs versus native framework classes as per Google's latest guidelines: - NativeScript activities now extend `android.support.v7.app.AppCompatActivity` (vs android.app.Activity) - NativeScript fragments now extend `android.support.v4.app.Fragment` (vs android.app.Fragment) - NativeScript now works internally with `android.support.v4.app.FragmentManager` (vs android.app.FragmentManager) The implications of these changes should be mostly transparent to the developer except for the fact that the support library Fragment / FragmentManager work with Animation APIs versus Animator APIs. For Android API Levels lower than 28 the new Fragment API uses a different fragment enter animation by default. You can customise the transition per navigation entry or globally via the [navigation transitions API](https://docs.nativescript.org/core-concepts/navigation#navigation-transitions) Before: Default fragment enter animation was fade animation After: Default fragment enter animation for API levels lower than 28 is now a fast "push fade" animation; default fragment enter animation for API levels equal to or greater than 28 remains fade animation Before: AndroidFragmentCallbacks interface exposed the following `onCreateAnimator(...)` method ``` ts export interface AndroidFragmentCallbacks { onCreateAnimator(fragment: any, transit: number, enter: boolean, nextAnim: number, superFunc: Function): any; // ... } ``` After: AndroidFragmentCallbacks interface now exposes the following `onCreateAnimation(...)` method instead (and `onCreateAnimator(...)` is now removed) ``` ts export interface AndroidFragmentCallbacks { onCreateAnimation(fragment: any, transit: number, enter: boolean, nextAnim: number, superFunc: Function): any; // ... } ``` Before: Transition class exposed the following abstract `createAndroidAnimator(...)` method ``` ts export class Transition { public createAndroidAnimator(transitionType: string): any; // ... } ``` After: Transition class now exposes the following abstract `createAndroidAnimation(...)` method instead (and `createAndroidAnimation(...) is now removed) ``` ts export class Transition { public createAndroidAnimation(transitionType: string): any; // ... } ``` To migrate the code of your custom transitions follow the example below: Before: ``` ts import * as transition from "tns-core-modules/ui/transition"; export class CustomTransition extends transition.Transition { constructor(duration: number, curve: any) { super(duration, curve); } public createAndroidAnimator(transitionType: string): android.animation.Animator { var scaleValues = Array.create("float", 2); switch (transitionType) { case transition.AndroidTransitionType.enter: case transition.AndroidTransitionType.popEnter: scaleValues[0] = 0; scaleValues[1] = 1; break; case transition.AndroidTransitionType.exit: case transition.AndroidTransitionType.popExit: scaleValues[0] = 1; scaleValues[1] = 0; break; } var objectAnimators = Array.create(android.animation.Animator, 2); objectAnimators[0] = android.animation.ObjectAnimator.ofFloat(null, "scaleX", scaleValues); objectAnimators[1] = android.animation.ObjectAnimator.ofFloat(null, "scaleY", scaleValues); var animatorSet = new android.animation.AnimatorSet(); animatorSet.playTogether(objectAnimators); var duration = this.getDuration(); if (duration !== undefined) { animatorSet.setDuration(duration); } animatorSet.setInterpolator(this.getCurve()); return animatorSet; } } ``` After: ``` ts import * as transition from "tns-core-modules/ui/transition"; export class CustomTransition extends transition.Transition { constructor(duration: number, curve: any) { super(duration, curve); } public createAndroidAnimation(transitionType: string): android.view.animation.Animation { const scaleValues = []; switch (transitionType) { case transition.AndroidTransitionType.enter: case transition.AndroidTransitionType.popEnter: scaleValues[0] = 0; scaleValues[1] = 1; break; case transition.AndroidTransitionType.exit: case transition.AndroidTransitionType.popExit: scaleValues[0] = 1; scaleValues[1] = 0; break; } const animationSet = new android.view.animation.AnimationSet(false); const duration = this.getDuration(); if (duration !== undefined) { animationSet.setDuration(duration); } animationSet.setInterpolator(this.getCurve()); animationSet.addAnimation( new android.view.animation.ScaleAnimation( scaleValues[0], scaleValues[1], scaleValues[0], scaleValues[1] )); return animationSet; } } ```
125 lines
5.3 KiB
TypeScript
125 lines
5.3 KiB
TypeScript
import { Transition, AndroidTransitionType } from "./transition";
|
|
|
|
// http://developer.android.com/training/animation/cardflip.html
|
|
export class FlipTransition extends Transition {
|
|
private _direction: string;
|
|
|
|
constructor(direction: string, duration: number, curve: any) {
|
|
super(duration, curve);
|
|
this._direction = direction;
|
|
}
|
|
|
|
public createAndroidAnimation(transitionType: string): android.view.animation.Animation {
|
|
ensureRotate3dAnimationClass();
|
|
|
|
let animation: android.view.animation.Animation;
|
|
let animationSet: android.view.animation.AnimationSet
|
|
let rotateAnimation: android.view.animation.Animation;
|
|
let alphaAnimation: android.view.animation.Animation;
|
|
const fullDuration = this.getDuration() || 300;
|
|
const interpolator = this.getCurve();
|
|
const rotationY = this._direction === "right" ? 180 : -180;
|
|
|
|
switch (transitionType) {
|
|
case AndroidTransitionType.enter: // card_flip_right_in
|
|
animation = new Rotate3dAnimationClass(rotationY, 0.0, 0.5, 0.5);
|
|
animation.setInterpolator(interpolator);
|
|
animation.setDuration(fullDuration);
|
|
break;
|
|
case AndroidTransitionType.exit: // card_flip_right_out
|
|
animation = animationSet = new android.view.animation.AnimationSet(false /* shareInterpolator */);
|
|
|
|
rotateAnimation = new Rotate3dAnimationClass(0.0, -rotationY, 0.5, 0.5);
|
|
rotateAnimation.setInterpolator(interpolator);
|
|
rotateAnimation.setDuration(fullDuration);
|
|
animationSet.addAnimation(rotateAnimation);
|
|
|
|
alphaAnimation = new android.view.animation.AlphaAnimation(1.0, 0.0);
|
|
alphaAnimation.setStartOffset(fullDuration / 2);
|
|
alphaAnimation.setDuration(1);
|
|
animationSet.addAnimation(alphaAnimation);
|
|
break;
|
|
case AndroidTransitionType.popEnter: // card_flip_left_in
|
|
animation = new Rotate3dAnimationClass(-rotationY, 0.0, 0.5, 0.5);
|
|
animation.setInterpolator(interpolator);
|
|
animation.setDuration(fullDuration);
|
|
break;
|
|
case AndroidTransitionType.popExit: // card_flip_left_out
|
|
animation = animationSet = new android.view.animation.AnimationSet(false /* shareInterpolator */);
|
|
|
|
rotateAnimation = new Rotate3dAnimationClass(0.0, rotationY, 0.5, 0.5);
|
|
rotateAnimation.setInterpolator(interpolator);
|
|
rotateAnimation.setDuration(fullDuration);
|
|
animationSet.addAnimation(rotateAnimation);
|
|
|
|
alphaAnimation = new android.view.animation.AlphaAnimation(1.0, 0.0);
|
|
alphaAnimation.setStartOffset(fullDuration / 2);
|
|
alphaAnimation.setDuration(1);
|
|
animationSet.addAnimation(alphaAnimation);
|
|
break;
|
|
}
|
|
|
|
return animation;
|
|
}
|
|
}
|
|
|
|
let Rotate3dAnimationClass;
|
|
function ensureRotate3dAnimationClass() {
|
|
if (Rotate3dAnimationClass) {
|
|
return;
|
|
}
|
|
|
|
/**
|
|
* Creates a new 3D rotation on the Y axis. The rotation is defined by its
|
|
* start angle and its end angle. Both angles are in degrees. The rotation
|
|
* is performed around a center point on the 2D space, definied by a pair
|
|
* of X and Y coordinates, called centerX and centerY.
|
|
*
|
|
* @param fromDegrees the start angle of the 3D rotation
|
|
* @param toDegrees the end angle of the 3D rotation
|
|
* @param centerX the X center of the 3D rotation (relative to self)
|
|
* @param centerY the Y center of the 3D rotation (relative to self)
|
|
*/
|
|
class Rotate3dAnimation extends android.view.animation.Animation {
|
|
private _camera: android.graphics.Camera;
|
|
private _pivotX: number;
|
|
private _pivotY: number;
|
|
|
|
constructor(
|
|
private _fromDegrees: number,
|
|
private _toDegrees: number,
|
|
private _centerX: number,
|
|
private _centerY: number) {
|
|
super();
|
|
return global.__native(this);
|
|
}
|
|
|
|
public initialize(width: number, height: number, parentWidth: number, parentHeight: number) {
|
|
super.initialize(width, height, parentWidth, parentHeight);
|
|
this._pivotX = this.resolveSize(android.view.animation.Animation.RELATIVE_TO_SELF, this._centerX, width, parentWidth);
|
|
this._pivotY = this.resolveSize(android.view.animation.Animation.RELATIVE_TO_SELF, this._centerY, height, parentHeight);
|
|
this._camera = new android.graphics.Camera();
|
|
}
|
|
|
|
public applyTransformation(interpolatedTime: number, t: android.view.animation.Transformation) {
|
|
const fromDegrees = this._fromDegrees;
|
|
const degrees = fromDegrees + ((this._toDegrees - fromDegrees) * interpolatedTime);
|
|
|
|
const pivotX = this._pivotX;
|
|
const pivotY = this._pivotY;
|
|
const camera = this._camera;
|
|
|
|
const matrix: android.graphics.Matrix = t.getMatrix();
|
|
|
|
camera.save();
|
|
camera.rotateY(degrees);
|
|
camera.getMatrix(matrix);
|
|
camera.restore();
|
|
|
|
matrix.preTranslate(-pivotX, -pivotY);
|
|
matrix.postTranslate(pivotX, pivotY);
|
|
}
|
|
}
|
|
|
|
Rotate3dAnimationClass = Rotate3dAnimation;
|
|
} |