Search code examples
androidresolutionobjectanimator

Why are my animations different compared to phone/tablet (density) and how to prevent it?


When using an ObjectAnimator to move an item I get different results due to the density difference between my devices. How would I go about making these independent from density?

ObjectAnimator anim1 = ObjectAnimator.ofFloat(clockLayout, "translationY", 0f, -80f);
                anim1.setDuration(300);

Solution

  • Best way I found is to add a dimension using dpi:

    so in your values dimens.xml file:

    <dimen name="delta_y">80dp</dimen>
    

    and then you can turn this into a float in your activity with:

    float deltaY = getResources().getDimensionPixelSize(R.dimen.delta_y);
    
    ObjectAnimator.ofFloat(clockLayout, "translationY", 0f, -deltaY);