Custom Transition in Android
We can create custom transition through overridePendingTransition.overridePendingTransition Call immediately after one of the flavors of finish() or startActivity(Intent) to specify an explicit transition animation to perform next.
Create Android Project with name as "TransitionDemo" with package name "com.etr.transitiondemo". Create Activity with name as "TransitionActivity". This is the code for TransitionActivity.
You can download source code here.
Create another Activity with name as TransitionActivityOne.This is code for TransitionActivityOne.
Create Custom animation file inside of Animation folder.
This is code for left_to_rights_in.xml
This is code for left_to_rights_out.xml
This is code for right_to_left_in.xml
This is code for right_to_left_out.xml
Create Android Project with name as "TransitionDemo" with package name "com.etr.transitiondemo". Create Activity with name as "TransitionActivity". This is the code for TransitionActivity.
You can download source code here.
package com.etr.transitiondemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TransitionActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(new Intent(TransitionActivity.this,
TransitionActivityOne.class));
TransitionActivity.this
.overridePendingTransition(
R.anim.left_to_rights_out,
R.anim.left_to_rights_in);
finish();
}
});
} catch (Exception e) {
Log.d("Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.transition, menu);
return true;
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TransitionActivity extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(new Intent(TransitionActivity.this,
TransitionActivityOne.class));
TransitionActivity.this
.overridePendingTransition(
R.anim.left_to_rights_out,
R.anim.left_to_rights_in);
finish();
}
});
} catch (Exception e) {
Log.d("Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.transition, menu);
return true;
}
}
Create another Activity with name as TransitionActivityOne.This is code for TransitionActivityOne.
package com.etr.transitiondemo;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TransitionActivityOne extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition_one);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(new Intent(TransitionActivityOne.this,
TransitionActivity.class));
TransitionActivityOne.this.overridePendingTransition(
R.anim.right_to_left_out, R.anim.right_to_left_in);
finish();
}
});
} catch (Exception e) {
Log.d("Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.transition, menu);
return true;
}
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TransitionActivityOne extends Activity {
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
try {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transition_one);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
startActivity(new Intent(TransitionActivityOne.this,
TransitionActivity.class));
TransitionActivityOne.this.overridePendingTransition(
R.anim.right_to_left_out, R.anim.right_to_left_in);
finish();
}
});
} catch (Exception e) {
Log.d("Exception", Log.getStackTraceString(e));
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.transition, menu);
return true;
}
}
Create Custom animation file inside of Animation folder.
This is code for left_to_rights_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<translate
android:duration="400"
android:fromXDelta="-100%p"
android:toXDelta="0%p" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<translate
android:duration="400"
android:fromXDelta="-100%p"
android:toXDelta="0%p" />
</set>
This is code for left_to_rights_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<translate
android:duration="400"
android:fromXDelta="0%p"
android:toXDelta="100%p" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<translate
android:duration="400"
android:fromXDelta="0%p"
android:toXDelta="100%p" />
</set>
This is code for right_to_left_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<translate
android:duration="400"
android:fromXDelta="100%p"
android:toXDelta="0%p" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<translate
android:duration="400"
android:fromXDelta="100%p"
android:toXDelta="0%p" />
</set>
This is code for right_to_left_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<translate
android:duration="400"
android:fromXDelta="0%p"
android:toXDelta="-100%p" />
</set>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator" >
<translate
android:duration="400"
android:fromXDelta="0%p"
android:toXDelta="-100%p" />
</set>
Comments
Post a Comment