|
| 1 | +package com.miku.ray.particlesdrawable; |
| 2 | + |
| 3 | +import android.content.res.Resources; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.graphics.Canvas; |
| 6 | +import android.graphics.ColorFilter; |
| 7 | +import android.graphics.PixelFormat; |
| 8 | +import android.graphics.drawable.Animatable; |
| 9 | +import android.graphics.drawable.Drawable; |
| 10 | +import android.os.SystemClock; |
| 11 | +import android.util.AttributeSet; |
| 12 | + |
| 13 | +import androidx.annotation.ColorInt; |
| 14 | +import androidx.annotation.FloatRange; |
| 15 | +import androidx.annotation.IntRange; |
| 16 | +import androidx.annotation.Keep; |
| 17 | +import androidx.annotation.NonNull; |
| 18 | +import androidx.annotation.Nullable; |
| 19 | + |
| 20 | +import com.miku.ray.particlesdrawable.contract.SceneConfiguration; |
| 21 | +import com.miku.ray.particlesdrawable.contract.SceneController; |
| 22 | +import com.miku.ray.particlesdrawable.contract.SceneRenderer; |
| 23 | +import com.miku.ray.particlesdrawable.contract.SceneScheduler; |
| 24 | +import com.miku.ray.particlesdrawable.engine.Engine; |
| 25 | +import com.miku.ray.particlesdrawable.engine.SceneConfigurator; |
| 26 | +import com.miku.ray.particlesdrawable.model.Scene; |
| 27 | +import com.miku.ray.particlesdrawable.renderer.CanvasSceneRenderer; |
| 28 | +import com.miku.ray.particlesdrawable.renderer.DefaultSceneRenderer; |
| 29 | +import com.miku.ray.R; |
| 30 | + |
| 31 | +import org.xmlpull.v1.XmlPullParser; |
| 32 | +import org.xmlpull.v1.XmlPullParserException; |
| 33 | + |
| 34 | +import java.io.IOException; |
| 35 | + |
| 36 | +@Keep |
| 37 | +public class ParticlesDrawable extends Drawable implements |
| 38 | + Animatable, |
| 39 | + SceneConfiguration, |
| 40 | + SceneController, |
| 41 | + SceneScheduler { |
| 42 | + |
| 43 | + |
| 44 | + private CanvasSceneRenderer canvasRenderer = new CanvasSceneRenderer(); |
| 45 | + |
| 46 | + private Scene scene = new Scene(); |
| 47 | + |
| 48 | + private SceneConfigurator sceneConfigurator = new SceneConfigurator(); |
| 49 | + |
| 50 | + private SceneRenderer renderer = new DefaultSceneRenderer(canvasRenderer); |
| 51 | + |
| 52 | + private Engine engine = new Engine(scene, this, renderer); |
| 53 | + |
| 54 | + @Override |
| 55 | + public void inflate( |
| 56 | + @NonNull final Resources r, |
| 57 | + @NonNull final XmlPullParser parser, |
| 58 | + @NonNull final AttributeSet attrs, |
| 59 | + @Nullable final Resources.Theme theme) throws XmlPullParserException, IOException { |
| 60 | + super.inflate(r, parser, attrs, theme); |
| 61 | + |
| 62 | + final TypedArray a; |
| 63 | + if (theme != null) { |
| 64 | + a = theme.obtainStyledAttributes(attrs, R.styleable.ParticlesView, 0, 0); |
| 65 | + } else { |
| 66 | + a = r.obtainAttributes(attrs, R.styleable.ParticlesView); |
| 67 | + } |
| 68 | + |
| 69 | + try { |
| 70 | + final int count = a.getIndexCount(); |
| 71 | + float particleRadiusMax = Defaults.PARTICLE_RADIUS_MAX; |
| 72 | + float particleRadiusMin = Defaults.PARTICLE_RADIUS_MIN; |
| 73 | + |
| 74 | + for (int i = 0; i < count; i++) { |
| 75 | + final int attr = a.getIndex(i); |
| 76 | + if (attr == R.styleable.ParticlesView_density) { |
| 77 | + scene.setDensity(a.getInteger(attr, Defaults.DENSITY)); |
| 78 | + } else if (attr == R.styleable.ParticlesView_frameDelayMillis) { |
| 79 | + scene.setFrameDelay(a.getInteger(attr, Defaults.FRAME_DELAY)); |
| 80 | + } else if (attr == R.styleable.ParticlesView_lineColor) { |
| 81 | + scene.setLineColor(a.getColor(attr, Defaults.LINE_COLOR)); |
| 82 | + } else if (attr == R.styleable.ParticlesView_lineLength) { |
| 83 | + scene.setLineLength(a.getDimension(attr, Defaults.LINE_LENGTH)); |
| 84 | + } else if (attr == R.styleable.ParticlesView_lineThickness) { |
| 85 | + scene.setLineThickness(a.getDimension(attr, Defaults.LINE_THICKNESS)); |
| 86 | + } else if (attr == R.styleable.ParticlesView_particleColor) { |
| 87 | + scene.setParticleColor(a.getColor(attr, Defaults.PARTICLE_COLOR)); |
| 88 | + } else if (attr == R.styleable.ParticlesView_particleRadiusMax) { |
| 89 | + particleRadiusMax = a.getDimension(attr, Defaults.PARTICLE_RADIUS_MAX); |
| 90 | + } else if (attr == R.styleable.ParticlesView_particleRadiusMin) { |
| 91 | + particleRadiusMin = a.getDimension(attr, Defaults.PARTICLE_RADIUS_MIN); |
| 92 | + } else if (attr == R.styleable.ParticlesView_speedFactor) { |
| 93 | + scene.setSpeedFactor(a.getFloat(attr, Defaults.SPEED_FACTOR)); |
| 94 | + } |
| 95 | + } |
| 96 | + scene.setParticleRadiusRange(particleRadiusMin, particleRadiusMax); |
| 97 | + } finally { |
| 98 | + a.recycle(); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public void setBounds(final int left, final int top, final int right, final int bottom) { |
| 104 | + super.setBounds(left, top, right, bottom); |
| 105 | + engine.setDimensions(right - left, bottom - top); |
| 106 | + } |
| 107 | + |
| 108 | + @Override |
| 109 | + public void draw(@NonNull final Canvas canvas) { |
| 110 | + canvasRenderer.setCanvas(canvas); |
| 111 | + engine.draw(); |
| 112 | + canvasRenderer.setCanvas(null); |
| 113 | + engine.run(); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public void scheduleNextFrame(final long delay) { |
| 118 | + if (delay == 0L) { |
| 119 | + requestRender(); |
| 120 | + } else { |
| 121 | + scheduleSelf(invalidateSelfRunnable, SystemClock.uptimeMillis() + delay); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void unscheduleNextFrame() { |
| 127 | + unscheduleSelf(invalidateSelfRunnable); |
| 128 | + } |
| 129 | + |
| 130 | + @Override |
| 131 | + public void requestRender() { |
| 132 | + invalidateSelf(); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void setAlpha(final int alpha) { |
| 137 | + engine.setAlpha(alpha); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public int getAlpha() { |
| 142 | + return engine.getAlpha(); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void setColorFilter(@Nullable final ColorFilter colorFilter) { |
| 147 | + canvasRenderer.setColorFilter(colorFilter); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public int getOpacity() { |
| 152 | + return PixelFormat.TRANSLUCENT; |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void start() { |
| 157 | + engine.start(); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void stop() { |
| 162 | + engine.stop(); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public boolean isRunning() { |
| 167 | + return engine.isRunning(); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void nextFrame() { |
| 172 | + engine.nextFrame(); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void makeFreshFrame() { |
| 177 | + engine.makeFreshFrame(); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void makeFreshFrameWithParticlesOffscreen() { |
| 182 | + engine.makeFreshFrameWithParticlesOffscreen(); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public void setFrameDelay(@IntRange(from = 0) final int delay) { |
| 187 | + scene.setFrameDelay(delay); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public int getFrameDelay() { |
| 192 | + return scene.getFrameDelay(); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public void setSpeedFactor(@FloatRange(from = 0) final float speedFactor) { |
| 197 | + scene.setSpeedFactor(speedFactor); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public float getSpeedFactor() { |
| 202 | + return scene.getSpeedFactor(); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public void setParticleRadiusRange( |
| 207 | + @FloatRange(from = 0.5f) final float minRadius, |
| 208 | + @FloatRange(from = 0.5f) final float maxRadius) { |
| 209 | + scene.setParticleRadiusRange(minRadius, maxRadius); |
| 210 | + } |
| 211 | + |
| 212 | + @Override |
| 213 | + public float getParticleRadiusMin() { |
| 214 | + return scene.getParticleRadiusMin(); |
| 215 | + } |
| 216 | + |
| 217 | + @Override |
| 218 | + public float getParticleRadiusMax() { |
| 219 | + return scene.getParticleRadiusMax(); |
| 220 | + } |
| 221 | + |
| 222 | + @Override |
| 223 | + public void setLineThickness(@FloatRange(from = 1) final float lineThickness) { |
| 224 | + scene.setLineThickness(lineThickness); |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public float getLineThickness() { |
| 229 | + return scene.getLineThickness(); |
| 230 | + } |
| 231 | + |
| 232 | + @Override |
| 233 | + public void setLineLength(@FloatRange(from = 0) final float lineLength) { |
| 234 | + scene.setLineLength(lineLength); |
| 235 | + } |
| 236 | + |
| 237 | + @Override |
| 238 | + public float getLineLength() { |
| 239 | + return scene.getLineLength(); |
| 240 | + } |
| 241 | + |
| 242 | + public void setDensity(@IntRange(from = 0) final int newNum) { |
| 243 | + scene.setDensity(newNum); |
| 244 | + } |
| 245 | + |
| 246 | + @Override |
| 247 | + public int getDensity() { |
| 248 | + return scene.getDensity(); |
| 249 | + } |
| 250 | + |
| 251 | + public void setParticleColor(@ColorInt final int color) { |
| 252 | + scene.setParticleColor(color); |
| 253 | + } |
| 254 | + |
| 255 | + @Override |
| 256 | + public int getParticleColor() { |
| 257 | + return scene.getParticleColor(); |
| 258 | + } |
| 259 | + |
| 260 | + @Override |
| 261 | + public void setLineColor(@ColorInt final int lineColor) { |
| 262 | + scene.setLineColor(lineColor); |
| 263 | + } |
| 264 | + |
| 265 | + @Override |
| 266 | + public int getLineColor() { |
| 267 | + return scene.getLineColor(); |
| 268 | + } |
| 269 | + |
| 270 | + private final Runnable invalidateSelfRunnable = new Runnable() { |
| 271 | + @Override |
| 272 | + public void run() { |
| 273 | + invalidateSelf(); |
| 274 | + } |
| 275 | + }; |
| 276 | +} |
0 commit comments