Use setUpdateListener on api level below 19

For animation, I have to listen to every step of the ViewPropertyAnimator . I am using AnimatorUpdateListener

in combination with setUpdateListener

. Source: http://developer.android.com/reference/android/view/ViewPropertyAnimator.html


An example of how I use it:

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

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

      

Now im moving the object from A to B and has to detect

move some things. Now setUpdateListener really helps with this and with this code everything works. But it requires api level 19. I really want to use api level 14 for this project. Is there an alternative for setUpdateListener

?

ViewPropertyAnimator.setUpdateListener

Call requires api level 19 (current min is 14)

      

+3


source to share


3 answers


With API level 19 or higher, you can say

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

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

     });

      

With API level 11 or higher, you can resort to:



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();

      

NOTE. While ViewProperyAnimator

calling View.setHasTransientState()

under the hood for an animated presentation, ObjectAnimator

no. This can lead to behavior when custom (ie, not with ItemAnimator

) RecyclerView

animation elements are executed .

+4


source


Below is an improvement on Zsolt's answer with listener code in one place and checking the code level of the API version:



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();
}

      

+4


source


Try using 9OldAndroid lib .. it supports Honeycomb animation API (Android 3.0) on all platform versions up to 1.0!

Links https://github.com/JakeWharton/NineOldAndroids/

0


source







All Articles