问题
I wrote a transtion that extends androidx.transition.Transtion
which rotates view from start value to end value, this just an example it happens with other classes that extends Transition.
It works fine if i use TransitionManager.beginDelayedTransition()
if i use the start and end values as arguments instead of endTransitionValues.
class CustomRotationTransition : Transition {
private var startRotation: Float = 0f
private var endRotation: Float = 0f
var forceValues: Boolean = false
constructor(startRotation: Float, endRotation: Float, forceValues: Boolean=false) {
this.startRotation = startRotation
this.endRotation = endRotation
this.forceValues = forceValues
}
constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
val a = context.obtainStyledAttributes(attrs, R.styleable.CustomRotationTransition)
startRotation =
a.getFloat(R.styleable.CustomRotationTransition_startRotation, startRotation)
endRotation = a.getFloat(R.styleable.CustomRotationTransition_startRotation, endRotation)
a.recycle()
}
override fun captureStartValues(transitionValues: TransitionValues) {
if (forceValues) {
transitionValues.values[PROPNAME_ROTATION] = startRotation
} else {
captureValues(transitionValues)
}
println("⚠️ ${this::class.java.simpleName} captureStartValues() view: ${transitionValues.view} ")
transitionValues.values.forEach { (key, value) ->
println("Key: $key, value: $value")
}
}
override fun captureEndValues(transitionValues: TransitionValues) {
if (forceValues) {
transitionValues.values[PROPNAME_ROTATION] = endRotation
} else {
captureValues(transitionValues)
}
println("🔥 ${this::class.java.simpleName} captureEndValues() view: ${transitionValues.view} ")
transitionValues.values.forEach { (key, value) ->
println("Key: $key, value: $value")
}
}
private fun captureValues(transitionValues: TransitionValues) {
transitionValues.values[PROPNAME_ROTATION] = transitionValues.view.rotation
}
override fun createAnimator(
sceneRoot: ViewGroup,
startValues: TransitionValues?,
endValues: TransitionValues?
): Animator? {
println("🎃 ${this::class.java.simpleName} createAnimator() startValues: $startValues endValues: $endValues ")
if (endValues == null || startValues == null) return null // no values
val startRotation = startValues.values[PROPNAME_ROTATION] as Float
val endRotation = endValues.values[PROPNAME_ROTATION] as Float
if (startRotation == endRotation) return null // no rotation to run
val view = startValues.view
val propRotation =
PropertyValuesHolder.ofFloat(PROPNAME_ROTATION, startRotation, endRotation)
val valAnim = ValueAnimator.ofPropertyValuesHolder(propRotation)
valAnim.addUpdateListener { valueAnimator ->
view.pivotX = view.width / 2f
view.pivotY = view.height / 2f
view.rotation = valueAnimator.getAnimatedValue(PROPNAME_ROTATION) as Float
}
return valAnim
}
companion object {
private const val PROPNAME_ROTATION = "PROPNAME_ROTATION"
}
}
When i set this transition for exit transition on a view for instance, when a fragment transition, there is also an image for shared Transition, and other transitions work.
val slide = Slide(Gravity.TOP)
.apply {
duration = 3000
}
val transitionSet = TransitionSet()
val textTransition =
CustomRotationTransition(-360f,0f, true)
.apply {
addTarget(tvExitTransition)
duration = 800
}
transitionSet.addTransition(slide)
transitionSet.addTransition(textTransition)
exitTransition = transitionSet
I: ⚠️ CustomRotationTransition captureStartValues() view: com.google.android.material.textview.MaterialTextView{ee699a0 V.ED..... ........ 79,1544-213,1598 #7f08018c app:id/tvExitTransition}
I: Key: PROPNAME_ROTATION, value: -360.0
I: 🎃 CustomRotationTransition createAnimator() startValues: TransitionValues@5e18b379:
I: view = com.google.android.material.textview.MaterialTextView{ee699a0 V.ED..... ........ 79,1544-213,1598 #7f08018c app:id/tvExitTransition}
I: values: android:visibilityPropagation:center: [I@e8877ad
I: android:visibilityPropagation:visibility: 0
I: PROPNAME_ROTATION: -360.0
I: endValues: null
i see that captureEndValues not called, i really can't get why it does not called
来源:https://stackoverflow.com/questions/64498661/in-transition-when-fragment-shared-element-transitions-happen-sometimes-capturee