animation Close View - Android User Interface

Android examples for User Interface:View Hide Show

Description

animation Close View

Demo Code


//package com.java2s;
import android.animation.Animator;

import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;

import android.view.View;
import android.view.animation.DecelerateInterpolator;

public class Main {
    private static final int DURATION = 250;

    public static void animationClose(View srcView, final View desView,
            int stateBarHeight) {
        if (android.os.Build.VERSION.SDK_INT < 11) {
            desView.setVisibility(View.GONE);
            return;
        }/*from ww w.ja va2  s.c  o  m*/
        int[] xy = new int[2];
        srcView.getLocationOnScreen(xy);
        xy[1] -= stateBarHeight;
        if ((desView.getWidth() - srcView.getWidth()) + xy[0] == 0
                || (desView.getHeight() - srcView.getHeight()) + xy[1] == 0)
            return;
        float x = srcView.getWidth() * xy[0]
                / (desView.getWidth() - srcView.getWidth()) + xy[0];
        float y = srcView.getHeight() * xy[1]
                / (desView.getHeight() - srcView.getHeight()) + xy[1];
        desView.setPivotX(x);
        desView.setPivotY(y);
        PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha",
                0.2f);
        final float fScaleX = srcView.getWidth()
                / (float) desView.getWidth();
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat(
                "scaleX", fScaleX);
        PropertyValuesHolder scaleY = PropertyValuesHolder
                .ofFloat("scaleY",
                        srcView.getHeight() / (float) desView.getHeight());
        final ObjectAnimator anim = ofPropertyValuesHolder(desView, alpha,
                scaleX, scaleY);
        anim.addUpdateListener(new AnimatorUpdateListener() {

            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float scale = (Float) animation.getAnimatedValue("scaleX");
                if (fScaleX == scale) {
                    desView.setVisibility(View.GONE);
                }
            }
        });

        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                desView.setVisibility(View.GONE);
            }
        });

        anim.setInterpolator(new DecelerateInterpolator());
        anim.setDuration(DURATION);
        anim.start();
    }

    public static ObjectAnimator ofPropertyValuesHolder(Object target,
            PropertyValuesHolder... values) {
        ObjectAnimator anim = new ObjectAnimator();
        anim.setTarget(target);
        anim.setValues(values);
        return anim;
    }
}

Related Tutorials