Android屬性動畫-簡單實例

1.ValueAnimator

//在2000毫秒內,將值從0過渡到1的動畫
        ValueAnimator anim = ValueAnimator.ofFloat(0f, 1f);
        anim.setDuration(2000);
        anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                float currentValue = (float) valueAnimator.getAnimatedValue();
                Log.e("tag", "currentValue="+currentValue);
                String textStr = ((int) (currentValue * 60)) + "";
                text1_tv.setText(textStr);
            }
        });
        anim.start();

2.ObjectAnimator

//將TextView從常規變換成全透明,再從全透明變換成常規
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(text2_tv, "alpha", 1f, 0f, 1f);
        anim1.setDuration(3000);
        anim1.start();
        //將TextView進行一次360度的旋轉
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(text2_tv, "rotation", 0f, 360f);
        anim2.setDuration(3000);
        anim2.start();
        //將TextView先向左移出屏幕,而後再移動回來
        float curTranslationX = text2_tv.getTranslationX();
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(text2_tv, "translationX", curTranslationX, -500f, curTranslationX);
        anim3.setDuration(3000);
        anim3.start();
        //將TextView在垂直方向上放大3倍再還原
        ObjectAnimator anim4 = ObjectAnimator.ofFloat(text2_tv, "scaleY", 1f, 3f, 1f);
        anim4.setDuration(5000);
        anim4.start();

3.組合動畫

//讓TextView先從屏幕外移動進屏幕,而後開始旋轉360度,旋轉的同時進行淡入淡出操做
        ObjectAnimator moveIn = ObjectAnimator.ofFloat(text3_tv, "translationX", -500f, 0f);
        ObjectAnimator rotate = ObjectAnimator.ofFloat(text3_tv, "rotation", 0f, 360f);
        ObjectAnimator fadeInOut = ObjectAnimator.ofFloat(text3_tv, "alpha", 1f, 0f, 1f);
        AnimatorSet animSet = new AnimatorSet();
        animSet.play(rotate).with(fadeInOut).after(moveIn);
        animSet.setDuration(5000);
        animSet.start();

 

相關文章
相關標籤/搜索