Skip to content
This repository was archived by the owner on Dec 9, 2024. It is now read-only.

Commit f2d19a5

Browse files
committed
Motion: Update the calculations for predictive back
Change-Id: I8ccbc5267ecd3aae1c1179ff5a7036030896ba8a
1 parent 6f362dc commit f2d19a5

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

Motion/app/src/main/java/com/example/android/motion/demo/containertransform/CheeseArticleFragment.kt

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Motion/app/src/main/res/values/dimens.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
<dimen name="pick_up_elevation">8dp</dimen>
2222
<dimen name="fab_elevation">6dp</dimen>
2323
<dimen name="icon_size">24dp</dimen>
24+
<dimen name="predictive_back_margin">8dp</dimen>
2425
</resources>

0 commit comments

Comments
 (0)