I want to use ObjectAnimator to move a textView in the Y axis.This code works but it is not suitable for every device because of the screen sizes.
ObjectAnimator mover = ObjectAnimator.ofFloat(expand, "translationY", -250, 0);
How can I change the last two parameters?There are also similar questions there I tried these also but they don't work.
There are 2 cases:
You want to move your view so that it takes some defined position relatively to another view. In that case you should make your calculations based only on position of those views (see getTop()
, getLeft()
, etc.)
You want your views to move some defined physical distance of the screen (which is your case I guess), so user will percept it as movement. In such case you should use device-independent-pixels (dp
) instead of raw pixels (-250
in your case). ObjectAnimator
itself does not support such conversion, so you have to do it by yourself. The easiest way (to write, read and support) is to store such value in dimensions
resources and get them through getResources().getDimensionPixelOffest()
method