这是使用FragmentStatePagerAdapter的viewpager的简单实现。 共有4个片段。 这是其中一个片段的onCreateView方法。
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.intro1, container, false);
/** initializing Views **/
initViews(view);
startAnimation();
return view;
}
所有片段都具有完全相同的onCreateView方法。 片段2的startAnimation方法就在这里。
private void startAnimations() {
ObjectAnimator rotateTasks = ObjectAnimator.ofFloat(viewTasks,"rotation",0f,360f);
ObjectAnimator alphaTasks = ObjectAnimator.ofFloat(viewTasks, "alpha",0f,1.0f);
final ObjectAnimator alphaPaint = ObjectAnimator.ofFloat(viewPaint,"alpha",0f,1.0f);
final ObjectAnimator scaleXPaint = ObjectAnimator.ofFloat(viewPaint,"scaleX",1.0f,1.2f);
final ObjectAnimator scaleYPaint = ObjectAnimator.ofFloat(viewPaint,"scaleY",1.0f,1.2f);
taskAnimator = new AnimatorSet();
taskAnimator.setInterpolator(new AccelerateInterpolator(-0.1f));
taskAnimator.setDuration(DEFAULT_ANIMATION_DURATION);
taskAnimator.play(rotateTasks)
.with(alphaTasks)
.with(alphaPaint)
.with(scaleXPaint)
.with(scaleYPaint);
taskAnimator.start();
}
现在,究竟发生了什么,一旦应用程序运行,动画就会播放除了2号片段之外的所有片段。但奇怪的是,当我从第四个片段向第三个片段再次向第二个片段滑动时,动画播放第二个片段。