ObjectAnimator詳解

轉載地址: //blog.csdn.net/xiaochuanding/article/details/73290917

ObjectAnimator繼承自ValueAnimator,所以ValueAnimator所能使用的方法,ObjectAnimator都可以使用,ObjectAnimator同時也重寫了幾個方法,比如:ofInt() ofFloat()等

基本使用

//第一個參數:指定執行動畫的控件,第二個參數:指定控件的屬性,第三個參數是可變長參數
public static ObjectAnimator ofFloat(Object target, String propertyName, float... values) 
  • 1
  • 2
//透明度動畫
ObjectAnimator animator = ObjectAnimator.ofFloat(view,"alpha",1,0,1);  
animator.setDuration(2000);  
animator.start(); 

//旋轉動畫:圍繞x軸旋轉
ObjectAnimator animator = ObjectAnimator.ofFloat(tv,"rotationX",0,270,0);  
animator.setDuration(2000);  
animator.start();

//旋轉動畫:圍繞y軸旋轉
ObjectAnimator animator = ObjectAnimator.ofFloat(tv,"rotationY",0,180,0);  
animator.setDuration(2000);  
animator.start();

//旋轉動畫:圍繞z軸旋轉
ObjectAnimator animator = ObjectAnimator.ofFloat(tv,"rotation",0,270,0);  
animator.setDuration(2000);  
animator.start();  

//平移動畫:在x軸上平移
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationX", 0, 200, -200,0);  
animator.setDuration(2000);  
animator.start(); 

//平移動畫:在y軸上平移
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "translationY", 0, 200, -100,0);  
animator.setDuration(2000);  
animator.start(); 

//縮放動畫:在x軸縮放
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "scaleX", 0, 3, 1);  
animator.setDuration(2000);  
animator.start();

//縮放動畫:在y軸上縮放
ObjectAnimator animator = ObjectAnimator.ofFloat(tv, "scaleY", 0, 3, 1);  
animator.setDuration(2000);  
animator.start(); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39

ObjectAnimator動畫原理

這裏寫圖片描述 
ObjectAnimator動畫原理:如上圖最後一步,根據屬性值拼裝成對應的set函數的名字,比如”scaleY”的拼接方法就是將屬性的第一個字母強制大寫後,與set拼接,也就是setScaleY,然後通過反射找到對應控件的setScaleY(float scaleY)函數,將當前數字值作爲setScaleY(float scale)的參數將其傳入。屬性值得首字母大小寫都可以,最終都會被強轉成大寫。View中都已經實現了相關的alpha rotation translate scale相關的set方法。

自定義ObjectAnimator屬性

這裏寫圖片描述

   ObjectAnimator objectAnimator = ObjectAnimator.ofInt(mCircleView,"pointRadius",0,200,100,200,50);
        objectAnimator.setDuration(1000);
        objectAnimator.start();
  • 1
  • 2
  • 3

自定義Point

public class Ponit {
    private int mRadius;

    public Ponit() {
    }

    public Ponit(int mRadius) {
        this.mRadius = mRadius;
    }

    public int getRadius() {
        return mRadius;
    }

    public void setRadius(int mRadius) {
        this.mRadius = mRadius;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

自定義view

public class CircleView extends View {
    private Ponit mCurrentPoint = new Ponit();
    private Paint mPiant ;
    private int mScreenWidth;//屏幕寬度
    public CircleView(Context context) {
        this(context,null);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mPiant = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPiant.setColor(Color.RED);
        mPiant.setStyle(Paint.Style.FILL);
        mScreenWidth = ((WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mCurrentPoint != null){
            canvas.drawCircle(mScreenWidth/2,getY()+getPaddingTop(),mCurrentPoint.getRadius(),mPiant);
        }
    }
    public int getPointRadius(){  //這個get方法只作爲默認值出現在這裏的
        return 50;  
    }  
    public void setPointRadius(int radius){//這裏set方法必須和ObjectAnimator中的屬性值對應
        mCurrentPoint.setRadius(radius);
        invalidate();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

ObjectAnimator改變控件背景顏色

以TextView爲類,改變其背景顏色,關鍵方法是繼承View的控件都實現了這個方法

public void setBackgroundColor(int color); 
  • 1


這裏寫圖片描述

 ObjectAnimator objectAnimator = ObjectAnimator.ofInt(tv,"backgroundColor",0xfff10f0f,0xff0f94f1,0xffeaf804,0xfff92a0f);
        objectAnimator.setDuration(2000);
        objectAnimator.setEvaluator(new ArgbEvaluator());
        objectAnimator.start();
  • 1
  • 2
  • 3
  • 4

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章