Chromium Chronicle #33: Views AnimationBuilder

Việc sử dụng ảnh động dựa trên lớp trong Khung hiển thị có thể cải thiện hiệu suất và giảm hiện tượng giật, nhưng sẽ khá khó thiết lập. Các lớp AnimationBuilder có thể làm giảm đáng kể độ phức tạp và cải thiện khả năng đọc cho ảnh động theo lớp.

Giả sử bạn cần tạo ảnh động mờ dần liên tục giữa hai khung hiển thị sau, chẳng hạn như trong hình sau.

Dưới đây là ví dụ về cách trực tiếp thực hiện việc này bằng cách sử dụng các API ảnh động lớp.

auto primary_title_sequence = std::make_unique<LayerAnimationSequence>();
auto working_sequence = std::make_unique<LayerAnimationSequence>();
primary_title_sequence->set_is_repeating(true);
working_sequence->set_is_repeating(true);

primary_title_sequence->AddElement(CreatePauseElement(OPACITY, base::Seconds(2)));
primary_title_sequence->AddElement(CreateOpacityElement(0.0f, base::Seconds(1)));
primary_title_sequence->AddElement(CreatePauseElement(OPACITY, base::Seconds(2)));
primary_title_sequence->AddElement(CreateOpacityElement(1.0f, base::Seconds(1)));

working_sequence->AddElement(CreatePauseElement(OPACITY, base::Seconds(2)));
working_sequence->AddElement(CreateOpacityElement(1.0f, base::Seconds(1)));
working_sequence->AddElement(CreatePauseElement(OPACITY, base::Seconds(2)));
working_sequence->AddElement(CreateOpacityElement(0.0f, base::Seconds(1)));

primary_title_->layer()->GetAnimator()->StartAnimation(primary_title_sequence.release());
working_->layer()->GetAnimator()->StartAnimation(working_sequence.release());

Phần sau đây cho biết cách tạo cùng một hiệu ứng bằng AnimationBuilder. Ảnh động sẽ bắt đầu khi bạn thoát khỏi phạm vi.

AnimationBuilder()
    .Repeatedly()
    .Offset(base::Seconds(2))
    .SetDuration(base::Seconds(1))
    .SetOpacity(primary_title_, 0.0f)
    .SetOpacity(working_, 1.0f)
    .Offset(base::Seconds(2))
    .SetDuration(base::Seconds(1))
    .SetOpacity(primary_title_, 1.0f)
    .SetOpacity(working_, 0.0f);

Bạn muốn viết hoặc đọc mã nào? Quan trọng hơn, AnimationBuilder không thêm mức hao tổn bổ sung vào ảnh động vì mục đích của thao tác này là đơn giản hoá việc tạo ảnh động dựa trên lớp. Hãy thử tính năng này vào lần tiếp theo bạn cần tạo ảnh động nào đó.

Để được trợ giúp thêm, hãy gửi email đến chromium-dev@chromium.org.