animation Open View - Android User Interface

Android examples for User Interface:View Hide Show

Description

animation Open View

Demo Code


//package com.java2s;

import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;

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

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

    public static void animationOpen(View srcView, View desView,
            int stateBarHeight) {
        if (android.os.Build.VERSION.SDK_INT < 11) {
            desView.setVisibility(View.VISIBLE);
            return;
        }/*from  w  w  w  .  j a va 2 s .  com*/
        int[] xy = new int[2];
        srcView.getLocationOnScreen(xy);
        xy[1] -= stateBarHeight;
        positionAndSizeAsIcon(srcView, desView);
        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",
                1f);
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat(
                "scaleX", 1.0f);
        PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat(
                "scaleY", 1.0f);
        final ObjectAnimator anim = ofPropertyValuesHolder(desView, alpha,
                scaleX, scaleY);

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

    private static void positionAndSizeAsIcon(View srcView, View desView) {
        desView.setScaleX(srcView.getWidth() / (float) desView.getWidth());
        desView.setScaleY(srcView.getHeight() / (float) desView.getHeight());
        desView.setAlpha(0.2f);
        desView.setVisibility(View.VISIBLE);
    }

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

Related Tutorials