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);
}
});
}
I reversed the editing to make everything coherent (my answer would be meaningless)
– ramaral