@@ -119,37 +119,45 @@ class CheeseArticleFragment : Fragment() {
119
119
v.findNavController().popBackStack()
120
120
}
121
121
122
+ val predictiveBackMargin = resources.getDimensionPixelSize(R .dimen.predictive_back_margin)
123
+ var initialTouchY = - 1f
122
124
requireActivity().onBackPressedDispatcher.addCallback(
123
125
viewLifecycleOwner,
124
126
object : OnBackPressedCallback (true ) {
125
127
override fun handleOnBackPressed () {
126
128
// This invokes the sharedElementReturnTransition, which is
127
129
// 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?
130
130
findNavController().popBackStack()
131
131
}
132
132
133
133
override fun handleOnBackProgressed (backEvent : BackEventCompat ) {
134
134
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)
147
154
background.scaleX = scale
148
155
background.scaleY = scale
149
156
}
150
157
151
158
override fun handleOnBackCancelled () {
152
159
TransitionManager .beginDelayedTransition(background, cancelTransition)
160
+ initialTouchY = - 1f
153
161
background.run {
154
162
translationX = 0f
155
163
scaleX = 1f
0 commit comments