• Android navigation Fragment 共享元素动画


    依赖 项目级别build.gradle

    1. buildscript {
    2. dependencies {
    3. classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.4.2")
    4. }
    5. }

     app级别

    1. implementation 'androidx.navigation:navigation-fragment-ktx:2.5.1'
    2. implementation 'androidx.navigation:navigation-ui:2.5.1'
    3. }

     顶部添加插件saferargs

    1. plugins {
    2. id 'com.android.application'
    3. id 'org.jetbrains.kotlin.android'
    4. id 'androidx.navigation.safeargs.kotlin'
    5. }

    mainActivity里面没有代码 布局如此

    1. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:app="http://schemas.android.com/apk/res-auto"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent"
    6. tools:context=".MainActivity">
    7. <FrameLayout
    8. android:layout_width="match_parent"
    9. android:layout_height="match_parent"
    10. app:layout_constraintBottom_toBottomOf="parent"
    11. app:layout_constraintEnd_toEndOf="parent"
    12. app:layout_constraintHorizontal_bias="0.5"
    13. app:layout_constraintStart_toStartOf="parent"
    14. app:layout_constraintTop_toTopOf="parent">
    15. <androidx.fragment.app.FragmentContainerView
    16. android:id="@+id/navHostFragment"
    17. android:name="androidx.navigation.fragment.NavHostFragment"
    18. android:layout_width="match_parent"
    19. android:layout_height="match_parent"
    20. app:defaultNavHost="true"
    21. app:navGraph="@navigation/nav_graph" />
    22. FrameLayout>
    23. androidx.constraintlayout.widget.ConstraintLayout>

    mav_graph.xml

    1. <navigation xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:app="http://schemas.android.com/apk/res-auto"
    3. android:id="@+id/nav_graph"
    4. app:startDestination="@id/mainFragment">
    5. <fragment
    6. android:id="@+id/mainFragment"
    7. android:name="com.example.share_element_anim.MainFragment"
    8. android:label="MainFragment" >
    9. <action
    10. android:id="@+id/action_mainFragment_to_imageFragment"
    11. app:destination="@id/imageFragment" />
    12. fragment>
    13. <fragment
    14. android:id="@+id/imageFragment"
    15. android:name="com.example.share_element_anim.ImageFragment"
    16. android:label="ImageFragment" />
    17. navigation>
    MainFragment
    1. package com.example.share_element_anim
    2. import android.os.Bundle
    3. import android.view.LayoutInflater
    4. import android.view.View
    5. import android.view.ViewGroup
    6. import androidx.fragment.app.Fragment
    7. import androidx.navigation.fragment.FragmentNavigatorExtras
    8. import androidx.navigation.fragment.findNavController
    9. import com.example.share_element_anim.databinding.FragmentMainBinding
    10. class MainFragment : Fragment(R.layout.fragment_main) {
    11. private lateinit var binding: FragmentMainBinding
    12. override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    13. super.onViewCreated(view, savedInstanceState)
    14. binding = FragmentMainBinding.bind(view)
    15. binding.ivImage.setOnClickListener {
    16. val extras = FragmentNavigatorExtras(binding.ivImage to "image_big")
    17. findNavController().navigate(
    18. R.id.action_mainFragment_to_imageFragment,
    19. null,
    20. null,
    21. extras
    22. )
    23. }
    24. }
    25. override fun onDestroy() {
    26. super.onDestroy()
    27. }
    28. }

    布局

    1. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:app="http://schemas.android.com/apk/res-auto"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent">
    6. <ImageView
    7. android:id="@+id/ivImage"
    8. android:layout_width="120dp"
    9. android:layout_height="120dp"
    10. android:transitionName="image_small"
    11. app:layout_constraintBottom_toBottomOf="parent"
    12. app:layout_constraintEnd_toEndOf="parent"
    13. app:layout_constraintHorizontal_bias="0.5"
    14. app:layout_constraintStart_toStartOf="parent"
    15. app:layout_constraintTop_toTopOf="parent"
    16. app:srcCompat="@drawable/ic_launcher_background" />
    17. androidx.constraintlayout.widget.ConstraintLayout>
    ImageFragment
    1. package com.example.share_element_anim
    2. import android.os.Bundle
    3. import android.transition.TransitionInflater
    4. import androidx.fragment.app.Fragment
    5. class ImageFragment : Fragment(R.layout.fragment_image) {
    6. override fun onCreate(savedInstanceState: Bundle?) {
    7. super.onCreate(savedInstanceState)
    8. val animation = TransitionInflater.from(requireContext()).inflateTransition(
    9. android.R.transition.move
    10. )
    11. sharedElementEnterTransition = animation
    12. sharedElementReturnTransition = animation
    13. }
    14. }

    布局

    1. <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    2. xmlns:app="http://schemas.android.com/apk/res-auto"
    3. xmlns:tools="http://schemas.android.com/tools"
    4. android:layout_width="match_parent"
    5. android:layout_height="match_parent">
    6. <ImageView
    7. android:id="@+id/ivImage"
    8. android:layout_width="0dp"
    9. android:layout_height="500dp"
    10. android:scaleType="centerCrop"
    11. android:transitionName="image_big"
    12. app:layout_constraintBottom_toBottomOf="parent"
    13. app:layout_constraintEnd_toEndOf="parent"
    14. app:layout_constraintHorizontal_bias="0.5"
    15. app:layout_constraintStart_toStartOf="parent"
    16. app:layout_constraintTop_toTopOf="parent"
    17. app:srcCompat="@drawable/ic_launcher_background" />
    18. androidx.constraintlayout.widget.ConstraintLayout>

  • 相关阅读:
    Go:Gnome sort 侏儒排序(附完整源码)
    Mybiosource丨Mybiosource胃泌素细胞抗体ELISA试剂盒方案
    SpringBoot前后端分离开发拆分和部署
    【MindSpore易点通】数据处理之Numpy的介绍和使用
    基于matlab的MRC最大合并比误码率仿真,包括维特比译码,MRC,中继
    Oracle创建表空间及用户
    音视频技术开发周刊 | 257
    iOS获取当前网络连接状态WiFi、5G、4G、3G、2G
    基于Android+OpenCV+CNN+Keras的智能手语数字实时翻译——深度学习算法应用(含Python、ipynb工程源码)+数据集(三)
    服务器负载均衡算法
  • 原文地址:https://blog.csdn.net/mp624183768/article/details/126356470