Skip to content

Commit 3be03ee

Browse files
authored
Merge pull request #157 from trambui09/setCustomAnimations
Add setCustomAnimations into Platform Samples
2 parents ab694a1 + 31dde68 commit 3be03ee

14 files changed

+429
-3
lines changed

samples/user-interface/predictiveback/README.md

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ Shows different types of predictive back animations, including:
1010
+ Custom Progress API animation
1111
+ Custom AndroidX Transition
1212
+ Cross-fragment animation with MaterialSharedAxis
13+
+ setCustomAnimations
1314

1415
## Custom cross-activity
1516

@@ -236,5 +237,50 @@ override fun onCreateView(savedInstanceState: Bundle?) {
236237
returnTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ false)
237238
}
238239
```
240+
## setCustomAnimations
239241

242+
Use `setEnterTransition`, `setExitTransition`, `setReenterTransition`, `setReturnTransition`,
243+
`setSharedElementEnterTransition`, `setSharedElementReturnTransition` instead of `setCustomAnimations`
244+
where possible.
245+
246+
However, if you are using `setCustomAnimations`, here's a code sample showing
247+
predictive back working with animators and fragment manager.
248+
249+
```kotlin
250+
// PBSetCustomAnimationsActivity.kt
251+
252+
override fun onCreate(savedInstanceState: Bundle?) {
253+
super.onCreate(savedInstanceState)
254+
255+
"..."
256+
supportFragmentManager.commit {
257+
replace(R.id.fragment_container, PBSetCustomAnimationsFirstFragment())
258+
}
259+
260+
}
261+
262+
// PBSetCustomAnimationsFirstFragment.kt
263+
264+
override fun onCreateView(
265+
inflater: LayoutInflater, container: ViewGroup?,
266+
savedInstanceState: Bundle?,
267+
): View {
268+
_binding = FragmentSetCustomAnimationsBinding
269+
.inflate(inflater, container, false)
270+
271+
binding.box.setOnClickListener {
272+
parentFragmentManager.commit {
273+
setCustomAnimations(
274+
android.R.animator.fade_in, // enter
275+
android.R.animator.fade_out, // exit
276+
android.R.animator.fade_in, // popEnter
277+
android.R.animator.fade_out) // popExit
278+
replace(R.id.fragment_container,PBSetCustomAnimationsSecondFragment())
279+
setReorderingAllowed(true)
280+
addToBackStack(null)
281+
}
282+
}
283+
return binding.root
284+
}
285+
```
240286

samples/user-interface/predictiveback/src/main/AndroidManifest.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
33

44
<application>
5+
<activity
6+
android:name=".PBSetCustomAnimationsActivity"
7+
android:exported="false"
8+
android:label="@string/title_activity_set_custom_animations"
9+
android:theme="@style/Theme.PlatformSamples"/>
510
<activity
611
android:name=".PBCustomCrossActivityAnimation"
712
android:exported="false"

samples/user-interface/predictiveback/src/main/java/com/example/platform/ui/predictiveback/PBAnimation.kt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ enum class PBAnimation {
2525
SHARED_ELEMENT_CROSS_FRAGMENT,
2626
PROGRESS_API,
2727
TRANSITION,
28-
MATERIAL_SHARED_AXIS
28+
MATERIAL_SHARED_AXIS,
29+
SET_CUSTOM_ANIMATIONS
2930
}
3031
data class PBAnimationText(val title: String, val description: String)
3132

@@ -65,5 +66,10 @@ val animations = mapOf<PBAnimation, PBAnimationText>(
6566
PBAnimation.MATERIAL_SHARED_AXIS to PBAnimationText(
6667
"Material Shared Axis",
6768
"Click to see an animation created with Material Shared Axis."
69+
),
70+
PBAnimation.SET_CUSTOM_ANIMATIONS to PBAnimationText(
71+
"setCustomAnimations",
72+
"Click to see an animation created with setCustomAnimations."
6873
)
74+
6975
)

samples/user-interface/predictiveback/src/main/java/com/example/platform/ui/predictiveback/PBListFragment.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ class PBListFragment : Fragment() {
4141
exitTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ true)
4242
reenterTransition = MaterialSharedAxis(MaterialSharedAxis.Z, /* forward= */ false)
4343

44-
4544
return binding.root
4645
}
4746

@@ -71,6 +70,9 @@ class PBListFragment : Fragment() {
7170
binding.materialSharedAxisCard.setOnClickListener {
7271
findNavController().navigate(R.id.show_PBMaterialSharedAxisAnimations)
7372
}
73+
binding.setCustomAnimationsCard.setOnClickListener {
74+
findNavController().navigate(R.id.show_PBSetCustomAnimationsActivity)
75+
}
7476
}
7577

7678
override fun onDestroyView() {
@@ -97,5 +99,7 @@ class PBListFragment : Fragment() {
9799
binding.transitionsDescription.text = animations[PBAnimation.TRANSITION]?.description ?: ""
98100
binding.materialSharedAxisTitle.text = animations[PBAnimation.MATERIAL_SHARED_AXIS]?.title ?: ""
99101
binding.materialSharedAxisDescription.text = animations[PBAnimation.MATERIAL_SHARED_AXIS]?.description ?: ""
102+
binding.setCustomAnimationsTitle.text = animations[PBAnimation.SET_CUSTOM_ANIMATIONS]?.title ?: ""
103+
binding.setCustomAnimationsDescription.text = animations[PBAnimation.SET_CUSTOM_ANIMATIONS]?.description ?: ""
100104
}
101105
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.platform.ui.predictiveback
18+
19+
import androidx.appcompat.app.AppCompatActivity
20+
import android.os.Bundle
21+
import androidx.fragment.app.commit
22+
import com.example.platform.ui.predictiveback.databinding.ActivitySetCustomAnimationsBinding
23+
24+
class PBSetCustomAnimationsActivity : AppCompatActivity() {
25+
private lateinit var binding: ActivitySetCustomAnimationsBinding
26+
27+
override fun onCreate(savedInstanceState: Bundle?) {
28+
super.onCreate(savedInstanceState)
29+
30+
binding = ActivitySetCustomAnimationsBinding.inflate(layoutInflater)
31+
setContentView(binding.root)
32+
33+
supportFragmentManager.commit {
34+
replace(R.id.fragment_container, PBSetCustomAnimationsFirstFragment())
35+
}
36+
37+
}
38+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.platform.ui.predictiveback
18+
19+
import android.os.Bundle
20+
import android.view.LayoutInflater
21+
import android.view.View
22+
import android.view.ViewGroup
23+
import androidx.fragment.app.Fragment
24+
import androidx.fragment.app.commit
25+
import com.example.platform.ui.predictiveback.databinding.FragmentSetCustomAnimationsBinding
26+
27+
class PBSetCustomAnimationsFirstFragment : Fragment() {
28+
29+
private var _binding: FragmentSetCustomAnimationsBinding? = null
30+
private val binding get() = _binding!!
31+
32+
override fun onCreateView(
33+
inflater: LayoutInflater, container: ViewGroup?,
34+
savedInstanceState: Bundle?,
35+
): View {
36+
_binding = FragmentSetCustomAnimationsBinding
37+
.inflate(inflater, container, false)
38+
39+
binding.button.setOnClickListener {
40+
parentFragmentManager.commit {
41+
setCustomAnimations(
42+
android.R.animator.fade_in, // enter
43+
android.R.animator.fade_out, // exit
44+
android.R.animator.fade_in, // popEnter
45+
android.R.animator.fade_out) // popExit
46+
replace(R.id.fragment_container,PBSetCustomAnimationsSecondFragment())
47+
setReorderingAllowed(true)
48+
addToBackStack(null)
49+
}
50+
}
51+
return binding.root
52+
}
53+
54+
override fun onDestroyView() {
55+
super.onDestroyView()
56+
_binding = null
57+
}
58+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*
2+
* Copyright 2024 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.example.platform.ui.predictiveback
18+
19+
import android.os.Bundle
20+
import android.view.LayoutInflater
21+
import android.view.View
22+
import android.view.ViewGroup
23+
import androidx.fragment.app.Fragment
24+
import com.example.platform.ui.predictiveback.databinding.FragmentSetCustomAnimationsSecondBinding
25+
26+
class PBSetCustomAnimationsSecondFragment : Fragment() {
27+
28+
private var _binding: FragmentSetCustomAnimationsSecondBinding? = null
29+
private val binding get() = _binding!!
30+
31+
override fun onCreateView(
32+
inflater: LayoutInflater, container: ViewGroup?,
33+
savedInstanceState: Bundle?,
34+
): View {
35+
_binding = FragmentSetCustomAnimationsSecondBinding
36+
.inflate(inflater, container, false)
37+
38+
return binding.root
39+
}
40+
41+
override fun onDestroyView() {
42+
super.onDestroyView()
43+
_binding = null
44+
}
45+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="utf-8"?><!--
2+
~ Copyright 2024 The Android Open Source Project
3+
~
4+
~ Licensed under the Apache License, Version 2.0 (the "License");
5+
~ you may not use this file except in compliance with the License.
6+
~ You may obtain a copy of the License at
7+
~
8+
~ https://www.apache.org/licenses/LICENSE-2.0
9+
~
10+
~ Unless required by applicable law or agreed to in writing, software
11+
~ distributed under the License is distributed on an "AS IS" BASIS,
12+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
~ See the License for the specific language governing permissions and
14+
~ limitations under the License.
15+
-->
16+
<shape xmlns:android="http://schemas.android.com/apk/res/android">
17+
18+
<solid android:color="#4285F4" />
19+
<corners android:radius="20dp" />
20+
</shape>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
~ Copyright 2024 The Android Open Source Project
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
xmlns:app="http://schemas.android.com/apk/res-auto"
20+
xmlns:tools="http://schemas.android.com/tools"
21+
android:layout_width="match_parent"
22+
android:layout_height="match_parent"
23+
tools:context=".PBSetCustomAnimationsActivity">
24+
25+
<FrameLayout
26+
android:id="@+id/fragment_container"
27+
android:layout_width="0dp"
28+
android:layout_height="0dp"
29+
app:layout_constraintBottom_toBottomOf="parent"
30+
app:layout_constraintEnd_toEndOf="parent"
31+
app:layout_constraintStart_toStartOf="parent"
32+
app:layout_constraintTop_toTopOf="parent" />
33+
34+
</androidx.constraintlayout.widget.ConstraintLayout>

samples/user-interface/predictiveback/src/main/res/layout/fragment_animation_list.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,40 @@
345345

346346
</com.google.android.material.card.MaterialCardView>
347347

348+
<com.google.android.material.card.MaterialCardView
349+
android:id="@+id/set_custom_animations_card"
350+
android:layout_width="match_parent"
351+
android:layout_height="wrap_content"
352+
android:layout_margin="16dp"
353+
style="?attr/materialCardViewFilledStyle">
354+
355+
<LinearLayout
356+
android:layout_width="match_parent"
357+
android:layout_height="wrap_content"
358+
android:orientation="vertical">
359+
360+
<TextView
361+
android:id="@+id/set_custom_animations_title"
362+
android:layout_width="wrap_content"
363+
android:layout_height="wrap_content"
364+
android:padding="16dp"
365+
android:textAppearance="?attr/textAppearanceTitleMedium"
366+
android:textSize="16sp"/>
367+
368+
<TextView
369+
android:id="@+id/set_custom_animations_description"
370+
android:layout_width="wrap_content"
371+
android:layout_height="wrap_content"
372+
android:paddingLeft="16dp"
373+
android:paddingRight="16dp"
374+
android:paddingBottom="16dp"
375+
android:textAppearance="?attr/textAppearanceBodyMedium"
376+
android:textColor="?android:attr/textColorSecondary"
377+
android:textSize="12sp" />
378+
379+
</LinearLayout>
380+
381+
</com.google.android.material.card.MaterialCardView>
348382

349383
</LinearLayout>
350384

0 commit comments

Comments
 (0)