Hello friends today we are learn about how to add animation when communicate two activity or fragments.Most of beginner developer not able to add animation.
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
MainActivity.java
import android.app.Activity;
import android.app.ActivityOptions;
import android.app.SharedElementCallback;
import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.TextView;
import com.samset.simpleanimation.R;
import java.util.List;
public class MainActivity extends Activity {
View first, second, image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first = findViewById(R.id.hello);
second = findViewById(R.id.sam);
image = findViewById(R.id.img);
setExitSharedElementCallback(new SharedElementCallback() {
@Override
public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
findViewById(R.id.hello).setTranslationZ(0);
}
});
}
public void clicked(View view) {
/*
actvity_target.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
TargetActvity.java
ActvityTransition.java
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.samset.simpleanimation.activities.MainActivity"
android:onClick="clicked">
<ImageView
android:id="@+id/img"
android:transitionName="@string/images_transition"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="150dp" />
<TextView
android:layout_marginTop="10dp"
android:text="@string/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:transitionName="@string/first_transition"
android:textSize="25sp"
android:id="@+id/hello"
android:padding="5pt"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
<TextView
android:text="@string/lname"
android:layout_width="wrap_content"
android:textSize="25sp"
android:padding="5pt"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/sam"
android:text="@string/address"
android:layout_width="wrap_content"
android:textSize="25sp"
android:padding="5pt"
android:transitionName="@string/second_transition"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
<TextView android:text="@string/contact"
android:layout_width="wrap_content"
android:textSize="25sp"
android:padding="5pt"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
</LinearLayout>
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.samset.simpleanimation.activities.MainActivity"
android:onClick="clicked">
<ImageView
android:id="@+id/img"
android:transitionName="@string/images_transition"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="150dp" />
<TextView
android:layout_marginTop="10dp"
android:text="@string/fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:transitionName="@string/first_transition"
android:textSize="25sp"
android:id="@+id/hello"
android:padding="5pt"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
<TextView
android:text="@string/lname"
android:layout_width="wrap_content"
android:textSize="25sp"
android:padding="5pt"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="@+id/sam"
android:text="@string/address"
android:layout_width="wrap_content"
android:textSize="25sp"
android:padding="5pt"
android:transitionName="@string/second_transition"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
<TextView android:text="@string/contact"
android:layout_width="wrap_content"
android:textSize="25sp"
android:padding="5pt"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
</LinearLayout>
MainActivity.java
import android.app.Activity;
import android.app.ActivityOptions;
import android.app.SharedElementCallback;
import android.content.Intent;
import android.os.Bundle;
import android.util.Pair;
import android.view.View;
import android.widget.TextView;
import com.samset.simpleanimation.R;
import java.util.List;
public class MainActivity extends Activity {
View first, second, image;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first = findViewById(R.id.hello);
second = findViewById(R.id.sam);
image = findViewById(R.id.img);
setExitSharedElementCallback(new SharedElementCallback() {
@Override
public void onSharedElementEnd(List<String> sharedElementNames, List<View> sharedElements, List<View> sharedElementSnapshots) {
findViewById(R.id.hello).setTranslationZ(0);
}
});
}
public void clicked(View view) {
/*
// first options you can also use this class
Pair<View, String> pair1 = Pair.create(first, first.getTransitionName());
Pair<View, String> pair2 = Pair.create(second, second.getTransitionName());
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, pair1, pair2);*/
// second options you can also use this class
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this,
new Pair(first, getResources().getString(R.string.first_transition)),
new Pair(first, getResources().getString(R.string.second_transition)),
new Pair(image, getResources().getString(R.string.images_transition)));
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent, options.toBundle());
first.setTranslationZ(16);
second.setTranslationZ(16);
//image.setTranslationZ(25);
}
}
Pair<View, String> pair2 = Pair.create(second, second.getTransitionName());
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, pair1, pair2);*/
// second options you can also use this class
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(MainActivity.this,
new Pair(first, getResources().getString(R.string.first_transition)),
new Pair(first, getResources().getString(R.string.second_transition)),
new Pair(image, getResources().getString(R.string.images_transition)));
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent, options.toBundle());
first.setTranslationZ(16);
second.setTranslationZ(16);
//image.setTranslationZ(25);
}
}
actvity_target.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.samset.simpleanimation.activities.MainActivity2">
<LinearLayout
android:padding="5pt"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="First Name : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
/>
<TextView
android:text="@string/sfname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="@string/first_transition"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</LinearLayout>
<LinearLayout
android:padding="5pt"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:text="Last Name : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="sam"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
<TextView
android:text="@string/slname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
</LinearLayout>
<LinearLayout
android:padding="5pt"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:text="Adress : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="sam"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
<TextView
android:text="@string/saddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="@string/second_transition"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
</LinearLayout>
<LinearLayout
android:padding="5pt"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:text="Contact : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="sam"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
<TextView
android:text="@string/scontact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="hello"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
</LinearLayout>
<ImageView
android:transitionName="@string/images_transition"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="300dp" />
</LinearLayout>
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.samset.simpleanimation.activities.MainActivity2">
<LinearLayout
android:padding="5pt"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:text="First Name : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"/>
/>
<TextView
android:text="@string/sfname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="@string/first_transition"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</LinearLayout>
<LinearLayout
android:padding="5pt"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:text="Last Name : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="sam"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
<TextView
android:text="@string/slname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
</LinearLayout>
<LinearLayout
android:padding="5pt"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:text="Adress : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="sam"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
<TextView
android:text="@string/saddress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="@string/second_transition"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
</LinearLayout>
<LinearLayout
android:padding="5pt"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:text="Contact : "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="sam"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"/>
<TextView
android:text="@string/scontact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:transitionName="hello"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"/>
</LinearLayout>
<ImageView
android:transitionName="@string/images_transition"
android:scaleType="fitXY"
android:src="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="300dp" />
</LinearLayout>
TargetActvity.java
import android.app.Activity;
import android.os.Bundle;
import com.samset.simpleanimation.R;
public class TargetActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);
}
}
import android.os.Bundle;
import com.samset.simpleanimation.R;
public class TargetActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_target);
}
}
ActvityTransition.java
import android.animation.Animator;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.transition.Transition;
import android.transition.TransitionValues;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by samset on 05/03/2016.
*/
public class ShadowTransition extends Transition {
private static final String PROPERTY_TRANSLATION_Z = "shadow:translationZ";
private static final String[] PROPERTIES = { PROPERTY_TRANSLATION_Z };
public ShadowTransition() {
}
public ShadowTransition(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public String[] getTransitionProperties() {
return PROPERTIES;
}
private void captureValues(TransitionValues transitionValues) {
float z = transitionValues.view.getTranslationZ();
transitionValues.values.put(PROPERTY_TRANSLATION_Z, z);
}
@Override
public void captureStartValues(TransitionValues transitionValues) {
captureValues(transitionValues);
}
@Override
public void captureEndValues(TransitionValues transitionValues) {
captureValues(transitionValues);
}
@Override
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
if (startValues == null || endValues == null ||
!startValues.values.containsKey(PROPERTY_TRANSLATION_Z) ||
!endValues.values.containsKey(PROPERTY_TRANSLATION_Z)) {
return null;
}
final float startZ = (Float) startValues.values.get(PROPERTY_TRANSLATION_Z);
final float endZ = (Float) endValues.values.get(PROPERTY_TRANSLATION_Z);
final View view = endValues.view;
view.setTranslationZ(startZ);
return ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, startZ, endZ);
}
}
import android.animation.ObjectAnimator;
import android.content.Context;
import android.transition.Transition;
import android.transition.TransitionValues;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by samset on 05/03/2016.
*/
public class ShadowTransition extends Transition {
private static final String PROPERTY_TRANSLATION_Z = "shadow:translationZ";
private static final String[] PROPERTIES = { PROPERTY_TRANSLATION_Z };
public ShadowTransition() {
}
public ShadowTransition(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public String[] getTransitionProperties() {
return PROPERTIES;
}
private void captureValues(TransitionValues transitionValues) {
float z = transitionValues.view.getTranslationZ();
transitionValues.values.put(PROPERTY_TRANSLATION_Z, z);
}
@Override
public void captureStartValues(TransitionValues transitionValues) {
captureValues(transitionValues);
}
@Override
public void captureEndValues(TransitionValues transitionValues) {
captureValues(transitionValues);
}
@Override
public Animator createAnimator(ViewGroup sceneRoot, TransitionValues startValues, TransitionValues endValues) {
if (startValues == null || endValues == null ||
!startValues.values.containsKey(PROPERTY_TRANSLATION_Z) ||
!endValues.values.containsKey(PROPERTY_TRANSLATION_Z)) {
return null;
}
final float startZ = (Float) startValues.values.get(PROPERTY_TRANSLATION_Z);
final float endZ = (Float) endValues.values.get(PROPERTY_TRANSLATION_Z);
final View view = endValues.view;
view.setTranslationZ(startZ);
return ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, startZ, endZ);
}
}
Thank you.
I hope this blog helps you.
No comments:
Post a Comment