See: https://workday.github.io/ui/2017/09/05/androidinbox-material-transitions-for-recyclerview
Last active
December 5, 2018 00:18
-
-
Save ShinichiroFunatsu/e60cd4ea8005ede6cca90f4dd492614c to your computer and use it in GitHub Desktop.
FragmentTransaction setCustomAnimations
This file contains 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
class ExplodeFadeOut : Explode() { | |
init { | |
propagation = null | |
} | |
override fun onAppear(sceneRoot: ViewGroup?, view: View?, startValues: TransitionValues?, | |
endValues: TransitionValues?): Animator { | |
val explodeAnimator = super.onAppear(sceneRoot, view, startValues, endValues) | |
val fadeInAnimator = ObjectAnimator.ofFloat(view, View.ALPHA, 0f, 1f) | |
return animatorSet(explodeAnimator, fadeInAnimator) | |
} | |
override fun onDisappear(sceneRoot: ViewGroup?, view: View?, startValues: TransitionValues?, | |
endValues: TransitionValues?): Animator { | |
val explodeAnimator = super.onDisappear(sceneRoot, view, startValues, endValues) | |
val fadeOutAnimator = ObjectAnimator.ofFloat(view, View.ALPHA, 1f, 0f) | |
return animatorSet(explodeAnimator, fadeOutAnimator) | |
} | |
private fun animatorSet(explodeAnimator: Animator, fadeAnimator: Animator): AnimatorSet { | |
val animatorSet = AnimatorSet() | |
animatorSet.play(explodeAnimator).with(fadeAnimator) | |
return animatorSet | |
} | |
} |
This file contains 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
class ItemFragment : Fragment() { | |
override fun onCreateView( | |
inflater: LayoutInflater, container: ViewGroup?, | |
savedInstanceState: Bundle? | |
): View? { | |
val view = inflater.inflate(R.layout.fragment_item_list, container, false) | |
// Set the adapter | |
if (view is RecyclerView) { | |
with(view) { | |
layoutManager = LinearLayoutManager(context) | |
adapter = MyItemRecyclerViewAdapter(DummyContent.ITEMS) { v, item -> | |
with(this@ItemFragment) { | |
exitTransition = getListFragmentExitTransition(v) | |
reenterTransition = getListFragmentExitTransition(v) | |
} | |
val f = DetailFragment.newInstance(item).apply { | |
exitTransition = Fade().setInterpolator(AnticipateInterpolator()) | |
enterTransition = Fade().setInterpolator(AnticipateInterpolator()) | |
reenterTransition = Fade().setInterpolator(DecelerateInterpolator()) | |
returnTransition = Fade().setInterpolator(DecelerateInterpolator()) | |
} | |
requireFragmentManager().commit { | |
replace(R.id.fragment_container, f) | |
.addToBackStack(DetailFragment::javaClass.name) | |
// .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN) | |
} | |
} | |
} | |
} | |
return view | |
} | |
companion object { | |
@JvmStatic | |
fun newInstance() = ItemFragment() | |
} | |
private fun getListFragmentExitTransition(itemView: View): Transition { | |
val epicCenterRect = Rect() | |
//itemView is the full-width inbox item's view | |
itemView.getGlobalVisibleRect(epicCenterRect) | |
// Set Epic center to a imaginary horizontal full width line under the clicked item, so the explosion happens vertically away from it | |
epicCenterRect.top = epicCenterRect.bottom | |
val exitTransition = ExplodeFadeOut() | |
exitTransition.epicenterCallback = object : Transition.EpicenterCallback() { | |
override fun onGetEpicenter(transition: Transition): Rect { | |
return epicCenterRect | |
} | |
} | |
return exitTransition | |
} | |
} |
This file contains 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
fun AppCompatActivity.commit(fragment: Fragment) { | |
this.supportFragmentManager.commit { | |
setCustomAnimations(R.anim.popup_enter, R.anim.popup_exit) | |
replace(R.id.fragment_container, fragment) | |
} | |
} |
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<set xmlns:android="http://schemas.android.com/apk/res/android" | |
android:shareInterpolator="false"> | |
<alpha android:fromAlpha="0.8" android:toAlpha="1.0" | |
android:interpolator="@android:anim/decelerate_interpolator" | |
android:duration="@android:integer/config_shortAnimTime" | |
/> | |
<scale | |
android:pivotX="50%" | |
android:pivotY="100%" | |
android:fromXScale="0.8" android:fromYScale="0.8" | |
android:toXScale="1.0" android:toYScale="1.0" | |
android:interpolator="@android:anim/decelerate_interpolator" | |
android:duration="@android:integer/config_shortAnimTime" | |
/> | |
</set> |
This file contains 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
<?xml version="1.0" encoding="utf-8"?> | |
<set xmlns:android="http://schemas.android.com/apk/res/android" | |
android:shareInterpolator="false"> | |
<alpha android:fromAlpha="1.0" android:toAlpha="0.6" | |
android:interpolator="@android:anim/accelerate_interpolator" | |
android:duration="@android:integer/config_shortAnimTime" | |
/> | |
</set> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment