Android View動畫,androidview動畫

來源:互聯網
上載者:User

Android View動畫,androidview動畫
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();

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.