Code to Animate two layouts from top to bottom and bottom to top,in Android

MainActivity.java:-

package com.example.animationlayouts;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;

public class MainActivity extends Activity {

    View image_details_fragment=null,image_list_fragment=null;
    int cnt=1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainscreen);
        View b=findViewById(R.id.button1);
        image_details_fragment=findViewById(R.id.image_details_fragment);
        image_list_fragment=findViewById(R.id.image_list_fragment);
        image_details_fragment.requestFocus();
       
       
       
        b.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                image_details_fragment.startAnimation(AnimationUtils
                        .loadAnimation(MainActivity.this,R.anim.rotate_left));
                image_list_fragment.startAnimation(AnimationUtils.loadAnimation(MainActivity.this,R.anim.rotate_right));
                final Handler handler = new Handler();
               
                handler.postDelayed(new Runnable() {
                  @Override
                  public void run() {                  
                      System.out.println("Cnt value :"+cnt);
                        if(cnt%2==0)
                        {
                            System.out.println(" If True Image Details on Front ");
                            image_details_fragment.bringToFront();
                            image_details_fragment.requestFocus();
                            //image_list_fragment.invalidate();
                        }
                        else
                        {
                            System.out.println("else true Image List on Front ");
                            image_list_fragment.bringToFront();
                            image_list_fragment.requestFocus();
                            //image_details_fragment.invalidate();
                        }
                        cnt++;
                        image_details_fragment.startAnimation(AnimationUtils
                                .loadAnimation(MainActivity.this,
                                        R.anim.rotate_center_after_left));
                        image_list_fragment.startAnimation(AnimationUtils
                                .loadAnimation(MainActivity.this,
                                        R.anim.rotate_right_center));
                  }
                }, 500);               
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}




mainscreen.xml:-



<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Animate Now"/>

    
    <LinearLayout
        android:id="@+id/image_list_fragment"
        android:layout_width="250dip"
        android:layout_below="@+id/button1"
        android:layout_height="200dip"
        android:background="#FF00FF"
        android:layout_centerInParent="true"
        >
         <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Please Enter some text here"
        android:singleLine="false"
        />
    </LinearLayout>

  
 <LinearLayout
        android:layout_width="200dip"
        android:layout_height="250dip"
        android:orientation="vertical"
        android:layout_below="@+id/button1"
        android:visibility="visible"
        android:background="#32FFFF"
        android:layout_centerInParent="true"
        android:id="@+id/image_details_fragment"
       >
  <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="Please Enter some text here"
        android:singleLine="true"
        />
  </LinearLayout>
</RelativeLayout>





 res/anim/rotate_center_after_left.xml:-
<?xml version="1.0" encoding="utf-8"?>
<set>
  <translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXDelta="-200"
   android:toXDelta="0"
   android:interpolator="@android:anim/decelerate_interpolator"
   android:duration="500"/>
</set>


res/anim/rotate_in.xml 
<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
  
  <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="3000"/> 
  <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="300" /> 
  
</set>   


res/anim/rotate_left.xml:-
<?xml version="1.0" encoding="utf-8"?>
<set>
  <translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXDelta="0"
   android:toXDelta="-200"
   android:interpolator="@android:anim/decelerate_interpolator"
   android:duration="500"/>
</set>


res/anim/rotate_out.xml:-
<?xml version="1.0" encoding="utf-8"?>  
<set xmlns:android="http://schemas.android.com/apk/res/android"> 
   <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="3000" /> 
   <rotate android:fromDegrees="0" android:toDegrees="90" android:pivotX="25%" />
</set>


res/anim/rotate_right_center.xml:-
<?xml version="1.0" encoding="utf-8"?>
<set>
  <translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXDelta="200"
   android:toXDelta="0"
   android:interpolator="@android:anim/decelerate_interpolator"
   android:duration="500"/>
</set>


res/anim/rotate_right.xml:-
<?xml version="1.0" encoding="utf-8"?>
<set>
  <translate xmlns:android="http://schemas.android.com/apk/res/android"
   android:fromXDelta="0"
   android:toXDelta="200"
   android:interpolator="@android:anim/decelerate_interpolator"
   android:duration="500"/>
</set>






Comments

Popular posts from this blog

Custom Dialog with no title and full screen in Android

Best working code to set Date limits on Date Picker Dialog,in Android

code to display .pdf,.text,.DOC,.DOCX,.doc extension files exists in SDCard