switch View with Animation - Android User Interface

Android examples for User Interface:View Animation

Description

switch View with Animation

Demo Code


//package com.java2s;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationSet;

public class Main {
    private static AnimationSet showAction;
    private static AnimationSet hideAction;

    public static void switchView(View view1, View view2,
            AnimationListener listener) {
        if (hideAction == null) {
            initAnimation();//from w  w  w  .  j  ava 2s  . c o m
        }
        if (view2.getVisibility() != View.VISIBLE) {
            view2.setVisibility(View.VISIBLE);
            view1.startAnimation(hideAction);
            Animation animation = new AlphaAnimation(0.0f, 1.0f);
            animation.setStartOffset(1200);
            animation.setDuration(800);
            animation.setAnimationListener(listener);
            view2.startAnimation(animation);
        }

    }

    private static void initAnimation() {
        Animation animation = new AlphaAnimation(0.0f, 1.0f);
        animation.setDuration(500);
        showAction = new AnimationSet(false);
        showAction.addAnimation(animation);

        animation = new AlphaAnimation(1.0f, 0.2f);
        animation.setDuration(2000);
        hideAction = new AnimationSet(false);
        hideAction.addAnimation(animation);
    }
}

Related Tutorials