-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
feat: overhaul and streamline Android page navigation transitions #7925
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
63fcf3d
feat: remove Animators and replace with Transitions
ADjenkov 7527507
fix: handle disappearing nested fragments for tabs.
ADjenkov 99f477c
Merge branch 'master' into djenkov/animations-overhaul
ADjenkov e5c9c5e
chore: bump webpack cycles counter
ADjenkov 864d406
feat(android-widgets): add androidx.transition:transition as dependency
ADjenkov 548992e
chore: fix typescript errors
ADjenkov da2e8a8
fix(frame-android): child already has a parent. Replace removeView wi…
ADjenkov be232d2
fix(tests): wait for fragment before isAdded() check
ADjenkov 6d29974
fix(bottom-navigation): prevent changeTab logic when fragment manager…
ADjenkov 2006de4
Merge branch 'master' into djenkov/animations-overhaul
ADjenkov 1fbeb15
chore: apply PR comments changes
ADjenkov c19cefe
Merge branch 'master' into djenkov/animations-overhaul
ADjenkov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
...ules-widgets/android/widgets/src/main/java/org/nativescript/widgets/CustomTransition.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
package org.nativescript.widgets; | ||
|
||
import android.animation.Animator; | ||
import android.animation.AnimatorListenerAdapter; | ||
import android.animation.AnimatorSet; | ||
import android.view.View; | ||
import android.view.ViewGroup; | ||
import android.view.animation.Interpolator; | ||
|
||
import androidx.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.core.view.ViewCompat; | ||
import androidx.transition.Transition; | ||
import androidx.transition.TransitionListenerAdapter; | ||
import androidx.transition.TransitionValues; | ||
import androidx.transition.Visibility; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class CustomTransition extends Visibility { | ||
private boolean resetOnTransitionEnd; | ||
private AnimatorSet animatorSet; | ||
private AnimatorSet immediateAnimatorSet; | ||
private String transitionName; | ||
|
||
public CustomTransition(AnimatorSet animatorSet, String transitionName) { | ||
this.animatorSet = animatorSet; | ||
this.transitionName = transitionName; | ||
} | ||
|
||
@Nullable | ||
@Override | ||
public Animator onAppear(@NonNull ViewGroup sceneRoot, @NonNull final View view, @Nullable TransitionValues startValues, | ||
@Nullable TransitionValues endValues) { | ||
if (endValues == null || view == null || this.animatorSet == null) { | ||
return null; | ||
} | ||
|
||
return this.setAnimatorsTarget(this.animatorSet, view); | ||
} | ||
|
||
@Override | ||
public Animator onDisappear(@NonNull ViewGroup sceneRoot, @NonNull final View view, @Nullable TransitionValues startValues, | ||
@Nullable TransitionValues endValues) { | ||
if (startValues == null || view == null || this.animatorSet == null) { | ||
return null; | ||
} | ||
|
||
return this.setAnimatorsTarget(this.animatorSet, view); | ||
} | ||
|
||
public void setResetOnTransitionEnd(boolean resetOnTransitionEnd) { | ||
this.resetOnTransitionEnd = resetOnTransitionEnd; | ||
} | ||
|
||
public String getTransitionName(){ | ||
return this.transitionName; | ||
} | ||
|
||
private Animator setAnimatorsTarget(AnimatorSet animatorSet, final View view) { | ||
ArrayList<Animator> animatorsList = animatorSet.getChildAnimations(); | ||
boolean resetOnTransitionEnd = this.resetOnTransitionEnd; | ||
|
||
for (int i = 0; i < animatorsList.size(); i++) { | ||
animatorsList.get(i).setTarget(view); | ||
} | ||
|
||
// Reset animation to its initial state to prevent mirrorered effect | ||
if (this.resetOnTransitionEnd) { | ||
this.immediateAnimatorSet = this.animatorSet.clone(); | ||
} | ||
|
||
// Switching to hardware layer during transition to improve animation performance | ||
CustomAnimatorListener listener = new CustomAnimatorListener(view); | ||
ADjenkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
animatorSet.addListener(listener); | ||
this.addListener(new CustomTransitionListenerAdapter(this)); | ||
|
||
return this.animatorSet; | ||
} | ||
|
||
private class ReverseInterpolator implements Interpolator { | ||
@Override | ||
public float getInterpolation(float paramFloat) { | ||
return Math.abs(paramFloat - 1f); | ||
} | ||
} | ||
|
||
private class CustomTransitionListenerAdapter extends TransitionListenerAdapter { | ||
private CustomTransition customTransition; | ||
|
||
CustomTransitionListenerAdapter(CustomTransition transition) { | ||
this.customTransition = transition; | ||
} | ||
|
||
@Override | ||
public void onTransitionEnd(@NonNull Transition transition) { | ||
if (this.customTransition.resetOnTransitionEnd) { | ||
ADjenkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this.customTransition.immediateAnimatorSet.setDuration(0); | ||
this.customTransition.immediateAnimatorSet.setInterpolator(new ReverseInterpolator()); | ||
this.customTransition.immediateAnimatorSet.start(); | ||
this.customTransition.setResetOnTransitionEnd(false); | ||
} | ||
|
||
this.customTransition.immediateAnimatorSet = null; | ||
this.customTransition = null; | ||
transition.removeListener(this); | ||
} | ||
} | ||
|
||
private static class CustomAnimatorListener extends AnimatorListenerAdapter { | ||
|
||
private final View mView; | ||
private boolean mLayerTypeChanged = false; | ||
|
||
CustomAnimatorListener(View view) { | ||
mView = view; | ||
} | ||
|
||
@Override | ||
public void onAnimationStart(Animator animation) { | ||
if (ViewCompat.hasOverlappingRendering(mView) | ||
&& mView.getLayerType() == View.LAYER_TYPE_NONE) { | ||
mLayerTypeChanged = true; | ||
mView.setLayerType(View.LAYER_TYPE_HARDWARE, null); | ||
} | ||
} | ||
|
||
@Override | ||
public void onAnimationEnd(Animator animation) { | ||
if (mLayerTypeChanged) { | ||
mView.setLayerType(View.LAYER_TYPE_NONE, null); | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -34,6 +34,7 @@ const ownerSymbol = Symbol("_owner"); | |
let TabFragment: any; | ||
let BottomNavigationBar: any; | ||
let AttachStateChangeListener: any; | ||
let appResources: android.content.res.Resources; | ||
|
||
function makeFragmentName(viewId: number, id: number): string { | ||
return "android:bottomnavigation:" + viewId + ":" + id; | ||
|
@@ -55,8 +56,9 @@ function initializeNativeClasses() { | |
} | ||
|
||
class TabFragmentImplementation extends org.nativescript.widgets.FragmentBase { | ||
private tab: BottomNavigation; | ||
private owner: BottomNavigation; | ||
private index: number; | ||
private backgroundBitmap: android.graphics.Bitmap = null; | ||
|
||
constructor() { | ||
super(); | ||
|
@@ -77,18 +79,61 @@ function initializeNativeClasses() { | |
public onCreate(savedInstanceState: android.os.Bundle): void { | ||
super.onCreate(savedInstanceState); | ||
const args = this.getArguments(); | ||
this.tab = getTabById(args.getInt(TABID)); | ||
this.owner = getTabById(args.getInt(TABID)); | ||
this.index = args.getInt(INDEX); | ||
if (!this.tab) { | ||
if (!this.owner) { | ||
throw new Error(`Cannot find BottomNavigation`); | ||
} | ||
} | ||
|
||
public onCreateView(inflater: android.view.LayoutInflater, container: android.view.ViewGroup, savedInstanceState: android.os.Bundle): android.view.View { | ||
const tabItem = this.tab.items[this.index]; | ||
const tabItem = this.owner.items[this.index]; | ||
|
||
return tabItem.nativeViewProtected; | ||
} | ||
|
||
public onDestroyView() { | ||
const hasRemovingParent = this.getRemovingParentFragment(); | ||
|
||
// Get view as bitmap and set it as background. This is workaround for the disapearing nested fragments. | ||
// TODO: Consider removing it when update to androidx.fragment:1.2.0 | ||
if (hasRemovingParent && this.owner.selectedIndex === this.index) { | ||
const bitmapDrawable = new android.graphics.drawable.BitmapDrawable(appResources, this.backgroundBitmap); | ||
this.owner._originalBackground = this.owner.backgroundColor || new Color("White"); | ||
this.owner.nativeViewProtected.setBackgroundDrawable(bitmapDrawable); | ||
this.backgroundBitmap = null; | ||
} | ||
|
||
super.onDestroyView(); | ||
} | ||
|
||
public onPause(): void { | ||
const hasRemovingParent = this.getRemovingParentFragment(); | ||
|
||
// Get view as bitmap and set it as background. This is workaround for the disapearing nested fragments. | ||
// TODO: Consider removing it when update to androidx.fragment:1.2.0 | ||
if (hasRemovingParent && this.owner.selectedIndex === this.index) { | ||
this.backgroundBitmap = this.loadBitmapFromView(this.owner.nativeViewProtected); | ||
} | ||
|
||
super.onPause(); | ||
} | ||
|
||
private loadBitmapFromView(view: android.view.View): android.graphics.Bitmap { | ||
// Another way to get view bitmap. Test performance vs setDrawingCacheEnabled | ||
ADjenkov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
// const width = view.getWidth(); | ||
// const height = view.getHeight(); | ||
// const bitmap = android.graphics.Bitmap.createBitmap(width, height, android.graphics.Bitmap.Config.ARGB_8888); | ||
// const canvas = new android.graphics.Canvas(bitmap); | ||
// view.layout(0, 0, width, height); | ||
// view.draw(canvas); | ||
|
||
view.setDrawingCacheEnabled(true); | ||
const bitmap = android.graphics.Bitmap.createBitmap(view.getDrawingCache()); | ||
view.setDrawingCacheEnabled(false); | ||
|
||
return bitmap; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this return null? Would |
||
} | ||
} | ||
|
||
class BottomNavigationBarImplementation extends org.nativescript.widgets.BottomNavigationBar { | ||
|
@@ -168,6 +213,7 @@ function initializeNativeClasses() { | |
TabFragment = TabFragmentImplementation; | ||
BottomNavigationBar = BottomNavigationBarImplementation; | ||
AttachStateChangeListener = new AttachListener(); | ||
appResources = application.android.context.getResources(); | ||
} | ||
|
||
function setElevation(bottomNavigationBar: org.nativescript.widgets.BottomNavigationBar) { | ||
|
@@ -196,6 +242,7 @@ export class BottomNavigation extends TabNavigationBase { | |
private _currentFragment: androidx.fragment.app.Fragment; | ||
private _currentTransaction: androidx.fragment.app.FragmentTransaction; | ||
private _attachedToWindow = false; | ||
public _originalBackground: any; | ||
|
||
constructor() { | ||
super(); | ||
|
@@ -320,6 +367,12 @@ export class BottomNavigation extends TabNavigationBase { | |
public onLoaded(): void { | ||
super.onLoaded(); | ||
|
||
if (this._originalBackground) { | ||
this.backgroundColor = null; | ||
this.backgroundColor = this._originalBackground; | ||
this._originalBackground = null; | ||
} | ||
|
||
if (this.tabStrip) { | ||
this.setTabStripItems(this.tabStrip.items); | ||
} else { | ||
|
@@ -334,8 +387,14 @@ export class BottomNavigation extends TabNavigationBase { | |
|
||
_onAttachedToWindow(): void { | ||
super._onAttachedToWindow(); | ||
|
||
this._attachedToWindow = true; | ||
|
||
// _onAttachedToWindow called from OS again after it was detach | ||
// TODO: Consider testing and removing it when update to androidx.fragment:1.2.0 | ||
if (this._manager && this._manager.isDestroyed()) { | ||
return; | ||
} | ||
|
||
this.changeTab(this.selectedIndex); | ||
} | ||
|
||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.