我在使用 ViewAnimator 的 Android 动画方面遇到了一些问题。
这是我的代码(是的,我知道我应该用 XML 自己制作动画,但这不是问题):
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final ViewAnimator animator = (ViewAnimator)getActivity().findViewById(R.id.animator);
Button next = (Button)getActivity().findViewById(R.id.buttonNext);
Button back = (Button)getActivity().findViewById(R.id.buttonBack);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animator.setInAnimation(inFromBottomAnimation());
animator.setOutAnimation(outToTopAnimation());
animator.showNext();
}
});
back.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
animator.setInAnimation(inFromTopAnimation());
animator.setOutAnimation(outToBottomAnimation());
animator.showPrevious();
}
});
}
private Animation inFromBottomAnimation() {
Animation inFromBottom = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, +1.0f, Animation.RELATIVE_TO_PARENT, 0.0f
);
inFromBottom.setDuration(1000);
inFromBottom.setInterpolator(new AccelerateInterpolator());
return inFromBottom;
}
private Animation outToTopAnimation() {
Animation outToTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, -1.0f
);
outToTop.setDuration(1000);
outToTop.setInterpolator(new AccelerateInterpolator());
return outToTop;
}
private Animation inFromTopAnimation() {
Animation inFromTop = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -1.0f, Animation.RELATIVE_TO_PARENT, 0.0f
);
inFromTop.setDuration(1000);
inFromTop.setInterpolator(new AccelerateInterpolator());
return inFromTop;
}
private Animation outToBottomAnimation() {
Animation outToBottom = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f, Animation.RELATIVE_TO_PARENT, +1.0f
);
outToBottom.setDuration(1000);
outToBottom.setInterpolator(new AccelerateInterpolator());
return outToBottom;
}
首先,动画延迟。在动画开始之前我必须等待半秒钟。其次,在 ViewAnimator 中运行的第一个动画要么根本不运行(只是跳到下一个视图 - 尽管因为第一个问题而等待半秒后),要么它运行得太快而无法注意到。
这些问题是否有任何解决方法/修复?