One of the cornerstones in Material design are meaningful movements between screens. Lollipop provides support for these animations in the form of a transition framework between Activity and Fragment. Since there are not so many articles on this topic, I decided to write my own!View each other.ViewCompat.setTransitionName() . Of course, you can also just call setTransitionName() if support starts with Lollipop.android:transitionName attribute.ListView or RecyclerView will set the same name for all other elements.FragmentTransactions should be very familiar to you: getSupportFragmentManager() .beginTransaction() .addSharedElement(sharedElement, transitionName) .replace(R.id.container, newFragment) .addToBackStack(null) .commit(); addSharedElement() method.addSharedElement() View to addSharedElement() is the View from the first fragment that you want to share ( share ) with the second fragment. The transition name here is the transition name in the divided ( shared ) View in the second fragment.shared items:setSharedElementEnterTransition() method.setSharedElementReturnTransition() back, use the setSharedElementReturnTransition() method. Animation will occur when you press the back button.setEnterTransition() , setExitTransition() , setReturnTransition() , and setReenterTransition() in the appropriate snippets.Transition parameter to perform the animation.Fade ) on exit.Fade performs a fade animation. Slide animates the transition appearance / disappearance of the slide from the corner of the screen. Explode animation like an explosion, the image moves from the edges of the screen. Finally, AutoTransition will cause the image to fade, move and resize. These are just some examples from the package of movements, there are actually a lot more of them! public class DetailsTransition extends TransitionSet { public DetailsTransition() { setOrdering(ORDERING_TOGETHER); addTransition(new ChangeBounds()). addTransition(new ChangeTransform()). addTransition(new ChangeImageTransform())); } } ChangeBounds
animates the borders (position and size) of our view.ChangeTransform
animates the view scale, including the parent.ChangeImageTransform
allows us to change the size (and / or type of scale) of our imageTransition
Transition
. DetailsFragment details = DetailsFragment.newInstance(); // Note that we need the API version check here because the actual transition classes (eg Fade) // are not in the support library and are only available in API 21+. The methods we are calling on the Fragment // ARE available in the support library (though they don't do anything on API < 21) if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { details.setSharedElementEnterTransition(new DetailsTransition()); details.setEnterTransition(new Fade()); setExitTransition(new Fade()); details.setSharedElementReturnTransition(new DetailsTransition()); } getActivity().getSupportFragmentManager() .beginTransaction() .addSharedElement(holder.image, "sharedImage") .replace(R.id.container, details) .addToBackStack(null) .commit(); Source: https://habr.com/ru/post/270361/
All Articles