android 动画

// 淡入
            AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);
            animation.setDuration(1000);
            animation.setStartOffset(300);
            button1.setVisibility(View.VISIBLE);
            button1.startAnimation(animation);

 

// 淡出
                AlphaAnimation animation = new AlphaAnimation(1.0f, 0.0f);
                animation.setDuration(1000);
                animation.setStartOffset(300);
                // 为Aniamtion对象设置监听器

                animation.setAnimationListener(new Animation.AnimationListener() {

                    public void onAnimationEnd(Animation arg0) {

                        button1.setVisibility(View.INVISIBLE);

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onAnimationStart(Animation animation) {
                        // TODO Auto-generated method stub

                    }
                });

                button1.startAnimation(animation);

            }

 

 

// 图标的动画(入动画)
    public static void startAnimationsIn(ViewGroup viewgroup, int durationMillis) {
        for (int i = 0; i < viewgroup.getChildCount(); i++) {
            ImageButton inoutimagebutton = (ImageButton) viewgroup.getChildAt(i);
            inoutimagebutton.setVisibility(0);
            inoutimagebutton.setClickable(true);
            inoutimagebutton.setFocusable(true);
            MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton.getLayoutParams();
            Animation animation = new TranslateAnimation(xOffset - mlp.leftMargin, 0F, yOffset + mlp.bottomMargin, 0F);

            animation.setFillAfter(true);
            animation.setDuration(durationMillis);
            animation.setStartOffset((i * 100) / (-1 + viewgroup.getChildCount()));// 下一个动画的偏移时间
            animation.setInterpolator(new OvershootInterpolator(2F));// 动画的效果
                                                                        // 弹出再回来的效果
            inoutimagebutton.startAnimation(animation);

        }
    }

    // 图标的动画(出动画)
    public static void startAnimationsOut(ViewGroup viewgroup, int durationMillis) {
        for (int i = 0; i < viewgroup.getChildCount(); i++) {
            final ImageButton inoutimagebutton = (ImageButton) viewgroup.getChildAt(i);
            MarginLayoutParams mlp = (MarginLayoutParams) inoutimagebutton.getLayoutParams();
            Animation animation = new TranslateAnimation(0F, xOffset - mlp.leftMargin, 0F, yOffset + mlp.bottomMargin);

            animation.setFillAfter(true);
            animation.setDuration(durationMillis);
            animation.setStartOffset(((viewgroup.getChildCount() - i) * 100) / (-1 + viewgroup.getChildCount()));// 下一个动画的偏移时间
            animation.setAnimationListener(new Animation.AnimationListener() {
                public void onAnimationStart(Animation arg0) {
                }

                public void onAnimationRepeat(Animation arg0) {
                }

                public void onAnimationEnd(Animation arg0) {
                    inoutimagebutton.setVisibility(8);
                    inoutimagebutton.setClickable(false);
                    inoutimagebutton.setFocusable(false);
                }
            });
            inoutimagebutton.startAnimation(animation);
        }
    }

    // icon缩小消失的动画
    public static Animation getMiniAnimation(int durationMillis) {
        Animation miniAnimation = new ScaleAnimation(1.0f, 0f, 1.0f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        miniAnimation.setDuration(durationMillis);
        miniAnimation.setFillAfter(true);
        return miniAnimation;
    }

    // icon放大渐变消失的动画
    public static Animation getMaxAnimation(int durationMillis) {
        AnimationSet animationset = new AnimationSet(true);

        Animation maxAnimation = new ScaleAnimation(1.0f, 4.0f, 1.0f, 4.0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        Animation alphaAnimation = new AlphaAnimation(1, 0);

        animationset.addAnimation(maxAnimation);
        animationset.addAnimation(alphaAnimation);

        animationset.setDuration(durationMillis);
        animationset.setFillAfter(true);
        return animationset;
    }

posted @ 2013-01-06 17:03  rui90102  阅读(333)  评论(0编辑  收藏  举报