Monday, January 30, 2012

Animate the layout's towards right and left i.e moving towards outside?

Create two xml file in anim folder of application...


1)push_left_out.xml



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


2)push_right_out.xml



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


In OnCreate() method take the reference of layout or any view's



img = (ImageView) findViewById(R.id.imageView1);
layout = (LinearLayout) findViewById(R.id.lin);


Animation an1, an2;

an1 = AnimationUtils.loadAnimation(this, R.anim.push_right_out);
an2 = AnimationUtils.loadAnimation(this, R.anim.push_left_out);


img.startAnimation(an1);
layout.startAnimation(an2);
In above example the layout will move towards left and imageview towards right...






Sunday, January 29, 2012

How to fade the current activity ?


On your onCreate() method add something like this:
    final Button button = (Button) findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {

            final View l = findViewById(R.id.main);

            Animation a = AnimationUtils.loadAnimation(
                    YourActivity.this, android.R.anim.fade_out);
            a.setDuration(200);
            a.setAnimationListener(new AnimationListener() {

                public void onAnimationEnd(Animation animation) {
                        // Do what ever you need, if not remove it.  
                }

                public void onAnimationRepeat(Animation animation) {
                        // Do what ever you need, if not remove it.  
                }

                public void onAnimationStart(Animation animation) {
                        // Do what ever you need, if not remove it.  
                }

            });
            l.startAnimation(a);
        }
    });

How to use animation between two acitivities i.e. transition ?


Here's the code to do a nice smooth fade between two Activities..
Create a file called fadein.xml in res/anim
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/accelerate_interpolator"
   android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="2000" />
Create a file called fadeout.xml in res/anim
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
   android:interpolator="@android:anim/accelerate_interpolator"
   android:fromAlpha="1.0" android:toAlpha="0.0" android:duration="2000" />
If you want to fade from Activity A to Activity B, put the following on button click
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
If the fades are too slow for you, change android:duration in the xml files above to something smaller.
button_click.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
Intent mainIntent = new Intent(this,B.class);
ctx.startActivity(mainIntent);
overridePendingTransition(R.anim.fadein, R.anim.fadeout);
}
}, 900);
}
});