Android Activity Animation Tutorial
In this tutorial you can learn how to animate two activities like android navigation drawer style.
- First of all create 2 activities.
- MainActivity
- SubActivity
- Then 2 XML Files (simple way two xml enough,but we need another two xml to accomplish reverse animation)
- Right lick your project root folder and goto New-> then Click Android XML File
- Then Select Resource type as Tween Animation and select Root Element as translate,then give file name (slide_in_left.xml ). repeat this task to second xml ( slide_in_left.xml ).
- Then add flowing codes to that files.
slide_in_left.xml
- <?xml version="1.0" encoding="utf-8"?>
- <translate xmlns:android="http://schemas.android.com/apk/res/android"
- android:duration="500"
- android:fromXDelta="100%p"
- android:toXDelta="0" />
slide_out.left.xml
- <?xml version="1.0" encoding="utf-8"?>
- <translate xmlns:android="http://schemas.android.com/apk/res/android"
- android:duration="500"
- android:fromXDelta="0"
- android:toXDelta="-50%p" />
Now in MainActivity.java create a button and add this code in onClickListener under onClick event to start out second activity with animation.
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.View;
- import android.widget.Button;
- public class MainActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final Button defaultButton = (Button) findViewById(R.id.button1);
- defaultButton.setOnClickListener(new View.OnClickListener() {
- @Override
public void onClick(View v) {
Intent subActivity = new Intent(MainActivity.this,SubActivity.class);
- startActivity(subActivity);
- overridePendingTransition(R.anim.side_in_left, R.anim.slide_out_left);//animation part
- }
- });
- }
- }
This is the basic animation task.you can create another animation xml file with reverse values and call it from SubActivity.java by overriding onFinish() method.
- import android.os.Bundle;
- import android.app.Activity;
- public class SubActivity extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_sub);
- }
- @Override
- public void finish() {
- super.finish(); overridePendingTransition( R.anim.slide_in_right,R.anim.slide_out_right);/*override and add your own[think 10 seconds and create that files :)] reverse animation xml files */
- }
- }
Thats all have fun with android animation!
This is grate help for me.
ReplyDelete