Этот код взят из одного из моих проектов.Не стесняйтесь использовать его в своем.Ваша деятельность должна расширять ее и делать, как описано в HOWTO.
/**
* FadedActivity is a subclass of Activity. The difference with a
* standard activity is the fade in/out animation launched when
* the activity begins/ends.
*
* <p>
* HOWTO:
* <ul>
* <li>layout's main layer must have android:id="root"
* <li>derived class must call FadedActivity's super.onCreate() instead of Activity's
* <li>use finishFade() instead of finish()
* </ul>
*
* @author Joel
* @version 1.0
* @since 1.0
*/
public static class FadedActivity extends Activity {
private static final int FADEIN_DELAY_MS = 1000;
private static final int FADEOUT_DELAY_MS = 500;
private View root;
private void runFadeAnimationOn(Activity ctx, View target, boolean in, int delay) {
int start, finish;
if (in) {
start = 0;
finish = 1;
} else {
start = 1;
finish = 0;
}
AlphaAnimation fade = new AlphaAnimation(start, finish);
fade.setDuration(delay);
fade.setFillAfter(true);
target.startAnimation(fade);
}
public void onCreate(Bundle savedInstanceState, int layoutId) {
super.onCreate(savedInstanceState);
setContentView(layoutId);
root = (View)findViewById(R.id.root);
runFadeAnimationOn(this, root, true, FADEIN_DELAY_MS);
}
public void finishFade() {
final int delay = FADEOUT_DELAY_MS;
runFadeAnimationOn(this, root, false, delay);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(delay);
} catch (InterruptedException e) {
e.printStackTrace();
}
FadedActivity.super.finish();
}
}).start();
}
}