Reputation: 632
I'm working with Android Lollipop Transitions, and I've sumbled upon the following problem:
CardView
with an ImageView
and, on top of it, a TextView
.ImageView
and the TextView
in different positions.TextView
in the Transition
as a shared element, it suddenly dissapears [goes behind] the ImageView
, which doesn't look, well, great.ImageView
Transition
, which is a combination of a ChangeBounds
Transition
, a ChangeImageTransform
, ... among others).So, anybody knows how to have different transitions being thrown for different shared views when launching the new Activity
?
Cheers
Upvotes: 2
Views: 2241
Reputation: 16968
The way you pointed to it in your answer can be applied for this purpose but it's actually for accepting only suitable View
types by a special transition
(such as only ImageView
s for ChangeImageTransform
).
You can use addTarget(Class targetType)
instead:
final Transition transition = new TransitionSet()
.addTransition(new ChangeTransform()).addTarget(TextView.class) // Only for TextViews
.addTransition(new ChangeImageTransform()).addTarget(ImageView.class) // Only for ImageViews
.addTransition(new ChangeBounds()); // For both
setSharedElementEnterTransition(transition);
This way is more simple and more logical. Also, it can be used for some other filterings (by transitionName
and etc). See addTarget()
overloaded types.
Upvotes: 2
Reputation: 632
OK,
This is achievable extending the Transition class. Since I wanted to animate differently an ImageView and a TextView, I just wrote a TextTransform
child class ofTransform
, analogous to the ChangeImageTransform
one which is part of the Android API 21+. The key was overriding this method (shown the case for ChangeImageTransform which looks for ImageView
objects):
@Override
private void captureValues(TransitionValues transitionValues) {
View view = transitionValues.view;
if (!(view instanceof ImageView) || view.getVisibility() != View.VISIBLE) {
return;
}
(...)
}
Then you apply all transforms to the new scene, and relevant transform will be attached to their corresponding views:
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
<fade android:fadingMode="fade_out" />
<changeBounds />
<changeImageTransform />
<com.mypackage.transforms.TextTransform />
<fade android:fadingMode="fade_in" />
</transitionSet>
And then you set this Transition
on the OnCreate method of the new Activity
using setSharedElementEnterTransition(inflatedTransitionSet);
Upvotes: 0