• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.internal.widget.remotecompose.core.operations.layout.animation;
17 
18 import android.annotation.NonNull;
19 
20 import com.android.internal.widget.remotecompose.core.PaintContext;
21 import com.android.internal.widget.remotecompose.core.operations.layout.Component;
22 import com.android.internal.widget.remotecompose.core.operations.layout.measure.ComponentMeasure;
23 import com.android.internal.widget.remotecompose.core.operations.paint.PaintBundle;
24 
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 
28 public class ParticleAnimation {
29     @NonNull HashMap<Integer, ArrayList<Particle>> mAllParticles = new HashMap<>();
30 
31     @NonNull PaintBundle mPaint = new PaintBundle();
32 
33     /**
34      * Animate the particle animation
35      *
36      * @param context the current paint context
37      * @param component the target component
38      * @param start the component's measure at the end of the animation
39      * @param end the component's measure at the end of the animation
40      * @param progress the current animation progress
41      */
animate( @onNull PaintContext context, @NonNull Component component, @NonNull ComponentMeasure start, @NonNull ComponentMeasure end, float progress)42     public void animate(
43             @NonNull PaintContext context,
44             @NonNull Component component,
45             @NonNull ComponentMeasure start,
46             @NonNull ComponentMeasure end,
47             float progress) {
48         ArrayList<Particle> particles = mAllParticles.get(component.getComponentId());
49         if (particles == null) {
50             particles = new ArrayList<Particle>();
51             for (int i = 0; i < 20; i++) {
52                 float x = (float) Math.random();
53                 float y = (float) Math.random();
54                 float radius = (float) Math.random();
55                 float r = 220f;
56                 float g = 220f;
57                 float b = 220f;
58                 particles.add(new Particle(x, y, radius, r, g, b));
59             }
60             mAllParticles.put(component.getComponentId(), particles);
61         }
62         context.save();
63         context.savePaint();
64         for (int i = 0; i < particles.size(); i++) {
65             Particle particle = particles.get(i);
66             mPaint.reset();
67             mPaint.setColor(
68                     particle.r / 255f,
69                     particle.g / 255f,
70                     particle.b / 255f,
71                     (200 * (1 - progress)) / 255f);
72             context.applyPaint(mPaint);
73             float dx = start.getX() + component.getWidth() * particle.x;
74             float dy =
75                     start.getY()
76                             + component.getHeight() * particle.y
77                             + progress * 0.01f * component.getHeight();
78             float dr = (component.getHeight() + 60) * 0.15f * particle.radius + (30 * progress);
79             context.drawCircle(dx, dy, dr);
80         }
81         context.restorePaint();
82         context.restore();
83     }
84 }
85