Android SharedElementTransition API -- how can I make the new view expand in size from the original view? - Stack Overflow

admin2025-04-20  2

I'm using the SharedElementTransition API on Android for transition from a view on Activity 1 to a full screen view on Activity 2.

The API I'm using is:

ActivityOptionsCompat.makeSceneTransitionAnimation(activity2, sharedElementView, sharedElementName);

The two views are not the same and I want the new activity to expand out from the first view. Currently using the default transitions or ChangeBounds causes the second view to start zoomed in and zooms out as the transition happens. What can I do to make it scale out to full size instead?

I'm using the SharedElementTransition API on Android for transition from a view on Activity 1 to a full screen view on Activity 2.

The API I'm using is:

ActivityOptionsCompat.makeSceneTransitionAnimation(activity2, sharedElementView, sharedElementName);

The two views are not the same and I want the new activity to expand out from the first view. Currently using the default transitions or ChangeBounds causes the second view to start zoomed in and zooms out as the transition happens. What can I do to make it scale out to full size instead?

Share Improve this question asked Jan 25 at 0:17 CoreyCorey 1,0591 gold badge25 silver badges43 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 0

You should provide more code,ChangeBounds() is only responsible for changing the size and position of the view.If you just want to make the original view bigger, ssuming it's an imageView, make sure it takes up the entire screen and its transitionName is the same

     <ImageView
     android:id="@+id/shared_element_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:transitionName="@string/shared_element_name"
     android:scaleType="centerCrop"
     android:src="@drawable/example_image" />

It's in your Activity2

View sharedElementView = findViewById(R.id.shared_element_view);
TransitionSet transitionSet = new TransitionSet()
            .addTransition(new ChangeBounds())
            .addTransition(new ChangeTransform())
            .addTransition(new ChangeClipBounds());

transitionSet.setDuration(500);  //set transition duration

getWindow().setSharedElementEnterTransition(transitionSet);
getWindow().setSharedElementReturnTransition(transitionSet);

If you want to set the scale

sharedElementView.setScaleX(0.5f);
sharedElementView.setScaleY(0.5f);

I ended up using a MaterialContainerTransform instead which did the expansion that I was looking for: https://developer.android.com/reference/com/google/android/material/transition/MaterialContainerTransform

Specifically I had to use the one from com.google.android.material.transition.platform.MaterialContainerTransform in order to use in my activity to activity transition instead of com.google.android.material.transition.MaterialContainerTransform which is used for fragments (note the lack of platform in the path).

转载请注明原文地址:http://anycun.com/QandA/1745116455a90381.html