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