一、单个元素界面跳转

iShot2021-01-11 22.06.10.gif
具体实现:

    1. 第一个 Activity ``` class TransitionDemoActivity : BaseActivity(R.layout.activity_transition_demo) {

      override fun initData() {

      }

      override fun initEvent() {

      iv_transition_demo_pic.setOnClickListener {

      1. goToDetail(it)

      }

  1. }
  2. private fun goToDetail(it: View?) {
  3. val paris = TransitionHelper.createSafeTransitionParticipants(this,true,
  4. Pair(it,ResUtils.getString(R.string.transition_demo_pic)),
  5. Pair(iv_transition_demo_header,ResUtils.getString(R.string.transition_demo_header)),
  6. Pair(tv_transition_demo_name,ResUtils.getString(R.string.transition_demo_nickname))
  7. )
  8. val intent = Intent(this,TransitionDemoDetailActivity::class.java)
  9. transitionTo(intent,*paris)
  10. }
  11. override fun initInterface() {
  12. }

}

  1. 第一个界面布局:

<?xml version=”1.0” encoding=”utf-8”?>

  1. <TextView
  2. android:layout_width="match_parent"
  3. android:layout_height="wrap_content"
  4. android:text="测试单个元素跳转"
  5. android:layout_marginVertical="@dimen/m10"/>

  1. baseactivity 中用到的 transitionTo()
  1. open fun transitionTo(i: Intent?) {
  2. val pairs: Array<Pair<View, String>> = TransitionHelper.createSafeTransitionParticipants(this,true)
  3. val transitionActivityOptions: ActivityOptionsCompat =
  4. ActivityOptionsCompat.makeSceneTransitionAnimation(this, *pairs)
  5. startActivity(i, transitionActivityOptions.toBundle())
  6. }
  7. open fun transitionTo(i: Intent?,vararg pairs: Pair<View, String>) {
  8. //val pairs: Array<Pair<View, String>> = TransitionHelper.createSafeTransitionParticipants(this,true)
  9. val transitionActivityOptions: ActivityOptionsCompat =
  10. ActivityOptionsCompat.makeSceneTransitionAnimation(this, *pairs)
  11. startActivity(i, transitionActivityOptions.toBundle())
  12. }
  1. - 2. 详情界面
  2. activity:

class TransitionDemoDetailActivity: BaseActivity(R.layout.activity_transition_demo_detail) { override fun initData() {

  1. }
  2. override fun initIsToolbar(): Boolean {
  3. return false
  4. }
  5. override fun initEvent() {
  6. iv_transition_demo_detail_back.setOnClickListener {
  7. onBackPressed()
  8. }
  9. }
  10. override fun initInterface() {
  11. //可以设置动画时间
  12. window.enterTransition.duration = 500
  13. }

}

  1. xml:

<?xml version=”1.0” encoding=”utf-8”?>

  1. <ImageView
  2. android:id="@+id/iv_transition_demo_detial_pic"
  3. android:layout_width="match_parent"
  4. android:layout_height="@dimen/m300"
  5. android:src="@mipmap/wuhuang"
  6. android:scaleType="centerCrop"
  7. android:transitionName="@string/transition_demo_pic"/>
  8. <TextView
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:text="测试过渡元素,详情界面"
  12. android:layout_marginTop="@dimen/m15"/>

  1. - 3. 用到的关键点 transitionName
  1. <string name="transition_demo_pic">transition_demo_pic</string>
  2. <string name="transition_demo_header">transition_demo_header</string>
  3. <string name="transition_demo_nickname">transition_demo_nickname</string>
  1. <a name="BIpy2"></a>
  2. ## 二、列表跳转
  3. ![iShot2021-01-11 22.13.18.gif](https://cdn.nlark.com/yuque/0/2021/gif/1624725/1610374551713-0eaedd9d-229b-4627-ae21-77d0a9083484.gif#align=left&display=inline&height=1094&margin=%5Bobject%20Object%5D&name=iShot2021-01-11%2022.13.18.gif&originHeight=1094&originWidth=634&size=4734453&status=done&style=none&width=634)
  4. - 1. activity

class TransitionDemoListActivity: BaseActivity(R.layout.activity_transition_demolist) {

  1. val mAdapter by lazy { TransitionDemoListAdapter() }
  2. override fun initData() {
  3. val list = arrayListOf<String>("1","1","1","1","1","1","1","1","1","1","1","1","1","1","1")
  4. mAdapter.setList(list)
  5. }
  6. override fun initEvent() {
  7. }
  8. override fun initInterface() {
  9. rv_transition_demolist_list?.run {
  10. layoutManager = GridLayoutManager(this@TransitionDemoListActivity,2)
  11. adapter = mAdapter
  12. }
  13. }

}

class TransitionDemoListAdapter : BaseQuickAdapter(R.layout.item_transition_demolist){ override fun convert(holder: BaseViewHolder, item: String) {

  1. val ivPic = holder.getView<View>(R.id.iv_item_transition_demolist_pic)
  2. val ivHeader = holder.getView<View>(R.id.iv_item_transition_demolist_header)
  3. val tvName = holder.getView<View>(R.id.tv_item_transition_demolist_name)
  4. holder.itemView.setOnClickListener {
  5. goToDetail(ivPic,ivHeader,tvName)
  6. }
  7. }
  8. private fun goToDetail(ivPic: View, ivHeader: View, tvName: View) {
  9. val intent = Intent(context,TransitionDemoDetailActivity::class.java)
  10. val pairs = TransitionHelper.createSafeTransitionParticipants(
  11. context as Activity,true,
  12. Pair(ivPic,ResUtils.getString(R.string.transition_demo_pic)),
  13. Pair(ivHeader,ResUtils.getString(R.string.transition_demo_header)),
  14. Pair(tvName,ResUtils.getString(R.string.transition_demo_nickname))
  15. )
  16. transitionTo(intent, *pairs)
  17. }
  18. fun transitionTo(i: Intent?,vararg pairs: Pair<View, String>) {
  19. //val pairs: Array<Pair<View, String>> = TransitionHelper.createSafeTransitionParticipants(this,true)
  20. val transitionActivityOptions: ActivityOptionsCompat =
  21. ActivityOptionsCompat.makeSceneTransitionAnimation(context as Activity, *pairs)
  22. context.startActivity(i, transitionActivityOptions.toBundle())
  23. }

}

  1. - 2. xml

<?xml version=”1.0” encoding=”utf-8”?>

  1. <androidx.recyclerview.widget.RecyclerView
  2. android:id="@+id/rv_transition_demolist_list"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"/>

  1. 详情页同上。
  2. <a name="SNoJL"></a>
  3. ## 三、用到的工具类

package com.kiwilss.xview.help

import android.R import android.app.Activity import android.view.View import androidx.core.util.Pair import java.util.*

/**

  • Helper class for creating content transitions used with [android.app.ActivityOptions]. / internal object TransitionHelper { /*

    • Create the transition participants required during a activity transition while
    • avoiding glitches with the system UI. *
    • @param activity The activity used as start for the transition.
    • @param includeStatusBar If false, the status bar will not be added as the transition
    • participant.
    • @return All transition participants. */ fun createSafeTransitionParticipants( activity: Activity, includeStatusBar: Boolean, vararg otherParticipants: Pair ): Array> { // Avoid system UI glitches as described here: // https://plus.google.com/+AlexLockwood/posts/RPtwZ5nNebb val decor = activity.window.decorView var statusBar: View? = null if (includeStatusBar) {

      1. statusBar = decor.findViewById(R.id.statusBarBackground)

      } val navBar =

      1. decor.findViewById<View>(R.id.navigationBarBackground)

      // Create pair of transition participants. val participants: MutableList> =

      1. ArrayList(3)

      addNonNullViewToTransitionParticipants(statusBar, participants) addNonNullViewToTransitionParticipants(navBar, participants) // only add transition participants if there’s at least one none-null element if (!(otherParticipants.size == 1

      1. && otherParticipants[0] == null)

      ) {

      1. participants.addAll(listOf(*otherParticipants))

      } return participants.toTypedArray() }

      private fun addNonNullViewToTransitionParticipants( view: View?, participants: MutableList> ) { if (view == null) {

      1. return

      } participants.add(

      1. Pair(
      2. view,
      3. view.transitionName
      4. )

      ) } } ```