Android 小知识 共享元素

983 阅读1分钟

这是Android 5 推出的东西,本篇只作为笔记来参考!

先来看效果图:

单独元素共享多个元素共享
49B66D8BA093BD06C55F9A15C7168128.gifB7DA1698E348583CC4A08DA225315F95.gif

单个元素共享

使用:

MainActivity:

xml布局:

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ImageView
        android:onClick="imageClick"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

kotlin代码:

image.png

Main2Activity:

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:ignore="MissingDefaultResource">

    <ImageView
        android:transitionName="shareElement"
        app:layout_constraintTop_toTopOf="parent"
        android:onClick="image2Click"
        android:src="@mipmap/ic_launcher_round"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_width="200dp"
        android:layout_height="200dp"/>

</androidx.constraintlayout.widget.ConstraintLayout>

辅助图:

image.png

这两个参数必须保证一致,这个参数是标示A页面到B页面的

B页面返回A页面:

B页面返回A页面时候,不能直接调用finsh,我是调用的物理返回键.

image.png

多个元素共享

思路一样,直接上代码了:

image.png

这里需要注意的是:

传参数的时候,不能使用kt中的Pair,得使用java中的Pair

image.png

Main2Activity页面:

还是什么操作都不需要做,只需要保证传递过来的参数一致就行:

image.png

来看看效果图:

B7DA1698E348583CC4A08DA225315F95.gif