ahmad
ahmad

Reputation: 2209

Android animations compatibility

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

Upvotes: 1

Views: 2259

Answers (2)

marmor
marmor

Reputation: 28169

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

Upvotes: 2

klmprt
klmprt

Reputation: 661

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;
      }
    });

Upvotes: 3

Related Questions