【发布时间】:2013-12-11 00:13:11
【问题描述】:
【问题讨论】:
-
似乎这个方法使用的结果可以提供一个想法grepcode.com/search/…
标签: android documentation
【问题讨论】:
标签: android documentation
onCreateAnimator 方法很奇怪。
我看到的原型是这样的:
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)
int transit - 过渡类型,正如上面sandrstar所说的
boolean enter - 如果“进入”则为真,否则为假
int nextAnim - 即将播放动画的资源ID。
因此,例如,如果您尝试翻转卡片,from the documentation:
// Create and commit a new fragment transaction that adds the fragment for the back of
// the card, uses custom animations, and is part of the fragment manager's back stack.
BackOfCardFragment backFragment = new BackOfCardFragment();
getFragmentManager()
.beginTransaction()
// Replace the default fragment animations with animator resources representing
// rotations when switching to the back of the card, as well as animator
// resources representing rotations when flipping back to the front (e.g. when
// the system Back button is pressed).
.setCustomAnimations(
R.animator.card_flip_right_in, R.animator.card_flip_right_out,
R.animator.card_flip_left_in, R.animator.card_flip_left_out)
// Replace any fragments currently in the container view with a fragment
// representing the next page (indicated by the just-incremented currentPage
// variable).
.replace(R.id.container_view, backFragment)
// Add this transaction to the back stack, allowing users to press Back
// to get to the front of the card.
.addToBackStack(null)
// Commit the transaction.
.commit();
注意:上例中的 R.id.container_view 是包含您要替换的现有片段的 ViewGroup 的 ID。
上述代码执行时,会调用onCreateAnimator方法,nextAnim参数将是传入setCustomAnimations()函数的四个动画ID之一,即R.animator.card_flip_right_in、R。 animator.card_flip_right_out...等
一开始这似乎并没有立竿见影的用处,因为它没有为您提供可以附加侦听器的实际 Animator 对象的引用。但奇怪的是,您可以直接从 nextAnim 资源中为另一个 Animator 充气,然后将侦听器附加到该资源上,奇怪的是,这会在正确的时间触发所有被覆盖的回调:
@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
Animator animator = null;
// In this example, i want to add a listener when the card_flip_right_in animation
// is about to happen.
if (nextAnim == R.animator.card_flip_right_in) {
animator = AnimatorInflater.loadAnimator(getActivity(), nextAnim);
// * Sometimes onCreateAnimator will be called and nextAnim will be 0,
// causing animator to be null.
// * I wanted to add a listener when the fragment was entering -
// your use case may be different.
if (animator != null && enter) {
animator.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
// Do something when the card flip animation begins
}
@Override
public void onAnimationEnd(Animator animation) {
// Do something as soon as the card flip animation is over
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
}
}
return animator;
}
通过这种方式,您应该能够将侦听器添加到片段过渡动画器中,就像您自己创建它们一样。
【讨论】:
基于FragmentManager 代码和FragmentManagerImpl.loadAnimator(android.app.Fragment,int,boolean,int) 的用法,Fragment.onCreateAnimator(int, boolean, int) 似乎让您可以定义自己的动画来隐藏、显示、更改状态。但是,我从未见过在实际应用中使用它。
关于参数:
int transit - 转换类型(常量FragmentTransaction,例如用于here);boolean enter - true 如果状态进入,则为 false - 否则;int transitionStyle - 来自资源的样式 ID(该样式可能包含 onCreateAnimator 遗漏的动画);【讨论】:
onCreateAnimator()方法的开头放了Log.d()语句,发现在交换片段时transit总是设置为0。