当前位置: 代码迷 >> Android >> Android View动画片
  详细解决方案

Android View动画片

热度:82   发布时间:2016-04-28 00:15:46.0
Android View动画

0. 目录

    • 目录
    • 一种View背景颜色变换动画
    • 一种位移变化动画
    • 一种位移变化动画
    • 一种缩放和位移动画
    • 一种缩放和位移动画
    • 一种缩放位移和透明度动画


1. 一种View背景颜色变换动画

    private void startBackgroundTranslateAnim(int curColor, int transColor, final View view){        ValueAnimator backgroundColor = ValueAnimator.ofObject(new ArgbEvaluator(),                curColor, transColor);        backgroundColor.addListener(new AnimatorListenerAdapter() {            @Override            public void onAnimationStart(Animator animation) {            }            @Override            public void onAnimationEnd(Animator animation) {            }        });        backgroundColor.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                int color = (int) animation.getAnimatedValue();                view.setBackgroundColor(color);            }        });        backgroundColor.setInterpolator(new LinearInterpolator());        backgroundColor.setDuration(500);        backgroundColor.start();    }

2. 一种位移变化动画

        view.animate().translationX(animTranslationXPx)                //.alpha(0f)                .setStartDelay(0)                .setUpdateListener(null)                .setInterpolator(new AccelerateDecelerateInterpolator())                .setDuration(1000)                .withEndAction(new Runnable() {                    @Override                    public void run() {                    }                })                .start();

3. 一种位移变化动画

        ObjectAnimator anim = ObjectAnimator.ofFloat(v,View.TRANSLATION_X, newPos);        anim.setInterpolator(new LinearInterpolator());        anim.setDuration(500);        anim.addListener(new AnimatorListenerAdapter() {            @Override            public void onAnimationEnd(Animator animation) {            }        });        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {            }        });        anim.start();

4. 一种缩放和位移动画

        final Rect fromRect = ......        final Rect toRect = ......        final float originScaleX = 1.0f;//(float)fromRect.width() / toRect.width();        final float originScaleY = 1.0f;//(float)fromRect.height() / toRect.height();        ValueAnimator trans = ValueAnimator.ofFloat(0, 1);        trans.setInterpolator(new LinearInterpolator());        trans.setDuration(600);        trans.addListener(new AnimatorListener() {            @Override            public void onAnimationStart(Animator animation) { }            @Override            public void onAnimationRepeat(Animator animation) { }            @Override            public void onAnimationEnd(Animator animation) {            }            @Override            public void onAnimationCancel(Animator animation) {            }        });        trans.addUpdateListener(new AnimatorUpdateListener() {            @Override            public void onAnimationUpdate(ValueAnimator animation) {                float percent = (Float)animation.getAnimatedValue();                float toX = (fromRect.left + percent * (toRect.left - fromRect.left));                float toY = (fromRect.top + percent * (toRect.top - fromRect.top));                float toR = (fromRect.right + percent * (toRect.right - fromRect.right));                float toB = (fromRect.bottom + percent * (toRect.bottom - fromRect.bottom));                float scaleX = (float)(toR - toX) / fromRect.width();                float scaleY = (float)(toB - toY) / fromRect.height();                view.setTranslationX(toX-view.getLeft());                view.setTranslationY(toY-view.getTop());                view.setScaleX(scaleX*originScaleX);                view.setScaleY(scaleY*originScaleY);                float alpha = 0 + percent * 1 / (0.8f - 0);        ......            }        });        trans.start();

5. 一种缩放和位移动画

        ObjectAnimator trans = ObjectAnimator.ofFloat(view,View.TRANSLATION_X, newPos);        ObjectAnimator scalex = ObjectAnimator.ofFloat(view, View.SCALE_X, 1.0f, 0.8f);        ObjectAnimator scaley = ObjectAnimator.ofFloat(view, View.SCALE_Y, 1.0f, 0.8f);        AnimatorSet animSet = new AnimatorSet();        animSet.playTogether(scalex, scaley, trans);        animSet.setDuration(duration);        animSet.setInterpolator(new LinearInterpolator());        animSet.addListener(new AnimatorListener() {            @Override            public void onAnimationCancel(Animator animation) {            }            @Override            public void onAnimationEnd(Animator animation) {                // do end;            }            @Override            public void onAnimationRepeat(Animator animation) {            }            @Override            public void onAnimationStart(Animator animation) {            }        });        animSet.start();

6. 一种缩放、位移和透明度动画

        view.animate().translationX(animTranslationXPx)                .alpha(0.5f)                .setStartDelay(0)                .scaleX(0.8f)                .scaleY(0.8f)                .setUpdateListener(null)                .setInterpolator(new AccelerateDecelerateInterpolator())                .setDuration(1000)                .withEndAction(new Runnable() {                    @Override                    public void run() {                    }                })                .start();

版权声明:本文为博主原创文章,未经博主允许不得转载。

  相关解决方案