android屬性動畫

ObjectAnimator

ObjectAnimator繼承於ValueAnimator,通常我們更多的去使用ObjectAnimator。ObjectAnimator中最常用的方法有ofInt(),ofFloat,ofObject()方法。它們都能返回一個ObjectAnimator對象。通過使用ObjectAnimator,短短几行代碼就能夠實現一個動畫

ofFloat()常用參數:

ofFloat(Object target, String propertyName, float... values)

這裏target可以是任何對象,propertyName是屬性名有rotation,alpha,translationX等,第三個參數是針對第二個屬性的漸變值。

public void startAnimation(){
    ObjectAnimatior animator=ObjectAnimator.ofFloat(imageView,"rotation",0f,360f);      //讓imageView沿着中心360度旋轉
    animator.setDuration(1000);   //動畫的執行時間
    animator.start();
}

下面列舉一些常用屬性:

ObjectAnimator animator=ObjectAnimator.ofFloat(imageView,"scaleY",1f,2f,1f);   //imageView沿着Y軸拉伸至倆倍再到一倍

ObjectAnimator animator=ObjectAnimator.ofFloat(imageView,"scaleX",1f,2f,1f);      ////imageView沿着X軸拉伸至倆倍再到一倍

ObjectAnimator animator=ObjectAnimator.ofFloat(imageView,"rotationX",0f,360f);      //imageView以X軸爲軸心選擇360度,rotationY同理

ObjectAnimator animator=ObjectAnimator.ofFloat(imageView,"alpha",0f,1f);     //imageView 顏色淡入由0%到100%

ObjectAnimator animator=ObjectAnimator.ofFloat(imageView,"translationX",0f,-500f,0f);   //轉變imageView 的X座標值。同理translationY。

AnimatorSet類

AnimatorSet類允許你同時編排多個動畫

  public void startAnimation(){
        ObjectAnimator animator=ObjectAnimator.ofFloat(imageView,"alpha",1f,0f,1f);
        animator.setDuration(1000);

        ObjectAnimator rotation=ObjectAnimator.ofFloat(imageView,"rotation",0f,360f);

        ObjectAnimator translationX=ObjectAnimator.ofFloat(imageView,"translationX",0f,500f,0f);

        ObjectAnimator translationY=ObjectAnimator.ofFloat(imageView,"translationY",0f,500f,0f);

        AnimatorSet animatorSet=new AnimatorSet();
        animatorSet.play(animator)
                .with(rotation)
                .with(translationX)
                .with(translationY);
        animatorSet.setDuration(5000);
        animatorSet.start();
    }

如上所示,可以同時實現X,Y軸的移動並選擇淡入淡出。

實現拋物線:
(水平方向100px/s,所以x=100*t*fraction,y=0.5/a*t*t a取200px/s

public void startAnimation(){
        animator=new ValueAnimator();
        animator.setDuration(5000);
        animator.setObjectValues(new PointF(0,0));
        animator.setInterpolator(new LinearInterpolator());
        animator.setEvaluator(new TypeEvaluator<PointF>() {
            @Override
            public PointF evaluate(float fraction, PointF startValue, PointF endValue) {
                PointF pointF=new PointF();
                pointF.x=100*fraction*3;
                pointF.y=0.5f*200*(3*fraction)*(3*fraction);
                return pointF;
            }
        });
        animator.start();
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                PointF pointF=(PointF) animator.getAnimatedValue();
                imageView.setX(pointF.x);
                imageView.setY(pointF.y);
            }
        });
    }
發佈了38 篇原創文章 · 獲贊 18 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章