Java Code Examples for androidx.fragment.app.fragment#setSharedElementEnterTransition()

The following examples show how to use androidx.fragment.app.fragment#setSharedElementEnterTransition() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GalleryDetailScene.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTransition(Context context,
                            FragmentTransaction transaction, Fragment exit, Fragment enter) {
    if (!(enter instanceof GalleryListScene) && !(enter instanceof DownloadsScene) &&
            !(enter instanceof FavoritesScene) && !(enter instanceof HistoryScene)) {
        return false;
    }

    String transitionName = ViewCompat.getTransitionName(mThumb);
    if (transitionName != null) {
        exit.setSharedElementReturnTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_move));
        exit.setExitTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_fade));
        enter.setSharedElementEnterTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_move));
        enter.setEnterTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_fade));
        transaction.addSharedElement(mThumb, transitionName);
    }
    return true;
}
 
Example 2
Source File: EnterGalleryDetailTransaction.java    From MHViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTransition(Context context, FragmentTransaction transaction,
        Fragment exit, Fragment enter) {
    if (mThumb == null || !(enter instanceof GalleryDetailScene)) {
        return false;
    }

    String transitionName = ViewCompat.getTransitionName(mThumb);
    if (transitionName != null) {
        exit.setSharedElementReturnTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_move));
        exit.setExitTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_fade));
        enter.setSharedElementEnterTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_move));
        enter.setEnterTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_fade));
        transaction.addSharedElement(mThumb, transitionName);
    }
    return true;
}
 
Example 3
Source File: GalleryDetailScene.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTransition(Context context,
        FragmentTransaction transaction, Fragment exit, Fragment enter) {
    if (!(enter instanceof GalleryListScene) && !(enter instanceof DownloadsScene) &&
            !(enter instanceof FavoritesScene) && !(enter instanceof HistoryScene)) {
        return false;
    }

    String transitionName = ViewCompat.getTransitionName(mThumb);
    if (transitionName != null) {
        exit.setSharedElementReturnTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_move));
        exit.setExitTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_fade));
        enter.setSharedElementEnterTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_move));
        enter.setEnterTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_fade));
        transaction.addSharedElement(mThumb, transitionName);
    }
    return true;
}
 
Example 4
Source File: EnterGalleryDetailTransaction.java    From EhViewer with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onTransition(Context context, FragmentTransaction transaction,
        Fragment exit, Fragment enter) {
    if (mThumb == null || !(enter instanceof GalleryDetailScene)) {
        return false;
    }

    String transitionName = ViewCompat.getTransitionName(mThumb);
    if (transitionName != null) {
        exit.setSharedElementReturnTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_move));
        exit.setExitTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_fade));
        enter.setSharedElementEnterTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_move));
        enter.setEnterTransition(
                TransitionInflater.from(context).inflateTransition(R.transition.trans_fade));
        transaction.addSharedElement(mThumb, transitionName);
    }
    return true;
}
 
Example 5
Source File: LollipopTransitionAnimation.java    From Alligator with MIT License 5 votes vote down vote up
@Override
public void applyBeforeFragmentTransactionExecuted(@NonNull FragmentTransaction transaction, @NonNull Fragment enteringFragment, @NonNull Fragment exitingFragment) {
	enteringFragment.setEnterTransition(mEnterTransition);
	exitingFragment.setExitTransition(mExitTransition);
	enteringFragment.setSharedElementEnterTransition(mSharedElementTransition);
	enteringFragment.setAllowEnterTransitionOverlap(mAllowEnterTransitionOverlap);
	if (mSharedElements != null) {
		for (Pair<View, String> sharedElement : mSharedElements) {
			transaction.addSharedElement(sharedElement.first, sharedElement.second);
		}
	}
}
 
Example 6
Source File: LollipopTransitionAnimation.java    From Alligator with MIT License 5 votes vote down vote up
@Override
public void applyAfterFragmentTransactionExecuted(@NonNull Fragment enteringFragment, @NonNull Fragment exitingFragment) {
	enteringFragment.setEnterTransition(null);
	exitingFragment.setExitTransition(null);
	enteringFragment.setSharedElementEnterTransition(null);
	enteringFragment.setAllowEnterTransitionOverlap(true);
}
 
