1

I am currently working on an application that is compatible with API 14 and up. It's using ObjectAnimators to do a lot of the animations. The goal is to make all animations compatible to Android 2.2–2.2.3 Froyo (API level 8) and above. I have already started using nineoldandroids to convert all the objectanimator code. However there are a few functions I am not sure about what to use as an alternative since I don't believe nineoldandroid has support for them (I could be wrong).

Here is the list of current of functions that are only compatible up to API 11. Any help would be very appreciated.

setTranslationX setTranslationY setAlpha setX setY setScrollX setScrollY

ahmad
  • 2,149
  • 4
  • 21
  • 38
  • Check out [droidQuery](http://bit.ly/droidquery) for writing simple animations that are compatible with Froyo. This uses *NineOldAndroids*, and further simplifies animation syntax. – Phil Oct 20 '13 at 15:24

2 Answers2

3

In short: yes, nineoldandroids does have support for those.

If you look at the source of ObjectAnimator in nineoldandroids, you'll notice that it uses proxies to animate the properties you're looking to animate.

    PROXY_PROPERTIES.put("alpha", PreHoneycombCompat.ALPHA);
    PROXY_PROPERTIES.put("pivotX", PreHoneycombCompat.PIVOT_X);
    PROXY_PROPERTIES.put("pivotY", PreHoneycombCompat.PIVOT_Y);
    PROXY_PROPERTIES.put("translationX", PreHoneycombCompat.TRANSLATION_X);
    PROXY_PROPERTIES.put("translationY", PreHoneycombCompat.TRANSLATION_Y);
    PROXY_PROPERTIES.put("rotation", PreHoneycombCompat.ROTATION);
    PROXY_PROPERTIES.put("rotationX", PreHoneycombCompat.ROTATION_X);
    PROXY_PROPERTIES.put("rotationY", PreHoneycombCompat.ROTATION_Y);
    PROXY_PROPERTIES.put("scaleX", PreHoneycombCompat.SCALE_X);
    PROXY_PROPERTIES.put("scaleY", PreHoneycombCompat.SCALE_Y);
    PROXY_PROPERTIES.put("scrollX", PreHoneycombCompat.SCROLL_X);
    PROXY_PROPERTIES.put("scrollY", PreHoneycombCompat.SCROLL_Y);
    PROXY_PROPERTIES.put("x", PreHoneycombCompat.X);
    PROXY_PROPERTIES.put("y", PreHoneycombCompat.Y);

Use ObjectAnimator as you normally would (just make sure it's com.nineoldandroids.animation!

ObjectAnimator anim = ObjectAnimator.ofFloat(yourView, "translationX", 0f, 1f);
anim.setDuration(1000);
anim.start();

Edit: here's an example of how you can animate a view within an onTouchListener. Notice that returning false indicates the listener has not consumed the event.

view.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View view, MotionEvent event) {
        ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationX", 0f, 1f);
        anim.setDuration(1000);
        anim.start();
        return false;
      }
    });
klmprt
  • 651
  • 6
  • 7
  • Thank you so much for the answer, I have found it very useful. I have been looking through the nineoldandroids demo sample for usage examples. So far everything is good, except one thing. I am actually animating views based on onTouch() event, I can not use ObjectAnimator to set view properties for the object. Any idea on how to set the properties? – ahmad Oct 20 '13 at 00:41
  • @ahmad, of course you can still use an `ObjectAnimator` in an `onTouch` event. – Phil Oct 20 '13 at 15:21
  • I modified my answer to include an example of onTouch. Let me know if that answers your question, and please mark it as accepted it if you found it useful. – klmprt Oct 20 '13 at 19:45
  • Absolutely, thank you so much for your help. The onTouch I meant how do I setTranslateX without using object animator using touch coordinates. But I found the answer in ProxyAnimators :) Thank you again so much for your help! – ahmad Oct 21 '13 at 01:23
2

The question refers to setting immediate values for fields like X, T, translationX, etc.

You can use the helper class ViewHelper that comes with NineOldAndroids to do that.

For example replace:

myView.setX(4f);

with:

ViewHelper.setX(myView, 4f);

For all supported methods see: https://github.com/JakeWharton/NineOldAndroids/blob/master/library/src/com/nineoldandroids/view/ViewHelper.java

marmor
  • 27,641
  • 11
  • 107
  • 150