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

Commit b9ab54c

Browse files
authored
Merge pull request #102 from android/yaraki/motion-predictive
Motion: Support predictive back in Container transform
2 parents 1f1c48b + 77e4183 commit b9ab54c

File tree

7 files changed

+83
-24
lines changed

7 files changed

+83
-24
lines changed

Motion/app/build.gradle

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ apply plugin: 'kotlin-android'
1919
apply plugin: "androidx.navigation.safeargs.kotlin"
2020

2121
android {
22-
compileSdk 31
22+
compileSdk 34
2323

2424
defaultConfig {
25-
applicationId 'com.example.android.motion'
26-
minSdk 14
27-
targetSdk 31
25+
namespace 'com.example.android.motion'
26+
minSdk 19
27+
targetSdk 34
2828
versionCode 1
2929
versionName '1.0'
3030
testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
@@ -51,33 +51,33 @@ android {
5151
dependencies {
5252
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
5353

54-
implementation 'androidx.core:core-ktx:1.7.0'
55-
implementation 'androidx.fragment:fragment-ktx:1.4.1'
56-
implementation 'androidx.appcompat:appcompat:1.4.1'
54+
implementation 'androidx.core:core-ktx:1.12.0'
55+
implementation 'androidx.fragment:fragment-ktx:1.6.2'
56+
implementation 'androidx.appcompat:appcompat:1.6.1'
5757
implementation 'androidx.transition:transition:1.4.1'
5858
implementation 'androidx.dynamicanimation:dynamicanimation:1.1.0-alpha03'
59-
implementation 'androidx.recyclerview:recyclerview:1.2.1'
59+
implementation 'androidx.recyclerview:recyclerview:1.3.2'
6060

61-
implementation 'androidx.constraintlayout:constraintlayout:2.1.3'
61+
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
6262

63-
def lifecycle_version = '2.4.1'
63+
def lifecycle_version = '2.7.0'
6464
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version"
6565
implementation "androidx.lifecycle:lifecycle-livedata-ktx:$lifecycle_version"
6666

67-
implementation 'androidx.paging:paging-runtime-ktx:3.1.1'
67+
implementation 'androidx.paging:paging-runtime-ktx:3.2.1'
6868

69-
implementation 'com.google.android.material:material:1.6.0'
69+
implementation 'com.google.android.material:material:1.11.0'
7070

7171
implementation "androidx.navigation:navigation-fragment-ktx:$navigation_version"
7272
implementation "androidx.navigation:navigation-ui-ktx:$navigation_version"
7373

74-
implementation 'com.github.bumptech.glide:glide:4.10.0'
74+
implementation 'com.github.bumptech.glide:glide:4.15.1'
7575

7676
testImplementation 'junit:junit:4.13.2'
7777
testImplementation 'com.google.truth:truth:1.1.3'
7878

79-
testImplementation 'androidx.test:core:1.4.0'
80-
androidTestImplementation 'androidx.test.ext:truth:1.4.0'
81-
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
82-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
79+
testImplementation 'androidx.test:core:1.5.0'
80+
androidTestImplementation 'androidx.test.ext:truth:1.5.0'
81+
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
82+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
8383
}

Motion/app/src/main/AndroidManifest.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@
2727

2828
<application
2929
android:allowBackup="false"
30+
android:enableOnBackInvokedCallback="true"
3031
android:icon="@mipmap/ic_launcher"
3132
android:label="@string/app_name"
3233
android:roundIcon="@mipmap/ic_launcher_round"
3334
android:supportsRtl="true"
3435
android:theme="@style/Theme.Motion"
35-
tools:ignore="GoogleAppIndexingWarning">
36+
tools:ignore="GoogleAppIndexingWarning"
37+
tools:targetApi="34">
3638

3739
<activity
3840
android:name=".MainActivity"

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

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,21 @@ import android.widget.FrameLayout
2424
import android.widget.ImageView
2525
import android.widget.LinearLayout
2626
import android.widget.TextView
27+
import androidx.activity.BackEventCompat
28+
import androidx.activity.OnBackPressedCallback
2729
import androidx.appcompat.widget.Toolbar
2830
import androidx.coordinatorlayout.widget.CoordinatorLayout
2931
import androidx.core.view.ViewCompat
3032
import androidx.core.view.ViewGroupCompat
3133
import androidx.core.view.WindowInsetsCompat
34+
import androidx.core.view.animation.PathInterpolatorCompat
3235
import androidx.core.view.updateLayoutParams
3336
import androidx.core.view.updatePadding
3437
import androidx.core.widget.NestedScrollView
3538
import androidx.fragment.app.Fragment
3639
import androidx.fragment.app.viewModels
3740
import androidx.navigation.findNavController
41+
import androidx.navigation.fragment.findNavController
3842
import androidx.navigation.fragment.navArgs
3943
import com.example.android.motion.R
4044
import com.google.android.material.appbar.CollapsingToolbarLayout
@@ -44,6 +48,8 @@ class CheeseArticleFragment : Fragment() {
4448

4549
companion object {
4650
const val TRANSITION_NAME_BACKGROUND = "background"
51+
52+
private val GestureInterpolator = PathInterpolatorCompat.create(0f, 0f, 0f, 1f)
4753
}
4854

4955
private val args: CheeseArticleFragmentArgs by navArgs()
@@ -111,5 +117,55 @@ class CheeseArticleFragment : Fragment() {
111117
toolbar.setNavigationOnClickListener { v ->
112118
v.findNavController().popBackStack()
113119
}
120+
121+
val predictiveBackMargin = resources.getDimensionPixelSize(R.dimen.predictive_back_margin)
122+
var initialTouchY = -1f
123+
requireActivity().onBackPressedDispatcher.addCallback(
124+
viewLifecycleOwner,
125+
object : OnBackPressedCallback(true) {
126+
override fun handleOnBackPressed() {
127+
// This invokes the sharedElementReturnTransition, which is
128+
// MaterialContainerTransform.
129+
findNavController().popBackStack()
130+
}
131+
132+
override fun handleOnBackProgressed(backEvent: BackEventCompat) {
133+
val progress = GestureInterpolator.getInterpolation(backEvent.progress)
134+
if (initialTouchY < 0f) {
135+
initialTouchY = backEvent.touchY
136+
}
137+
val progressY = GestureInterpolator.getInterpolation(
138+
(backEvent.touchY - initialTouchY) / background.height
139+
)
140+
141+
// See the motion spec about the calculations below.
142+
// https://developer.android.com/design/ui/mobile/guides/patterns/predictive-back#motion-specs
143+
144+
// Shift horizontally.
145+
val maxTranslationX = (background.width / 20) - predictiveBackMargin
146+
background.translationX = progress * maxTranslationX *
147+
(if (backEvent.swipeEdge == BackEventCompat.EDGE_LEFT) 1 else -1)
148+
149+
// Shift vertically.
150+
val maxTranslationY = (background.height / 20) - predictiveBackMargin
151+
background.translationY = progressY * maxTranslationY
152+
153+
// Scale down from 100% to 90%.
154+
val scale = 1f - (0.1f * progress)
155+
background.scaleX = scale
156+
background.scaleY = scale
157+
}
158+
159+
override fun handleOnBackCancelled() {
160+
initialTouchY = -1f
161+
background.run {
162+
translationX = 0f
163+
translationY = 0f
164+
scaleX = 1f
165+
scaleY = 1f
166+
}
167+
}
168+
}
169+
)
114170
}
115171
}

Motion/app/src/main/java/com/example/android/motion/demo/sharedelement/MirrorView.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class MirrorView @JvmOverloads constructor(
4747
setWillNotDraw(value == null)
4848
}
4949

50-
override fun onDraw(canvas: Canvas?) {
50+
override fun onDraw(canvas: Canvas) {
5151
_substance?.draw(canvas)
5252
}
5353
}

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>

Motion/build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
*/
1616

1717
buildscript {
18-
ext.kotlin_version = '1.6.21'
19-
ext.navigation_version = '2.4.2'
18+
ext.kotlin_version = '1.9.22'
19+
ext.navigation_version = '2.7.7'
2020
repositories {
2121
google()
2222
mavenCentral()
2323
}
2424
dependencies {
25-
classpath 'com.android.tools.build:gradle:7.3.0'
25+
classpath 'com.android.tools.build:gradle:8.2.2'
2626
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
2727
classpath "androidx.navigation:navigation-safe-args-gradle-plugin:$navigation_version"
2828
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#Wed Oct 28 16:25:10 JST 2020
1+
#Fri Feb 16 14:02:28 JST 2024
22
distributionBase=GRADLE_USER_HOME
33
distributionPath=wrapper/dists
4+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
45
zipStoreBase=GRADLE_USER_HOME
56
zipStorePath=wrapper/dists
6-
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4-all.zip

0 commit comments

Comments
 (0)