/** * Sent by Activity#startActivity to notify the entering activity that enter animation for * back is allowed. If this message is not received, the default exit animation will run when * backing out of an activity (instead of the 'reverse' shared element transition). */ publicstaticfinalintMSG_ALLOW_RETURN_TRANSITION=108;
/** * @param sharedElements 共享元素属性 */ openfunbuildSharedElements(vararg sharedElements: Pair<View, String>): ArrayList<String> { val names = ArrayList<String>() for (i in sharedElements.indices) { val sharedElement: Pair<View, String> = sharedElements[i] val sharedElementName = sharedElement.second ?: throw IllegalArgumentException("Shared element name must not be null") names.add(sharedElementName) val view = sharedElement.first ?: throw IllegalArgumentException("Shared element must not be null") views.add(sharedElement.first) } return names }
使用反射,给mPendingExitNames赋值
@SuppressLint("PrivateApi")funupdateResume(activity: Activity) { try { ActivityOptions.makeSceneTransitionAnimation(this) val stateField: Field = ReflectionUtil.getField( Activity::class.java, "mActivityTransitionState" ) val stateObj = stateField.get(activity) val activityTransitionStateClazz = classLoader.loadClass("android.app.ActivityTransitionState") val mPendingExitNamesField: Field = ReflectionUtil.getField( activityTransitionStateClazz, "mPendingExitNames" ) // 设置当前Activity需要的共享元素 val appIcon = Pair<View, String>(binding.appIcon, getString(string.transition_app_icon)) val dbName = Pair<View, String>(binding.dbName, getString(string.transition_db_name)) val dbVersion = Pair<View, String>(binding.dbVersion, getString(string.transition_db_version)) val dbLittle = Pair<View, String>(binding.arrow, getString(string.transition_db_little)) mPendingExitNamesField.set(stateObj, buildSharedElements(appIcon, dbName, dbVersion, dbLittle)) } catch (e: java.lang.Exception) { e.printStackTrace() } }