• 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>

  • 相关阅读:
    leetcode20. 有效的括号 [简单题]
    URL中的参数提取
    paddlepaddle 38 使用aistudio部署自己训练的动态图模型
    《2022爱分析·银行数字化厂商全景报告》发布,菊风连续入选「视频银行」优质代表厂商
    简单函数模拟优化器
    SSRF 服务端请求伪造, 简介,SSRF实验, 漏洞探测, 绕过技巧, SSRF防御
    22-08-01 西安 尚医通(01)跨域配置、Swagger2、R类、统一异常处理和自定义异常、Logback日志
    ajax请求出错自动重发
    CentOS7 —— yum安装mysql
    CListCtrl控件为只显示一列,持滚动显示其他,不用SetScrollFlags
  • 原文地址:https://blog.csdn.net/mp624183768/article/details/126356470