下面列出了android.support.annotation.TransitionRes#android.transition.Transition 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private void switchToPreview(Uri uri) {
Fragment fragment = FragmentPreview.instance(uri);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
int duration = getResources().getInteger(android.R.integer.config_mediumAnimTime);
Slide slideTransition = new Slide(Gravity.LEFT);
slideTransition.setDuration(duration);
setExitTransition(slideTransition);
Slide slideTransition2 = new Slide(Gravity.RIGHT);
slideTransition2.setDuration(duration);
fragment.setEnterTransition(slideTransition2);
TransitionInflater inflater = TransitionInflater.from(getContext());
Transition transition = inflater.inflateTransition(R.transition.transition_default);
fragment.setSharedElementEnterTransition(transition);
}
getFragmentManager()
.beginTransaction()
.replace(R.id.frg_docker, fragment)
.addSharedElement(mBtnFindWrapper, ViewCompat.getTransitionName(mBtnFindWrapper))
.addToBackStack("select")
.commitAllowingStateLoss();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private TransitionSet constructTransitions() {
int[] slideTargets = {R.id.ChatRecyclerView, R.id.chat_input, R.id.chat_input_divider};
Transition slideTransition = new Slide(Gravity.BOTTOM);
Transition fadeTransition = new Fade();
for (int slideTarget : slideTargets) {
slideTransition.addTarget(slideTarget);
fadeTransition.excludeTarget(slideTarget, true);
}
TransitionSet set = new TransitionSet();
set.addTransition(slideTransition);
set.addTransition(fadeTransition);
return set;
}
@Override
protected void viewsReady(ArrayMap<String, View> sharedElements) {
super.viewsReady(sharedElements);
mIsReadyForTransition = true;
hideViews(mSharedElements);
Transition viewsTransition = getViewsTransition();
if (viewsTransition != null && mTransitioningViews != null) {
removeExcludedViews(viewsTransition, mTransitioningViews);
stripOffscreenViews();
hideViews(mTransitioningViews);
}
if (mIsReturning) {
sendSharedElementDestination();
} else {
moveSharedElementsToOverlay();
}
if (mSharedElementsBundle != null) {
onTakeSharedElements();
}
}
/**
* This method is used for fragment transitions for ordered transactions to change the
* enter and exit transition targets after the call to
* {@link TransitionManager#beginDelayedTransition(ViewGroup, Transition)}. The exit transition
* must ensure that it does not target any Views and the enter transition must start targeting
* the Views of the incoming Fragment.
*
* @param sceneRoot The fragment container View
* @param inFragment The last fragment that is entering
* @param nonExistentView A view that does not exist in the hierarchy that is used as a
* transition target to ensure no View is targeted.
* @param sharedElementsIn The shared element Views of the incoming fragment
* @param enterTransition The enter transition of the incoming fragment
* @param enteringViews The entering Views of the incoming fragment
* @param exitTransition The exit transition of the outgoing fragment
* @param exitingViews The exiting views of the outgoing fragment
*/
private static void scheduleTargetChange(final ViewGroup sceneRoot,
final Fragment inFragment, final View nonExistentView,
final ArrayList<View> sharedElementsIn,
final Transition enterTransition, final ArrayList<View> enteringViews,
final Transition exitTransition, final ArrayList<View> exitingViews) {
OneShotPreDrawListener.add(sceneRoot, () -> {
if (enterTransition != null) {
enterTransition.removeTarget(nonExistentView);
ArrayList<View> views = configureEnteringExitingViews(
enterTransition, inFragment, sharedElementsIn, nonExistentView);
enteringViews.addAll(views);
}
if (exitingViews != null) {
if (exitTransition != null) {
ArrayList<View> tempExiting = new ArrayList<>();
tempExiting.add(nonExistentView);
replaceTargets(exitTransition, exitingViews, tempExiting);
}
exitingViews.clear();
exitingViews.add(nonExistentView);
}
});
}
/**
* Returns a TransitionSet containing the shared element transition. The wrapping TransitionSet
* targets all shared elements to ensure that no other Views are targeted. The shared element
* transition can then target any or all shared elements without worrying about accidentally
* targeting entering or exiting Views.
*
* @param inFragment The incoming fragment
* @param outFragment the outgoing fragment
* @param isPop True if this is a pop transaction or false if it is a normal (add) transaction.
* @return A TransitionSet wrapping the shared element transition or null if no such transition
* exists.
*/
private static TransitionSet getSharedElementTransition(Fragment inFragment,
Fragment outFragment, boolean isPop) {
if (inFragment == null || outFragment == null) {
return null;
}
Transition transition = cloneTransition(isPop
? outFragment.getSharedElementReturnTransition()
: inFragment.getSharedElementEnterTransition());
if (transition == null) {
return null;
}
TransitionSet transitionSet = new TransitionSet();
transitionSet.addTransition(transition);
return transitionSet;
}
/**
* After the transition has started, remove all targets that we added to the transitions
* so that the transitions are left in a clean state.
*/
private static void scheduleRemoveTargets(final Transition overalTransition,
final Transition enterTransition, final ArrayList<View> enteringViews,
final Transition exitTransition, final ArrayList<View> exitingViews,
final TransitionSet sharedElementTransition, final ArrayList<View> sharedElementsIn) {
overalTransition.addListener(new TransitionListenerAdapter() {
@Override
public void onTransitionStart(Transition transition) {
if (enterTransition != null) {
replaceTargets(enterTransition, enteringViews, null);
}
if (exitTransition != null) {
replaceTargets(exitTransition, exitingViews, null);
}
if (sharedElementTransition != null) {
replaceTargets(sharedElementTransition, sharedElementsIn, null);
}
}
});
}
/**
* This method adds views as targets to the transition, but only if the transition
* doesn't already have a target. It is best for views to contain one View object
* that does not exist in the view hierarchy (state.nonExistentView) so that
* when they are removed later, a list match will suffice to remove the targets.
* Otherwise, if you happened to have targeted the exact views for the transition,
* the replaceTargets call will remove them unexpectedly.
*/
public static void addTargets(Transition transition, ArrayList<View> views) {
if (transition == null) {
return;
}
if (transition instanceof TransitionSet) {
TransitionSet set = (TransitionSet) transition;
int numTransitions = set.getTransitionCount();
for (int i = 0; i < numTransitions; i++) {
Transition child = set.getTransitionAt(i);
addTargets(child, views);
}
} else if (!hasSimpleTarget(transition)) {
List<View> targets = transition.getTargets();
if (isNullOrEmpty(targets)) {
// We can just add the target views
int numViews = views.size();
for (int i = 0; i < numViews; i++) {
transition.addTarget(views.get(i));
}
}
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
ButterKnife.bind(this);
if (getIntent() != null) {
int avatar = getIntent().getIntExtra(getString(R.string.transition_shared_avatar), 0);
String title = getIntent().getStringExtra(getString(R.string.transition_shared_title));
if (avatar > 0) {
imageAvatar.setImageResource(avatar);
colorize(((BitmapDrawable) imageAvatar.getDrawable()).getBitmap());
}
if (title != null) {
textTitle.setText(title);
}
Transition transition =
TransitionInflater.from(this).inflateTransition(R.transition.slide_bottom);
getWindow().setEnterTransition(transition);
}
}
private void setupWindowAnimations() {
Transition transition = null;
switch (mSlideType) {
case SLIDE_CODE:
transition = buildEnterTransitionByCode();
break;
case SLIDE_XML:
transition = buildEnterTransitionByXml();
break;
default:
break;
}
getWindow().setEnterTransition(transition);
}
public void goAnimate(View view) {
//Resource file solution
ViewGroup root = findViewById(R.id.layout);
Scene scene = Scene.getSceneForLayout(root, R.layout.activity_main_end, this);
Transition transition = TransitionInflater.from(this)
.inflateTransition(R.transition.transition_move);
TransitionManager.go(scene, transition);
//Code only solution
// ViewGroup root = findViewById(R.id.layout);
// Scene scene = new Scene(root);
//
// Transition transition = new ChangeBounds();
// TransitionManager.beginDelayedTransition(root,transition);
//
// TextView textViewTop = findViewById(R.id.textViewTop);
// RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)textViewTop.getLayoutParams();
// params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,1);
// params.addRule(RelativeLayout.ALIGN_PARENT_TOP, 0);
// textViewTop.setLayoutParams(params);
//
// TextView textViewBottom = findViewById(R.id.textViewBottom);
// params = (RelativeLayout.LayoutParams) textViewBottom.getLayoutParams();
// params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,0);
// params.addRule(RelativeLayout.ALIGN_PARENT_TOP, 1);
// textViewBottom.setLayoutParams(params);
//
// TransitionManager.go(scene);
}
public static void cleanupTransitions(final View sceneRoot, final View nonExistentView,
Object enterTransitionObject, final ArrayList<View> enteringViews,
Object exitTransitionObject, final ArrayList<View> exitingViews,
Object sharedElementTransitionObject, final ArrayList<View> sharedElementTargets,
Object overallTransitionObject, final ArrayList<View> hiddenViews,
final Map<String, View> renamedViews) {
final Transition enterTransition = (Transition) enterTransitionObject;
final Transition exitTransition = (Transition) exitTransitionObject;
final Transition sharedElementTransition = (Transition) sharedElementTransitionObject;
final Transition overallTransition = (Transition) overallTransitionObject;
if (overallTransition != null) {
sceneRoot.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
public boolean onPreDraw() {
sceneRoot.getViewTreeObserver().removeOnPreDrawListener(this);
if (enterTransition != null) {
enterTransition.removeTarget(nonExistentView);
removeTargets(enterTransition, enteringViews);
}
if (exitTransition != null) {
removeTargets(exitTransition, exitingViews);
}
if (sharedElementTransition != null) {
removeTargets(sharedElementTransition, sharedElementTargets);
}
for (Map.Entry<String, View> entry : renamedViews.entrySet()) {
View view = entry.getValue();
String name = entry.getKey();
view.setTransitionName(name);
}
int numViews = hiddenViews.size();
for (int i = 0; i < numViews; i++) {
overallTransition.excludeTarget(hiddenViews.get(i), false);
}
overallTransition.excludeTarget(nonExistentView, false);
return true;
}
});
}
}
@SuppressLint("NewApi")
private void setupWindowAnimations() {
Transition slideLeft = TransitionInflater.from(this).inflateTransition(android.R.transition.slide_left);
Transition slideRight = TransitionInflater.from(this).inflateTransition(android.R.transition.slide_right);
getWindow().setExitTransition(slideLeft);
getWindow().setEnterTransition(slideRight);
getWindow().setBackgroundDrawable(new ColorDrawable(Color.DKGRAY));
}
private void setupWindowAnimations() {
Transition transition = null;
switch (mSlideType) {
case SLIDE_CODE:
transition = buildEnterTransitionByCode();
break;
case SLIDE_XML:
transition = buildEnterTransitionByXml();
break;
default:
break;
}
getWindow().setEnterTransition(transition);
}
/**
* Returns a clone of a transition or null if it is null
*/
private static Transition cloneTransition(Transition transition) {
if (transition != null) {
transition = transition.clone();
}
return transition;
}
/**
* Returns the View in the incoming Fragment that should be used as the epicenter.
*
* @param inSharedElements The mapping of shared element names to Views in the
* incoming fragment.
* @param fragments A structure holding the transitioning fragments in this container.
* @param enterTransition The transition used for the incoming Fragment's views
* @param inIsPop Is the incoming fragment being added as a pop transaction?
*/
private static View getInEpicenterView(ArrayMap<String, View> inSharedElements,
FragmentContainerTransition fragments,
Transition enterTransition, boolean inIsPop) {
BackStackRecord inTransaction = fragments.lastInTransaction;
if (enterTransition != null && inSharedElements != null
&& inTransaction.mSharedElementSourceNames != null
&& !inTransaction.mSharedElementSourceNames.isEmpty()) {
final String targetName = inIsPop
? inTransaction.mSharedElementSourceNames.get(0)
: inTransaction.mSharedElementTargetNames.get(0);
return inSharedElements.get(targetName);
}
return null;
}
/**
* Solve TransitionManager leak problem
*/
public static void removeActivityFromTransitionManager(Activity activity) {
final Class transitionManagerClass = TransitionManager.class;
try {
final Field runningTransitionsField = transitionManagerClass.getDeclaredField("sRunningTransitions");
if (runningTransitionsField == null) {
return;
}
runningTransitionsField.setAccessible(true);
//noinspection unchecked
final ThreadLocal<WeakReference<ArrayMap<ViewGroup, ArrayList<Transition>>>> runningTransitions
= (ThreadLocal<WeakReference<ArrayMap<ViewGroup, ArrayList<Transition>>>>)
runningTransitionsField.get(transitionManagerClass);
if (runningTransitions == null
|| runningTransitions.get() == null
|| runningTransitions.get().get() == null) {
return;
}
final ArrayMap map = runningTransitions.get().get();
final View decorView = activity.getWindow().getDecorView();
if (map.containsKey(decorView)) {
map.remove(decorView);
}
} catch (Exception e) {
e.printStackTrace();
}
}
static void include(Object transition, View targetView) {
((Transition) transition).addTarget(targetView);
}
@Override
public void onTransitionEnd(Transition transition) {
setExitSharedElementCallback(null);
}
@Override
public void onTransitionCancel(Transition transition) {}
public void onTransitionStart(Transition paramTransition) {
}
static void addTransition(Object transitionSet, Object transition) {
((TransitionSet) transitionSet).addTransition((Transition) transition);
}
@Nullable
public Transition getEnterTransition() {
return mEnterTransition;
}
@Override
public void onTransitionCancel(Transition transition) {
onTransitionEnd(transition);
}
@Override
public void onTransitionResume(Transition transition) {
}
@Override
public void onTransitionResume(Transition transition) {
// Nothing special here
}
@Override
public void onTransitionStart(Transition transition) {
}
@Override
public void onTransitionEnd(Transition transition) {
}
@Override
public void onTransitionPause(Transition transition) {
// Nothing special here
}
@Override
public void onTransitionResume(Transition transition) {
onTransitionStart(transition);
}
@Override
public Transition getExitTransition() {
throw new IllegalStateException("You can't call this!");
}