Search code examples
androidandroid-animationanimator

How do I move 4 images from center to 4 corners using ObjectAnimator?


When I click on the button, 4 images placed in the center should move from center to 4 corners.

How it is possible by using ObjectAnimator?


Solution

  • You can use the translationX and translationY property of the ObjectAnimator to achieve this.
    You need to get the new position you want to move to. (endX,endY) It's better to use functions to arrive at this rather than hardcoding, to avoid making the animation look weird on different devices.
    After that's done, Follow this...

    ObjectAnimator transX = ObjectAnimator.ofFloat(yourView,"x",startPos,endPos); 
    ObjectAnimator transY = ObjectAnimator.ofFloat(yourView,"y",startPos,endPos);  
    AnimatorSet animSetXY = new AnimatorSet();
    animSetXY.playTogether(transX, transY);
    animSetXY.start();  
    

    You can personalise your animator by using transX.setDuration(), transX.setRepeatCount(), transX.setRepeatMode() etc.,

    Hope this helps.