Problems with Objectanimator

Asked

Viewed 46 times

0

I’m doing an animation where an image slides from the bottom up using Objectanimator. In the emulator of Android Studio the animation occurs perfectly, but when I run on Android devices (tested in more than two), the animation seems to jump.

Here is the code:

imgAnim = (ImageView) findViewById(R.id.imagem);
ob = ObjectAnimator.ofFloat(imgAnim, "y", height, 70);
ob.setDuration(2100);
ob.start();
  • I reversed the editing to make everything coherent (my answer would be meaningless)

1 answer

0


Jump is due to variable value height not be the value of the position y where Imageview is located.

When starting the animation the image "jumps" from its position to the position height and only then is the gentle movement started between height and 70.

So there’s no jump, height must have the value of the position y imageview:

height = imgAnim.getY();

Note:

The method getY() will return 0 if used in the onCreate(), then the views had not yet been scaled or positioned.

If the code you posted is not in the onCreate() nor is being called from it change the line

ob = ObjectAnimator.ofFloat(imgAnim, "y", height, 70);

for

ob = ObjectAnimator.ofFloat(imgAnim, "y", imgAnim.getY(), 70);

If you are, you need to ensure that it is executed after the views have been scaled and positioned.

To do this create a method to put the code of the animation:

private void animateImage(ImageView imageView, float startY, float endY){

    ObjectAnimator ob = ObjectAnimator.ofFloat(imageView, "y", startY, endY);
    ob.setDuration(2100);
    ob.start();
}

In the method onCreate(), add an Ongloballayoutlistener to Imageview and call the method animateImage() in the method onGlobalLayout():

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ....
    ....
    imgAnim = (ImageView) findViewById(R.id.imagem);
    imgAnim.getViewTreeObserver().addOnGlobalLayoutListener(new 

        ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                //Remove o listenner para não ser novamente chamado.
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                    imgAnim.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                } else {
                    //noinspection deprecation
                    imgAnim.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                }

                //Aqui a ImageView já foi dimensionada e posicionada
                //inicia a animação
                animateImage(imgAnim, imgAnim.getY(), 70);
            }
        });
}
  • Look, the emulator works fine, but the phone continues with an initial delay.

  • If you are using the answer code I can’t find reason. However without seeing what you call delay it is difficult to know why. I’m gonna try to test it on a cell phone to see if I can replicate that behavior.

  • When the image will start to move, it goes from one point to another without sliding and only then does it begin to slide to the final position.

  • Are you sure you have the code just as it is in the answer?

  • I put my current code in the question.

  • I see nothing wrong. Ask the question, the xml of Activity

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.