1

I am trying to use objectAnimator on android 2.1+. Synce the official ObjectAnimator was introduce from android 3.0, I have to use the library NineOldAndroids. But the translate animation just works on android 3.0 +. On android <3.0, the view's position does not change after animation. Please help me to solve this. What I am trying to do is slide the view horizontally.

UPDATE: added the source code. I have tested this source on android >3.0 and it works fine.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    LayoutParams layoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
    layoutMain = new RelativeLayout(getApplicationContext());
    layoutMain.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    layoutLeftMenu = new LinearLayout(getApplicationContext());
    layoutLeftMenu.setLayoutParams(layoutParams);
    layoutLeftMenu.setId(101);
    layoutMain.addView(layoutLeftMenu);


    layoutContent = new RelativeLayout(getApplicationContext());
    layoutContent.setLayoutParams(layoutParams);
    layoutMain.addView(layoutContent);

//Add the button and click event handler here
//...
}



 //called to slide the content view
    private void showMenu(final boolean slideToLeft) {
                    float slidePercent = 0.8f;
            System.out.println("screen width1: " + layoutContent.getWidth() );
            ObjectAnimator animator;
            if (!slideToLeft) {
                animator = ObjectAnimator.ofFloat(layoutContent, "translationX", slidePercent * layoutContent.getWidth());
            }
            else {
                animator = ObjectAnimator.ofFloat(layoutContent, "translationX", 0);
            }
            animator.setDuration(300);
            animator.start();

    }
Nguyen Minh Binh
  • 23,891
  • 30
  • 115
  • 165
  • Can you add some example code? I am using that library and it works for me. – Heinrisch Dec 20 '12 at 15:56
  • Agreed, I have no problem with translations with NineOldAndroids. Here is one example: http://stackoverflow.com/a/12318422/115145 – CommonsWare Dec 20 '12 at 15:57
  • I have update the source code, please take a look and tell me if I have any mistake. Thanks – Nguyen Minh Binh Dec 20 '12 at 16:07
  • Did you test NineOldAndroids translation on android 2.x? – Nguyen Minh Binh Dec 20 '12 at 16:09
  • I tested my three-pane animation on a Kindle Fire (2.3) and it worked fine. My particular scenario was focused on tablets, and so I did not experiment with it on phones. However, since there are a *lot* of people using NineOldAndroids, I would be rather surprised if there were a universal problem with translate animations. – CommonsWare Dec 20 '12 at 16:34
  • This issue was opened at NineOldAndroid's githup source (https://github.com/JakeWharton/NineOldAndroids/issues/31). JakeWharton said that this bug can't be fixed. – Nguyen Minh Binh Dec 21 '12 at 02:35

1 Answers1

0

Your problem might be that the animation is not persisted. You can use something like this after the animation to make it stay in the new position:

AnimatorProxy.wrap(view).setTranslationX(offset); 
Heinrisch
  • 5,835
  • 4
  • 33
  • 43