The Chromium Chronicle #33: View AnimationBuilder

การใช้ภาพเคลื่อนไหวแบบเลเยอร์ใน View จะช่วยปรับปรุงประสิทธิภาพและลดความยุ่งยาก แต่ตั้งค่าได้ยาก คลาส AnimationBuilder ช่วยลดความซับซ้อนได้อย่างมากและช่วยให้ภาพเคลื่อนไหวของเลเยอร์อ่านง่ายขึ้น

สมมติว่าคุณต้องสร้างการเคลื่อนไหวแบบกากบาทอย่างต่อเนื่องระหว่าง 2 มุมมองต่อไปนี้ เช่นในรูปภาพต่อไปนี้

ตัวอย่างการใช้ API ภาพเคลื่อนไหวเลเยอร์โดยตรงมีดังนี้

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());

ต่อไปนี้แสดงวิธีสร้างเอฟเฟกต์เดียวกันโดยใช้ AnimationBuilder ภาพเคลื่อนไหวจะเริ่มต้นเมื่อออกจากขอบเขต

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);

คุณต้องการเขียนหรืออ่านโค้ดใด ที่สำคัญไปกว่านั้น AnimationBuilder ไม่มีค่าใช้จ่ายเพิ่มเติมให้กับภาพเคลื่อนไหวเนื่องจากมีจุดประสงค์เพื่อลดความซับซ้อนในการสร้างภาพเคลื่อนไหวแบบเลเยอร์ ลองใช้วิธีนี้ในครั้งถัดไปที่คุณต้องการ สร้างภาพเคลื่อนไหว

หากต้องการความช่วยเหลือเพิ่มเติม โปรดส่งอีเมลไปที่ chromium-dev@chromium.org