我正在尝试在 android 2.1+ 上使用 objectAnimator。同步官方 ObjectAnimator 是从 android 3.0 引入的,我必须使用库NineOldAndroids。但翻译动画只适用于 android 3.0 +。在 android <3.0 上,视图的位置在动画后不会改变。请帮我解决这个问题。我想要做的是水平滑动视图。
更新:添加了源代码。我已经在 android >3.0 上测试了这个源,它工作正常。
@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();
}