Phương pháp onCreateAnimator
là lẻ. Nguyên mẫu tôi đã nhìn thấy là thế này:
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim)
int transit
- loại chuyển tiếp, như đã nói ở trên sandrstar
boolean enter
- đúng nếu 'vào,' false
int nextAnim
- Các ID tài nguyên của hoạt ảnh sắp phát.
Vì vậy, ví dụ, nếu bạn đã thử làm một lật thẻ, 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();
LƯU Ý: R.id.container_view trong ví dụ trên là ID của một ViewGroup có chứa đoạn hiện bạn đang cố gắng thay thế.
Khi đoạn mã trên được thực thi, phương pháp onCreateAnimator
sẽ được gọi, và tham số nextAnim
sẽ là một trong bốn ID hình ảnh động chuyển vào setCustomAnimations()
chức năng, tức là R.animator.card_flip_right_in, R.animator.card_flip_right_out. .. vv
Điều này dường như không hữu ích ngay lập tức lúc đầu, vì nó không cung cấp cho bạn tham chiếu đến đối tượng Animator thực tế mà bạn có thể đính kèm người nghe vào.Nực cười là, bạn chỉ có thể thổi phồng Animator khác trực tiếp từ nguồn nextAnim
, và sau đó đính kèm các thính giả đó, nhờ đó sẽ kỳ quặc, bắn tất cả các callbacks ghi đè vào đúng thời điểm:
@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;
}
Bằng cách này, bạn nên có thể thêm người nghe vào hoạt ảnh chuyển tiếp phân đoạn như thể bạn đã tự tạo chúng.
Có vẻ như kết quả này của việc sử dụng phương pháp có thể đưa ra một ý tưởng http://grepcode.com/search/usages?type=method&id=repository.grepcode.com%24java%[email protected]%[email protected] 2_r1 @ android% 24app @ Phân đoạn @ onCreateAnimator% 28int% 2Cboolean% 2Cint% 29 & k = u – sandrstar