ObjectAnimator

ObjectAnimatorValueAnimator 的子類,具有將計算值設定為 target View 屬性的附加功能。

就像在 ValueAnimator 中一樣,有兩種方法可以建立 ObjectAnimator

(示例程式碼在 250ms 中將 Viewalpha0.4f 啟用到 0.2f

  1. 來自 xml(把它放在/res/animator
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
                android:duration="250"
                android:propertyName="alpha"
                android:valueFrom="0.4"
                android:valueTo="0.2"
                android:valueType="floatType"/>
ObjectAnimator animator = (ObjectAnimator) AnimatorInflater.loadAnimator(context,
        R.animator.example_animator);
animator.setTarget(exampleView);
// set all the animation-related stuff you want (interpolator etc.)
animator.start();
  1. 來自程式碼:
ObjectAnimator animator = ObjectAnimator.ofFloat(exampleView, View.ALPHA, 0.4f, 0.2f);
animator.setDuration(250);
// set all the animation-related stuff you want (interpolator etc.)
animator.start();