我找到了一種方法來添加Animator
。
它通過在將其添加到fragmentManager之前覆蓋片段的onCreateAnimator
方法來實現。作爲一個例子,在過渡期間滑動動畫可以這樣做:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tutorial);
if (savedInstanceState == null) {
Fragment fragment = new MyFragment(){
@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)
{
Display display = getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
Animator animator = null;
if(enter){
animator =
ObjectAnimator.ofFloat(this, "translationX", (float) size.x, 0);
} else {
animator =
ObjectAnimator.ofFloat(this, "translationX", 0, (float) size.x);
}
animator.setDuration(500);
return animator;
}
}
getFragmentManager().beginTransaction()
.add(R.id.container, fragment)
.commit();
}
}
P.S.這個答案要歸功於this forum的帖子。