ambient_animation_resizer.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // Copyright 2022 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_AMBIENT_UI_AMBIENT_ANIMATION_RESIZER_H_
  5. #define ASH_AMBIENT_UI_AMBIENT_ANIMATION_RESIZER_H_
  6. #include "ash/ash_export.h"
  7. namespace views {
  8. class AnimatedImageView;
  9. } // namespace views
  10. namespace ash {
  11. // Each Skottie animation has fixed dimensions baked into its corresponding
  12. // Lottie file that were picked by UX when the animation was designed. On the
  13. // other hand, the UI view rendering the animation can have arbitrary
  14. // dimensions. AmbientAnimationResizer modifies an AnimatedImageView such that
  15. // its underlying Skottie animation fills its bounds according to the following
  16. // UX requirements:
  17. //
  18. // Let's say the view is landscape with dimensions 1000x600 and the
  19. // corresponding Lottie animation has dimensions 2000x1500. The animation
  20. // must first be scaled down to match the UI's width. Then, the animation's
  21. // height must be center-aligned and cropped:
  22. // Width scale factor = 1000 / 2000 = 0.5
  23. // New Animation width = 2000 * 0.5 = 1000
  24. // New Animation height uncropped = 1500 * 0.5 = 750
  25. // At this point, 750 > 600, so 150 pixels must be cropped from the height.
  26. // Since the animation should be center-aligned vertically, 75 pixels will be
  27. // cropped from the top and 75 pixels will be cropped from the bottom.
  28. //
  29. // UX has intentionally designed the ambient animations such that the above
  30. // case is the most common. That is to say: the animation's dimensions are
  31. // already larger than most views' expected dimensions, and the animation's
  32. // width:height aspect ratio is intentionally smaller than most if not all of
  33. // the expected UI aspect ratios (meaning the height gets cropped).
  34. //
  35. // Corner cases:
  36. // 1) If the animation's new uncropped height is *less* than the the view's
  37. // height, keep the new height as is and vertically center-align the
  38. // animation within the view's bounds.
  39. // 2) If the UI's width is greater than the animation's width, run the exact
  40. // same logic (scale the width, then crop the height). The only difference is
  41. // the animation will be scaled "up" instead of "down".
  42. class ASH_EXPORT AmbientAnimationResizer {
  43. public:
  44. // Resizes the |animated_image_view| according to the UX requirements
  45. // described above. The |animated_image_view| must:
  46. // * Have initialized non-empty bounds.
  47. // * Have been initialized already with a lottie::Animation.
  48. //
  49. // |padding_for_jitter| specifies the additional padding in pixels that will
  50. // be applied to all 4 sides of the animation when it gets resized. For
  51. // landscape for example, the new animation width will end up being
  52. // |2 * padding_for_jitter| larger than the view's width. This allows the
  53. // animation to be translated in any direction by the padding amount while
  54. // still overlapping with the view's content bounds.
  55. static void Resize(views::AnimatedImageView& animated_image_view,
  56. int padding_for_jitter = 0);
  57. };
  58. } // namespace ash
  59. #endif // ASH_AMBIENT_UI_AMBIENT_ANIMATION_RESIZER_H_