คุณสามารถสร้างภาพเคลื่อนไหวและการเปลี่ยนผ่านของพร็อพเพอร์ตี้ในแอปที่กำหนดเอง ภาพเคลื่อนไหวข้ามกิจกรรมที่กำหนดเอง และภาพเคลื่อนไหวข้าม Fragment ที่กำหนดเองด้วยท่าทางสัมผัสการย้อนกลับที่คาดการณ์ได้โดยใช้ Views หรือ Compose หากต้องการลองใช้วิธี Compose โปรดดูเพิ่มการรองรับภาพเคลื่อนไหวของการย้อนกลับที่คาดการณ์ได้
เพิ่มการเปลี่ยนที่กำหนดเองโดยใช้ Progress API
เมื่อใช้ AndroidX Activity 1.8.0-alpha01 ขึ้นไป คุณสามารถใช้ API ความคืบหน้าของการย้อนกลับที่คาดการณ์ได้เพื่อพัฒนาภาพเคลื่อนไหวที่กำหนดเองสำหรับท่าทางสัมผัสการย้อนกลับที่คาดการณ์ได้ในแอป API ความคืบหน้ามีประโยชน์ในการสร้างภาพเคลื่อนไหวของมุมมอง แต่มีข้อจำกัดเมื่อสร้างภาพเคลื่อนไหวของการเปลี่ยนระหว่างส่วนย่อย ใน OnBackPressedCallback เราได้เปิดตัวเมธอด handleOnBackProgressed, handleOnBackCancelled และ handleOnBackStarted เพื่อทำให้ออบเจ็กต์เคลื่อนไหวขณะที่ผู้ใช้ปัดกลับ ใช้วิธีการเหล่านี้หากต้องการปรับแต่งมากกว่าภาพเคลื่อนไหวเริ่มต้นที่ระบบมีให้ หรือภาพเคลื่อนไหวของคอมโพเนนต์ Material
เราคาดว่าแอปส่วนใหญ่จะใช้ AndroidX API ที่เข้ากันได้แบบย้อนหลัง แต่ก็มี API ของแพลตฟอร์มที่คล้ายกันภายในอินเทอร์เฟซ OnBackAnimationCallback ที่พร้อมให้ทดสอบใน Android 14 ขึ้นไป
ใช้ Progress API กับ AndroidX Transitions
Progress API สามารถใช้กับ AndroidX Transitions 1.5.0-alpha01 ขึ้นไปใน Android 14 ขึ้นไปเพื่อสร้างการเปลี่ยนผ่านสำหรับการย้อนกลับที่คาดการณ์ได้
- ใช้
TransitionManager#controlDelayedTransitionแทนbeginDelayedTransitionเพื่อเล่นการเปลี่ยนภาพเมื่อผู้ใช้ปัดกลับ - สร้างการเปลี่ยนผ่านภายใน
handleOnBackStarted - เล่นการเปลี่ยนภาพด้วยเหตุการณ์ย้อนกลับภายใน
handleOnBackProgressedโดยเชื่อมโยงcurrentFractionกับBackEvent.progressซึ่งจะแสดงระยะทางที่ผู้ใช้ปัดย้อนกลับ - สิ้นสุดการเปลี่ยนผ่านหลังจากที่ผู้ใช้ทำท่าทางสัมผัสเพื่อย้อนกลับใน
handleOnBackPressed - สุดท้าย ให้รีเซ็ตสถานะของการเปลี่ยนผ่านภายใน
handleOnBackCancelled
วิดีโอ, โค้ด Kotlin และ XML ต่อไปนี้แสดงการเปลี่ยนที่กำหนดเองระหว่างกล่อง 2 กล่องที่ใช้ OnBackPressedCallback
class MyFragment : Fragment() { val transitionSet = TransitionSet().apply { addTransition(Fade(Fade.MODE_OUT)) addTransition(ChangeBounds()) addTransition(Fade(Fade.MODE_IN)) } ... override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) val callback = object : OnBackPressedCallback(enabled = false) { var controller: TransitionSeekController? = null @RequiresApi(34) override fun handleOnBackStarted(backEvent: BackEvent) { // Create the transition controller = TransitionManager.controlDelayedTransition( binding.card, transitionSet ) changeTextVisibility(ShowText.SHORT) } @RequiresApi(34) override fun handleOnBackProgressed(backEvent: BackEvent) { // Play the transition as the user swipes back if (controller?.isReady == true) { controller?.currentFraction = backEvent.progress } } override fun handleOnBackPressed() { // Finish playing the transition when the user commits back controller?.animateToEnd() this.isEnabled = false } @RequiresApi(34) override fun handleOnBackCancelled() { // If the user cancels the back gesture, reset the state transition(ShowText.LONG) } } binding.shortText.setOnClickListener { transition(ShowText.LONG) callback.isEnabled = true } this.requireActivity().onBackPressedDispatcher.addCallback(callback) } private fun transition(showText: ShowText) { TransitionManager.beginDelayedTransition( binding.card, transitionSet ) changeTextVisibility(showText) } enum class ShowText { SHORT, LONG } private fun changeTextVisibility(showText: ShowText) { when (showText) { ShowText.SHORT -> { binding.shortText.isVisible = true binding.longText.isVisible = false } ShowText.LONG -> { binding.shortText.isVisible = false binding.longText.isVisible = true } } } }
<?xml version="1.0" encoding="utf-8"?>
...
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
...>
<TextView
android:id="@+id/short_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
... />
<TextView
android:id="@+id/long_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
.../>
</androidx.constraintlayout.widget.ConstraintLayout>
โปรดคำนึงถึงสิ่งต่อไปนี้เมื่อทำงานกับการเปลี่ยนผ่านของการย้อนกลับที่คาดการณ์ได้
- ใช้
isSeekingSupportedเพื่อตรวจสอบว่าการเปลี่ยนภาพรองรับการย้อนกลับที่คาดการณ์ได้หรือไม่ - ลบล้าง
isSeekingSupportedเพื่อแสดงผลเป็น "จริง" สำหรับการเปลี่ยนที่กำหนดเอง - สร้างตัวควบคุม 1 ตัวต่อภาพเคลื่อนไหว
- การเปลี่ยนผ่านด้วยการย้อนกลับที่คาดการณ์ได้รองรับการเปลี่ยนผ่านด้วย AndroidX แต่ไม่รองรับการเปลี่ยนผ่านด้วยเฟรมเวิร์ก ย้ายข้อมูลออกจากการเปลี่ยนเฟรมเวิร์กและใช้
Animatorและการเปลี่ยน AndroidX แทน - การเปลี่ยนผ่านด้วยการย้อนกลับที่คาดการณ์ได้รองรับในอุปกรณ์ที่ใช้ Android 14 ขึ้นไป และไม่เข้ากันได้แบบย้อนหลัง
- นอกจากนี้ยังรองรับการเปลี่ยนฉากที่สร้างด้วยฉาก XML ด้วย ใน
handleOnBackStartedให้ตั้งค่าTransitionSeekControllerเป็นผลลัพธ์ของTransitionManager.createSeekControllerแทนผลลัพธ์ของcontrolDelayedTransition
แหล่งข้อมูลเพิ่มเติม
- ตัวอย่างโค้ดการย้อนกลับที่คาดการณ์ได้
- ข้อมูลเบื้องต้นเกี่ยวกับวิดีโอการสำรองข้อมูลระบบ
- การสร้างสรรค์สิ่งใหม่ๆ สำหรับอนาคตของวิดีโอ Android