Example 7
Source File: TransitionContainerTransformDemoFragment.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void configureTransitions(Fragment fragment) {
  // For all 3 container layer colors, use colorSurface since this transform can be configured
  // using any fade mode and some of the start views don't have a background and the end view
  // doesn't have a background.
  int colorSurface = MaterialColors.getColor(requireView(), R.attr.colorSurface);

  MaterialContainerTransform enterContainerTransform = buildContainerTransform(true);
  enterContainerTransform.setAllContainerColors(colorSurface);
  fragment.setSharedElementEnterTransition(enterContainerTransform);
  holdTransition.setDuration(enterContainerTransform.getDuration());

  MaterialContainerTransform returnContainerTransform = buildContainerTransform(false);
  returnContainerTransform.setAllContainerColors(colorSurface);
  fragment.setSharedElementReturnTransition(returnContainerTransform);
}
 
Example 8
Source File: SharedElementTransition.java    From Kore with Apache License 2.0 5 votes vote down vote up
/**
 * Sets up the transition for the entering fragment
 * @param fragmentTransaction
 * @param fragment entering fragment
 * @param sharedElement must have the transition name set
 */
@TargetApi(21)
public void setupEnterTransition(Context context,
                                 FragmentTransaction fragmentTransaction,
                                 final Fragment fragment,
                                 View sharedElement) {
    if (!(fragment instanceof SharedElement)) {
        LogUtils.LOGD(TAG, "Enter transition fragment must implement SharedElement interface");
        return;
    }

    androidx.core.app.SharedElementCallback seCallback = new androidx.core.app.SharedElementCallback() {
        @Override
        public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
            // On returning, onMapSharedElements for the exiting fragment is called before the onMapSharedElements
            // for the reentering fragment. We use this to determine if we are returning and if
            // we should clear the shared element lists. Note that, clearing must be done in the reentering fragment
            // as this is called last. Otherwise, the app will crash during transition setup. Not sure, but might
            // be a v4 support package bug.
            if (fragment.isVisible() && (!((SharedElement) fragment).isSharedElementVisible())) {
                // shared element not visible
                clearSharedElements = true;
            }
        }
    };
    fragment.setEnterSharedElementCallback(seCallback);

    fragment.setEnterTransition(TransitionInflater
                                        .from(context)
                                        .inflateTransition(R.transition.media_details));
    fragment.setReturnTransition(null);

    Transition changeImageTransition = TransitionInflater.from(
            context).inflateTransition(R.transition.change_image);
    fragment.setSharedElementReturnTransition(changeImageTransition);
    fragment.setSharedElementEnterTransition(changeImageTransition);

    fragmentTransaction.addSharedElement(sharedElement, sharedElement.getTransitionName());
}
 
Example 9
Source File: StageActivity.java    From MHViewer with Apache License 2.0 4 votes vote down vote up
private void finishScene(String tag, TransitionHelper transitionHelper) {
    FragmentManager fragmentManager = getSupportFragmentManager();

    // Get scene
    Fragment scene = fragmentManager.findFragmentByTag(tag);
    if (scene == null) {
        Log.e(TAG, "finishScene: Can't find scene by tag: " + tag);
        return;
    }

    // Get scene index
    int index = mSceneTagList.indexOf(tag);
    if (index < 0) {
        Log.e(TAG, "finishScene: Can't find the tag in tag list: " + tag);
        return;
    }

    if (mSceneTagList.size() == 1) {
        // It is the last fragment, finish Activity now
        Log.i(TAG, "finishScene: It is the last scene, finish activity now");
        finish();
        return;
    }

    Fragment next = null;
    if (index == mSceneTagList.size() - 1) {
        // It is first fragment, show the next one
        next = fragmentManager.findFragmentByTag(mSceneTagList.get(index - 1));
    }

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    if (next != null) {
        if (transitionHelper == null || !transitionHelper.onTransition(
                this, transaction, scene, next)) {
            // Clear shared item
            scene.setSharedElementEnterTransition(null);
            scene.setSharedElementReturnTransition(null);
            scene.setEnterTransition(null);
            scene.setExitTransition(null);
            next.setSharedElementEnterTransition(null);
            next.setSharedElementReturnTransition(null);
            next.setEnterTransition(null);
            next.setExitTransition(null);
            // Do not show animate if it is not the first fragment
            transaction.setCustomAnimations(R.anim.scene_close_enter, R.anim.scene_close_exit);
        }
        // Attach fragment
        transaction.attach(next);
    }
    transaction.remove(scene);
    transaction.commitAllowingStateLoss();
    onTransactScene();

    // Remove tag
    mSceneTagList.remove(index);

    // Return result
    if (scene instanceof SceneFragment) {
        ((SceneFragment) scene).returnResult(this);
    }
}
 
