在api级别低于19的情况下使用setUpdateListener

Dio*_*ijn 5 animation android

对于动画,我必须听取ViewPropertyAnimator的每一步.我用它AnimatorUpdateListener结合了setUpdateListener.
来源:http://developer.android.com/reference/android/view/ViewPropertyAnimator.html


我如何使用它的示例:

image.animate().translationY(transY).setDuration(duration).setUpdateListener(new AnimatorUpdateListener() {

       @Override
       public void onAnimationUpdate(ValueAnimator animation) {
           // do my things
       }
});
Run Code Online (Sandbox Code Playgroud)

现在我将一个物体从A移动到B,并detect在移动时需要做一些事情.现在setUpdateListener对此非常有帮助,并且使用此代码它都可以正常工作.但它需要api级别19.我真的想在这个项目中使用api level 14.有替代品setUpdateListener吗?

ViewPropertyAnimator.setUpdateListener

Call requires api level 19 (current min is 14)
Run Code Online (Sandbox Code Playgroud)

Joh*_*ngs 7

以下是Zsolt在一个地方使用侦听器代码的答案的改进以及API版本的代码级别检查:

ValueAnimator.AnimatorUpdateListener updateListener = new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        // do my things
    }     
};

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
    image.animate()
          .translationY(transY)
          .setDuration(duration)
          .setUpdateListener(updateListener);
} else {

    ObjectAnimator oa = ObjectAnimator.ofFloat(image, View.TRANSLATION_Y, transY)
                                  .setDuration(duration);
    oa.addUpdateListener(updateListener);
    oa.start();
}
Run Code Online (Sandbox Code Playgroud)


Zso*_*any 6

随着API级别19或以上,你可以说

image.animate()
     .translationY(transY)
     .setDuration(duration)
     .setUpdateListener(new AnimatorUpdateListener() {

         @Override
         public void onAnimationUpdate(ValueAnimator animation) {
             // do my things
         }

     });
Run Code Online (Sandbox Code Playgroud)

随着API级别11或以上,你可以求助于:

ObjectAnimator oa = ObjectAnimator.ofFloat(image, View.TRANSLATION_Y, transY)
                                  .setDuration(duration);
oa.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        // do my things
    }
});
oa.start();
Run Code Online (Sandbox Code Playgroud)

注意:虽然动画视图引擎盖下的ViewProperyAnimator调用View.setHasTransientState(),ObjectAnimator但没有.在执行自定义(即不使用ItemAnimator)RecyclerView项目动画时,这可能导致不同的行为.