Aug 2, 2012

Apply fade-in, fade-out effect in activity transition

Create XML files to define fadein/fadeout effect:

/res/anim/fadein.xml
<?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="3000" />


/anim/fadeout.xml
<?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="3000" />


Apply the effect to activity by calling overridePendingTransition(R.anim.fadein, R.anim.fadeout).
package com.example.androidfadein;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        overridePendingTransition(R.anim.fadein, R.anim.fadeout);
        
    }

 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    
}



No comments:

Post a Comment

Infolinks In Text Ads