@@ -119,37 +119,45 @@ class CheeseArticleFragment : Fragment() {
119119 v.findNavController().popBackStack()
120120 }
121121
122+ val predictiveBackMargin = resources.getDimensionPixelSize(R .dimen.predictive_back_margin)
123+ var initialTouchY = - 1f
122124 requireActivity().onBackPressedDispatcher.addCallback(
123125 viewLifecycleOwner,
124126 object : OnBackPressedCallback (true ) {
125127 override fun handleOnBackPressed () {
126128 // This invokes the sharedElementReturnTransition, which is
127129 // MaterialContainerTransform.
128- // TODO: It pops the scaleX/Y back to 1f. It would look nicer to retain the
129- // scale. How should we handle it?
130130 findNavController().popBackStack()
131131 }
132132
133133 override fun handleOnBackProgressed (backEvent : BackEventCompat ) {
134134 val progress = backEvent.progress
135- // Translate as far as 20% of finger movement.
136- val translation = progress * background.width * 0.2f
137- background.translationX =
138- if (backEvent.swipeEdge == BackEventCompat .EDGE_LEFT ) {
139- translation
140- } else {
141- - translation
142- }
143- // TODO: Consider handling backEvent.touchY to reflect the vertical movement.
144-
145- // Scale down from 100% to 50%.
146- val scale = (2f - progress) * 0.5f
135+ if (initialTouchY < 0f ) {
136+ initialTouchY = backEvent.touchY
137+ }
138+ val progressY = (backEvent.touchY - initialTouchY) / background.height
139+
140+ // See the motion spec about the calculations below.
141+ // https://developer.android.com/design/ui/mobile/guides/patterns/predictive-back#motion-specs
142+
143+ // Shift horizontally.
144+ val maxTranslationX = (background.width / 20 ) - predictiveBackMargin
145+ background.translationX = progress * maxTranslationX *
146+ (if (backEvent.swipeEdge == BackEventCompat .EDGE_LEFT ) 1 else - 1 )
147+
148+ // Shift vertically.
149+ val maxTranslationY = (background.height / 20 ) - predictiveBackMargin
150+ background.translationY = progressY * maxTranslationY
151+
152+ // Scale down from 100% to 90%.
153+ val scale = 1f - (0.1f * progress)
147154 background.scaleX = scale
148155 background.scaleY = scale
149156 }
150157
151158 override fun handleOnBackCancelled () {
152159 TransitionManager .beginDelayedTransition(background, cancelTransition)
160+ initialTouchY = - 1f
153161 background.run {
154162 translationX = 0f
155163 scaleX = 1f
0 commit comments