Wednesday, 27 May 2015

Shared Element Activity Transition



Getting Started

Note that the shared element transitions require Android 5.0 (API level 21) and above and will be ignored for any lower API versions. Be sure to check the version at runtime before using API 21 specific features.
1.Enable Window Content Transitions in your styles.xml file:
<style name="BaseAppTheme" parent="android:Theme.Material">
  enable window content transitions
  <item name="android:windowContentTransitions">true</item>

this can be enabled from activity also 


from Activity

 Window window = getWindow();
        window.requestFeature(Window.FEATURE_CONTENT_TRANSITIONS);

2. Assign a common transition name for the view you are going to animate in both the activities
for example



1.android:transitionName="profile" 
activity A 


 <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_below="@+id/logolay"
        android:scaleType="centerCrop"
        android:text="@string/hello_world"
        android:transitionName="profile" />


activity B

 <ImageView
        android:id="@+id/image"
        android:layout_width="match_parent"
        android:layout_height="240dp"
        android:layout_below="@+id/logolay"
        android:scaleType="centerCrop"
        android:text="@string/hello_world"
        android:transitionName="profile" />


FROM ACTIVITY A

window.setSharedElementExitTransition(TransitionInflater.from(this).inflateTransition(R.transition.shared_element_transition_a));
setContentView(R.layout.ActivityA);

and in on click of the image view in the Activity A


View v = view.findViewById(R.id.imageView2);
ActivityOptionsCompat OptionsCompat= ActivityOptionsCompat.makeSceneTransitionAnimation(ActivityA.this, v,profile(transitionname));

 Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent, OptionsCompat.toBundle());

create a folder in res/transition
shared_element_transition_a.xml

<?xml version="1.0" encoding="utf-8"?>
<transitionSet
    android:duration="500"
     xmlns:android="http://schemas.android.com/apk/res/android">
    <changeBounds/>
</transitionSet>


In ACTIVITY B

window.setSharedElementEnterTransition(TransitionInflater.from(this).inflateTransition(R.transition.shared_element_transition_a));
setContentView(R.layout.ActivityB);



Thats it!!!!!! good luck





No comments:

Post a Comment