15

I'm using the wonderful framework NineOldAndroids, but I can't find anything to set the pivot on my animated views. Specifically, I'm trying to do a scaleX + scaleY animation with a pivot on the top-left edge, so pivotX = 0 and pivotY = 0. On Honeycomb and beyond I would just set myView.setPivotX(0) and myView.setPivotY(0), but how to do it for pre-Honeycomb devices with NineOldAndroids? I tried the following:

AnimatorSet set = new AnimatorSet();
set.playTogether(
    ObjectAnimator.ofFloat(myView, "pivotX", -(myView.getWidth() / 2), -(myView.getWidth() / 2)),
    ObjectAnimator.ofFloat(myView, "pivotY", -(myView.getHeight() / 2), -(myView.getHeight() / 2)),
    ObjectAnimator.ofFloat(myView, "scaleX", 0, 1),
    ObjectAnimator.ofFloat(myView, "scaleY", 0, 1),
    ObjectAnimator.ofFloat(myView, "alpha", 0, 1)
);
set.setDuration(1000).start();

but it doesn't work, the pivot stays at the center of the view.

Can you help me please?

Thanks ;)

Venator85
  • 10,245
  • 7
  • 42
  • 57
  • I do not think that NineOldAndroids supports this :-( On Honeycomb or greater (API 11) you can call setPivotY(0) but this method is not available in View of < API 11. Sorry. – Tom Apr 17 '12 at 23:02
  • Yeah I know. I examined the source code of NineOldAndroids and I found some references to a pivot for the pre-Honeycomb implementation but I had no luck with my attempts in setting it... :( – Venator85 Apr 18 '12 at 18:53

2 Answers2

18

Thanks to the author Jake Wharton, here is the solution:

AnimatorProxy.wrap(myView).setPivotX(0);

at any time, even after having called ObjectAnimator.start().

Venator85
  • 10,245
  • 7
  • 42
  • 57
  • Thanks! I did not know this could be done. That is very useful because without it many animations could not be completed on older devices. – Tom Apr 19 '12 at 02:45
  • 2
    +1 for giving the author some cred. You might even go the extra mile and link his name to his site. Backlinks are tasty SEO. – Jacksonkr Sep 27 '12 at 16:24
  • Well, for me it worked ONLY AFTER calling `start()`. Maybe because I used AnimatorSet – Michał Klimczak Feb 28 '13 at 19:08
  • how to set pivot on left side ? so if its scaling it wont change position – ralphgabb Aug 06 '15 at 06:28
15

AnimatorProxy.wrap(...) didn't have any effect in my case. However, the following line did the trick:

ViewHelper.setPivotY(myView, 0)
Jaykob
  • 323
  • 2
  • 10
  • AnimatorProxy.wrap did not work for me as well , I am using AnimatorSet . Thumbs up for this answer coz it finally helped me out – Rahul Verma Sep 24 '13 at 21:03