安卓(Android) 怎么创建关闭动画(Animation)

2019年10月16日23:19:10安卓(Android) 怎么创建关闭动画(Animation)已关闭评论 688 2347字阅读7分49秒

1.简介

安卓(Android) 怎么创建关闭动画(Animation)?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */


import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnticipateInterpolator;

import android.view.animation.RotateAnimation;

import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RelativeLayout;

public class Main {

    public static void closeAnimation(final RelativeLayout relativeLayout,
            final ImageView menu, long durationMillis) {
        for (int i = 1; i < relativeLayout.getChildCount(); i++) {
            ImageView imageView = null;
            if (relativeLayout.getChildAt(i) instanceof ImageView) {
                imageView = (ImageView) relativeLayout.getChildAt(i);
            } else {
                continue;
            }

            AnimationSet set = new AnimationSet(true);
            set.addAnimation(getRotateAnimation(0, -360, durationMillis));
            set.addAnimation(getAlphaAnimation(1.0f, 0.5f, durationMillis));
            set.addAnimation(getTranslateAnimation(0, menu.getLeft()
                    - imageView.getLeft(), 0,
                    menu.getTop() - imageView.getTop() + 30, durationMillis));
            set.setFillAfter(true);
            set.setDuration(durationMillis);
            set.setStartOffset(((relativeLayout.getChildCount() - i) * 100)
                    / (-1 + relativeLayout.getChildCount()));
            set.setInterpolator(new AnticipateInterpolator(1f));
            set.setAnimationListener(new Animation.AnimationListener() {
                @Override
                public void onAnimationStart(Animation arg0) {
                }

                @Override
                public void onAnimationRepeat(Animation arg0) {
                }

                @Override
                public void onAnimationEnd(Animation arg0) {
                    relativeLayout.setVisibility(View.GONE);
                }
            });
            imageView.startAnimation(set);
        }
    }

    public static Animation getRotateAnimation(float fromDegrees,
            float toDegrees, long durationMillis) {
        RotateAnimation rotate = new RotateAnimation(fromDegrees,
                toDegrees, Animation.RELATIVE_TO_SELF, 0.5f,
                Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setDuration(durationMillis);
        rotate.setFillAfter(true);
        return rotate;
    }

    public static Animation getAlphaAnimation(float fromAlpha,
            float toAlpha, long durationMillis) {
        AlphaAnimation alpha = new AlphaAnimation(fromAlpha, toAlpha);
        alpha.setDuration(durationMillis);
        alpha.setFillAfter(true);
        return alpha;
    }

    public static Animation getTranslateAnimation(float fromXDelta,
            float toXDelta, float fromYDelta, float toYDelta,
            long durationMillis) {
        TranslateAnimation translate = new TranslateAnimation(fromXDelta,
                toXDelta, fromYDelta, toYDelta);
        translate.setDuration(durationMillis);
        translate.setFillAfter(true);
        return translate;
    }
}

 
继续阅读
编程十万个怎么办