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

Commit 9748cd7

Browse files
committed
Motion: Use MaterialContainerTransform
Replace custom Transition implementations in the "Navigation > Fade through" demo with MaterialContainerTransform in MDC.
1 parent 8b38e9f commit 9748cd7

File tree

4 files changed

+3
-48
lines changed

4 files changed

+3
-48
lines changed

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

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ import android.widget.FrameLayout
2424
import android.widget.ImageView
2525
import android.widget.LinearLayout
2626
import android.widget.TextView
27-
import androidx.annotation.IdRes
2827
import androidx.appcompat.widget.Toolbar
2928
import androidx.coordinatorlayout.widget.CoordinatorLayout
3029
import androidx.core.view.ViewCompat
@@ -36,25 +35,14 @@ import androidx.fragment.app.Fragment
3635
import androidx.fragment.app.viewModels
3736
import androidx.navigation.findNavController
3837
import androidx.navigation.fragment.navArgs
39-
import androidx.transition.ChangeBounds
40-
import androidx.transition.ChangeTransform
41-
import androidx.transition.Transition
4238
import com.example.android.motion.R
43-
import com.example.android.motion.demo.FAST_OUT_SLOW_IN
44-
import com.example.android.motion.demo.LARGE_COLLAPSE_DURATION
45-
import com.example.android.motion.demo.LARGE_EXPAND_DURATION
46-
import com.example.android.motion.demo.plusAssign
47-
import com.example.android.motion.demo.sharedelement.MirrorView
48-
import com.example.android.motion.demo.sharedelement.SharedFade
49-
import com.example.android.motion.demo.transitionTogether
5039
import com.google.android.material.appbar.CollapsingToolbarLayout
40+
import com.google.android.material.transition.MaterialContainerTransform
5141

5242
class CheeseArticleFragment : Fragment() {
5343

5444
companion object {
5545
const val TRANSITION_NAME_BACKGROUND = "background"
56-
const val TRANSITION_NAME_CARD_CONTENT = "card_content"
57-
const val TRANSITION_NAME_ARTICLE_CONTENT = "article_content"
5846
}
5947

6048
private val args: CheeseArticleFragmentArgs by navArgs()
@@ -65,26 +53,12 @@ class CheeseArticleFragment : Fragment() {
6553
super.onCreate(savedInstanceState)
6654

6755
// These are the shared element transitions.
68-
sharedElementEnterTransition =
69-
createSharedElementTransition(LARGE_EXPAND_DURATION, R.id.article_mirror)
70-
sharedElementReturnTransition =
71-
createSharedElementTransition(LARGE_COLLAPSE_DURATION, R.id.card_mirror)
56+
sharedElementEnterTransition = MaterialContainerTransform(requireContext(), true)
57+
sharedElementReturnTransition = MaterialContainerTransform(requireContext(), false)
7258

7359
viewModel.cheeseId = args.cheeseId
7460
}
7561

76-
private fun createSharedElementTransition(duration: Long, @IdRes noTransform: Int): Transition {
77-
return transitionTogether {
78-
this.duration = duration
79-
interpolator = FAST_OUT_SLOW_IN
80-
this += SharedFade()
81-
this += ChangeBounds()
82-
this += ChangeTransform()
83-
// The content is already transformed along with the parent. Exclude it.
84-
.excludeTarget(noTransform, true)
85-
}
86-
}
87-
8862
override fun onCreateView(
8963
inflater: LayoutInflater,
9064
container: ViewGroup?,
@@ -102,11 +76,8 @@ class CheeseArticleFragment : Fragment() {
10276

10377
val background: FrameLayout = view.findViewById(R.id.background)
10478
val coordinator: CoordinatorLayout = view.findViewById(R.id.coordinator)
105-
val mirror: MirrorView = view.findViewById(R.id.card_mirror)
10679

10780
ViewCompat.setTransitionName(background, TRANSITION_NAME_BACKGROUND)
108-
ViewCompat.setTransitionName(coordinator, TRANSITION_NAME_ARTICLE_CONTENT)
109-
ViewCompat.setTransitionName(mirror, TRANSITION_NAME_CARD_CONTENT)
11081
ViewGroupCompat.setTransitionGroup(coordinator, true)
11182

11283
// Adjust the edge-to-edge display.

Motion/app/src/main/java/com/example/android/motion/demo/navfadethrough/CheeseCardFragment.kt

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ import com.example.android.motion.demo.FAST_OUT_LINEAR_IN
3939
import com.example.android.motion.demo.LARGE_COLLAPSE_DURATION
4040
import com.example.android.motion.demo.LARGE_EXPAND_DURATION
4141
import com.example.android.motion.demo.LINEAR_OUT_SLOW_IN
42-
import com.example.android.motion.demo.sharedelement.MirrorView
4342
import com.google.android.material.appbar.AppBarLayout
4443
import com.google.android.material.card.MaterialCardView
4544

@@ -75,7 +74,6 @@ class CheeseCardFragment : Fragment() {
7574
val cardContent: ConstraintLayout = view.findViewById(R.id.card_content)
7675
val image: ImageView = view.findViewById(R.id.image)
7776
val name: TextView = view.findViewById(R.id.name)
78-
val mirror: MirrorView = view.findViewById(R.id.article_mirror)
7977

8078
ViewCompat.setOnApplyWindowInsetsListener(view.parent as View) { _, insets ->
8179
toolbar.updateLayoutParams<AppBarLayout.LayoutParams> {
@@ -90,8 +88,6 @@ class CheeseCardFragment : Fragment() {
9088
}
9189

9290
ViewCompat.setTransitionName(card, "card")
93-
ViewCompat.setTransitionName(cardContent, "card_content")
94-
ViewCompat.setTransitionName(mirror, "article")
9591
ViewGroupCompat.setTransitionGroup(cardContent, true)
9692

9793
viewModel.cheese.observe(viewLifecycleOwner) { cheese ->
@@ -105,8 +101,6 @@ class CheeseCardFragment : Fragment() {
105101
CheeseCardFragmentDirections.actionArticle(cheese.id),
106102
FragmentNavigatorExtras(
107103
card to CheeseArticleFragment.TRANSITION_NAME_BACKGROUND,
108-
cardContent to CheeseArticleFragment.TRANSITION_NAME_CARD_CONTENT,
109-
mirror to CheeseArticleFragment.TRANSITION_NAME_ARTICLE_CONTENT
110104
)
111105
)
112106
}

Motion/app/src/main/res/layout/cheese_article_fragment.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@
2525
android:elevation="1dp"
2626
tools:targetApi="21">
2727

28-
<com.example.android.motion.demo.sharedelement.MirrorView
29-
android:id="@+id/card_mirror"
30-
android:layout_width="match_parent"
31-
android:layout_height="match_parent" />
32-
3328
<androidx.coordinatorlayout.widget.CoordinatorLayout
3429
android:id="@+id/coordinator"
3530
android:layout_width="match_parent"

Motion/app/src/main/res/layout/cheese_card_fragment.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@
4949
android:layout_gravity="center"
5050
android:layout_margin="@dimen/spacing_small">
5151

52-
<com.example.android.motion.demo.sharedelement.MirrorView
53-
android:id="@+id/article_mirror"
54-
android:layout_width="match_parent"
55-
android:layout_height="match_parent" />
56-
5752
<androidx.constraintlayout.widget.ConstraintLayout
5853
android:id="@+id/card_content"
5954
android:layout_width="match_parent"

0 commit comments

Comments
 (0)