[Android] Activity transition animation

Android 5.0에서 activity나 fragment 전환할 때 Shared Element transition animation이 제공된다.

전환 전의 View에 transition name을 설정하고, 전환 후의 Activity나 fragment의 view에 transition name을 설정하면 animation이 동작된다.

 

적용하는 방법은 아래와 같다.


1. style.xml에서 windowSharedElementEnterTransition의 style을 적용하는 방법

<style name="BaseAppTheme" parent="android:Theme.Material">
  <!-- enable window content transitions -->
  <item name="android:windowContentTransitions">true</item>

  <!-- specify enter and exit transitions -->
  <item name="android:windowEnterTransition">@transition/explode</item>
  <item name="android:windowExitTransition">@transition/explode</item>

  <!-- specify shared element transitions -->
  <item name="android:windowSharedElementEnterTransition">
    @transition/change_image_transform</item>
  <item name="android:windowSharedElementExitTransition">
    @transition/change_image_transform</item>
</style>

change_image_transform.xml

<!-- res/transition/change_image_transform.xml -->
<!-- (see also Shared Transitions below) -->
<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
  <changeImageTransform/>
</transitionSet>

2. activity에서 아래의 API를 사용하여 적용하는 방법

To specify transitions in your code, call these methods with a Transition object:

3. fragment에서 아래의 API를 사용하여 적용하는 방법

void setSharedElementEnterTransition(Transition transition)

Sets the Transition that will be used for shared elements transferred into the content Scene.
void setSharedElementReturnTransition(Transition transition)

Sets the Transition that will be used for shared elements transferred back during a pop of the back stack.

 

ex)

view.setTransitionName(MusicPlayListData.transitionName);

Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra(“pos”, MusicPlayListData.getCurrentPosition());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
ActivityOptions activityOptions = ActivityOptions.makeSceneTransitionAnimation(this, new Pair<View, String>(
view, MusicPlayListData.transitionName));

startActivity(intent, activityOptions.toBundle());


<item name=”android:windowContentTransitions”>true</item>

<!– Set the transitions which are used for the entrance and exit of shared elements –>
<item name=”android:windowSharedElementEnterTransition”>
@drawable/grid_detail_transition
</item>
<item name=”android:windowSharedElementExitTransition”>
@drawable/grid_detail_transition
</item>

ps. 필자는 Material Theme에서 확인함. 다른 Theme에서도 동작하는지는 확인 필요.

Advertisements
[Android] Activity transition animation

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s