1

What I want to do:

Lets say I have an imageView in top right corner of the screen.

A. When I click on it it will move/translate to center of the screen

B. It should then animate into new activity using SharedElement Transition

I am able to do A using LINK and B using LINK

2 Questions

  1. Am I doing it right

  2. How to I combine A and B

After George's answers

v21/themes.xml

<resources>
<style name="AppTheme" parent="AppTheme.Base">
    <item name="android:windowContentTransitions">true</item>
    <item name="android:windowAllowEnterTransitionOverlap">true</item>
    <item name="android:windowAllowReturnTransitionOverlap">true</item>
    <item name="android:windowSharedElementExitTransition">@transition/exit_slide_transition</item>
    <item name="android:windowSharedElementEnterTransition">@transition/enter_transition</item>

</style>
</resources>

exit_slide_transition.xml

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <changeBounds
        android:duration="1000"
        android:interpolator="@android:anim/linear_interpolator"/>
</transitionSet>

linear.xml (used by AnimationUtils.loadAnimation)

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator">
    <translate
        android:fromYDelta="0"
        android:toYDelta="45%p"
        android:fromXDelta="0"
        android:toXDelta="45%p"
        android:duration="1000"/>
</set>

enter_transition.xml

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <changeImageTransform/>
</transitionSet>

Start Activity + moveImage():

//StartActivity:

    ActivityOptionsCompat options =
                    ActivityOptionsCompat.makeSceneTransitionAnimation(
                            activity, transitionView, EXTRA_IMAGE);
            Intent intent = new Intent(activity, DetailActivity.class);
            intent.putExtra(EXTRA_IMAGE, url);
            ActivityCompat.startActivity(activity, intent, options.toBundle());

//moveImage():

    RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams((int)getResources().getDimension(R.dimen.my_image_dp),(int)getResources().getDimension(R.dimen.my_image_dp));
layoutParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
imageView.setLayoutParams(layoutParams);
Community
  • 1
  • 1
T_C
  • 3,148
  • 5
  • 26
  • 46

1 Answers1

1

From what I read in the links, you are attempting to do the initial animation with Animators directly. It is possible to do this by using an Animator Listener (onAnimationEnd) to start the Activity, but I'm guessing that isn't what you are thinking.

The "correct" way is to use a SharedElementExitTransition to move the view before it transitions to the new Activity. You may use a ChangeTransform or ChangeBounds to move it, depending on the properties you modify(translation or position).

Essentially:

startActivity(intent, bundle);
moveImage();

That will cause the shared element's exit transition to run based on what you do in move image to put it in the final position. When it finishes, the launched activity will take it and move it to its final position using its shared element enter transition.

Edit:

It looks like you need to force the layout of the parent view if you just adjust the layout params. I'm not certain yet why that is necessary. After you call setLayoutParams, add this:

    View parent = (View) imageView.getParent();
    int widthSpec = View.MeasureSpec.makeMeasureSpec(parent.getWidth(), View.MeasureSpec.EXACTLY);
    int heightSpec = View.MeasureSpec.makeMeasureSpec(parent.getHeight(), View.MeasureSpec.EXACTLY);
    parent.measure(widthSpec, heightSpec);
    parent.layout(parent.getLeft(), parent.getTop(), parent.getRight(), parent.getBottom());

You could also get away with just changing the ImageView's left/top/right/bottom directly.

George Mount
  • 20,708
  • 2
  • 73
  • 61
  • SharedElementExitTransition !! that makes total sense, thanks will try it out – T_C Jan 30 '15 at 06:02
  • can you please check my updated question, I am now getting **RuntimeException: Unknown interpolator name: set** – T_C Feb 02 '15 at 19:25
  • 1
    The interpolator referenced in changeBounds should be @android:anim/linear_interpolator. linear.xml (referenced in changeBounds interpolator) is acting as an AnimationSet, not an interpolator. – George Mount Feb 03 '15 at 00:17
  • thanks for quick repl, i have tried that but I dont get expected results, one more thing if I have "@android:anim/linear_interpolator" in changeBounds then I am not having any code which moves the imageView from top right corner to center, where would I put that code and how ?? [updated the above answers with changes] – T_C Feb 03 '15 at 03:39
  • 1
    ChangeBounds creates an animator based on the two different states. For the shared element exit transition, it captures the first state when you call startActivity. It captures the second state before the next frame. So your job is to move the image to the place you want it to be after you call startActivity. You should update the layout to place the ImageView where you want it to go. The magic of Transitions is that you don't have to program the animations directly anymore, they're calculated for you. – George Mount Feb 03 '15 at 15:46
  • ok I get that part, one more question, hopefully last one, when you say moveImage() how do I exactly do it because I am using AnimationUtils.loadAnimation to slide image, so the way it works is the image moves from A --> B but then enter transition again starts from A and not B – T_C Feb 04 '15 at 01:05
  • 1
    Normally you do this by adjusting the layout. You can also do it by changing the translation. ChangeTransform will animate translation. – George Mount Feb 04 '15 at 05:23
  • I updated my above code with new moveImage(). I am still not getting expected results. The way its working now is image moves from A (top right corner) to B (screen center) immediately and then the ActivityTransition again starts from A and not B, Whats is wrong with the code now? – T_C Feb 04 '15 at 22:53
  • Thanks. your new code animates (or translates) the imageView from A —> B but the entry animation still starts from A :( – T_C Feb 05 '15 at 20:38
  • 1
    I can't say what is wrong. This is the test I was using: https://drive.google.com/file/d/0BzzMYfCAYzn4Z2RreU96LTdXZlk/view?usp=sharing – George Mount Feb 06 '15 at 00:28
  • I was using custom transition, but you are using android's move transition, which works :). Thanks you so much for your time I learned a lot. – T_C Feb 06 '15 at 01:16