Example 10
Source File: FeatureDemoUtils.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
public static void startFragmentInternal(
    FragmentActivity activity,
    Fragment fragment,
    String tag,
    @Nullable View sharedElement,
    @Nullable String sharedElementName,
    @Nullable ContainerTransformConfiguration containerTransformConfiguration) {
  FragmentTransaction transaction = activity.getSupportFragmentManager().beginTransaction();

  if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP
      && sharedElement != null
      && sharedElementName != null) {
    Fragment currentFragment = getCurrentFragment(activity);
    Hold hold = new Hold();
    // Add root view as target for the Hold so that the entire view hierarchy is held in place as
    // one instead of each child view individually. Helps keep shadows during the transition.
    hold.addTarget(currentFragment.getView());
    currentFragment.setExitTransition(hold);

    MaterialContainerTransform transform = new MaterialContainerTransform();
    if (containerTransformConfiguration != null
        && containerTransformConfiguration.isArcMotionEnabled()) {
      transform.setPathMotion(new MaterialArcMotion());
    }
    transform.setContainerColor(MaterialColors.getColor(sharedElement, R.attr.colorSurface));
    transform.setFadeMode(MaterialContainerTransform.FADE_MODE_THROUGH);
    fragment.setSharedElementEnterTransition(transform);
    transaction.addSharedElement(sharedElement, sharedElementName);

    if (fragment.getArguments() == null) {
      Bundle args = new Bundle();
      args.putString(ARG_TRANSITION_NAME, sharedElementName);
      fragment.setArguments(args);
    } else {
      fragment.getArguments().putString(ARG_TRANSITION_NAME, sharedElementName);
    }
  } else {
    transaction.setCustomAnimations(
        R.anim.abc_grow_fade_in_from_bottom,
        R.anim.abc_fade_out,
        R.anim.abc_fade_in,
        R.anim.abc_shrink_fade_out_from_bottom);
  }

  transaction
      .replace(MAIN_ACTIVITY_FRAGMENT_CONTAINER_ID, fragment, tag)
      .addToBackStack(null /* name */)
      .commit();
}
 
Example 11
Source File: StageActivity.java    From EhViewer with Apache License 2.0 4 votes vote down vote up
private void finishScene(String tag, TransitionHelper transitionHelper) {
    FragmentManager fragmentManager = getSupportFragmentManager();

    // Get scene
    Fragment scene = fragmentManager.findFragmentByTag(tag);
    if (scene == null) {
        Log.e(TAG, "finishScene: Can't find scene by tag: " + tag);
        return;
    }

    // Get scene index
    int index = mSceneTagList.indexOf(tag);
    if (index < 0) {
        Log.e(TAG, "finishScene: Can't find the tag in tag list: " + tag);
        return;
    }

    if (mSceneTagList.size() == 1) {
        // It is the last fragment, finish Activity now
        Log.i(TAG, "finishScene: It is the last scene, finish activity now");
        finish();
        return;
    }

    Fragment next = null;
    if (index == mSceneTagList.size() - 1) {
        // It is first fragment, show the next one
        next = fragmentManager.findFragmentByTag(mSceneTagList.get(index - 1));
    }

    FragmentTransaction transaction = fragmentManager.beginTransaction();
    if (next != null) {
        if (transitionHelper == null || !transitionHelper.onTransition(
                this, transaction, scene, next)) {
            // Clear shared item
            scene.setSharedElementEnterTransition(null);
            scene.setSharedElementReturnTransition(null);
            scene.setEnterTransition(null);
            scene.setExitTransition(null);
            next.setSharedElementEnterTransition(null);
            next.setSharedElementReturnTransition(null);
            next.setEnterTransition(null);
            next.setExitTransition(null);
            // Do not show animate if it is not the first fragment
            transaction.setCustomAnimations(R.anim.scene_close_enter, R.anim.scene_close_exit);
        }
        // Attach fragment
        transaction.attach(next);
    }
    transaction.remove(scene);
    transaction.commitAllowingStateLoss();
    onTransactScene();

    // Remove tag
    mSceneTagList.remove(index);

    // Return result
    if (scene instanceof SceneFragment) {
        ((SceneFragment) scene).returnResult(this);
    }
}