Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 8a64f10

Browse files
update particle samples
1 parent cb7d643 commit 8a64f10

File tree

2 files changed

+79
-34
lines changed

2 files changed

+79
-34
lines changed

‎Tutorial1-1Basics/src/main/java/com/smarttoolfactory/tutorial1_1basics/chapter6_graphics/ParticlePhysics.kt‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package com.smarttoolfactory.tutorial1_1basics.chapter6_graphics
33
import android.content.res.Resources
44
import androidx.compose.foundation.Canvas
55
import androidx.compose.foundation.border
6-
import androidx.compose.foundation.layout.Arrangement
76
import androidx.compose.foundation.layout.BoxWithConstraints
87
import androidx.compose.foundation.layout.Column
98
import androidx.compose.foundation.layout.Spacer

‎Tutorial1-1Basics/src/main/java/com/smarttoolfactory/tutorial1_1basics/chapter6_graphics/Tutorial6_39_1GraphicsLayer1.kt‎

Lines changed: 79 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.smarttoolfactory.tutorial1_1basics.chapter6_graphics
22

33
import android.graphics.Bitmap
4+
import android.media.Image
45
import androidx.compose.animation.core.Animatable
56
import androidx.compose.animation.core.tween
67
import androidx.compose.foundation.Canvas
@@ -169,6 +170,12 @@ data class Particle(
169170
)
170171
}
171172

173+
class ParticleController(
174+
) {
175+
var imageBitmap: ImageBitmap? = null
176+
val particles = mutableStateListOf<Particle>()
177+
}
178+
172179
@Preview
173180
@Composable
174181
fun GraphicsLayerToParticles() {
@@ -255,61 +262,99 @@ fun GraphicsLayerToParticles() {
255262
.clickable {
256263
coroutineScope.launch {
257264
animatable.snapTo(0f)
258-
val startTime = System.currentTimeMillis()
259265
animatable.animateTo(
260266
targetValue = 1f,
261-
animationSpec = tween(duration.toInt()),
267+
animationSpec = tween(
268+
durationMillis = duration.toInt(),
269+
// easing = LinearEasing
270+
),
262271
block = {
263272

264273
val progress = this.value
265274

266-
val timePassed = System.currentTimeMillis() - startTime
275+
particleList.forEachIndexed { index, particle ->
276+
277+
if (particle.active) {
278+
val posX = particle.initialCenter.x
279+
val posY = particle.initialCenter.y
267280

268281

269-
particleList.forEach { particle ->
282+
val range = progress
270283

271-
if (particle.active) {
272-
val oldCenter = particle.center
273-
val posX = oldCenter.x
274-
val posY = oldCenter.y
284+
val columnFraction =
285+
index / (particleList.size.toFloat())
286+
287+
val columnSection = columnFraction * 100
288+
289+
val animate = columnFraction < range
275290

276-
val newX = posX + 5f * progress * Random.nextFloat()
277-
val newY = posY - 15f * progress * Random.nextFloat()
291+
val sectionBasedProgress =
292+
scale(
293+
a1 = columnFraction,
294+
b1 = 1f,
295+
x1 = progress,
296+
a2 = 0f,
297+
b2 = 1f
298+
)
278299

279-
particle.center =Offset(newX, newY)
300+
if (animate) {
280301

281-
// TODO Decide whether to use time or progress
282-
val timeFraction = timePassed / duration
283302

284-
val particleDecayFactor = particle.decayFactor
303+
println(
304+
"progress: $progress, " +
305+
"sectionBasedProgress: $sectionBasedProgress, " +
306+
"columnFraction: $columnFraction, " +
307+
"columnSection: $columnSection, " +
308+
"range: $range," +
309+
" index: $index, " +
310+
"animate: $animate"
311+
)
285312

286-
val decayFactor =
287-
if (progress < .80f) particleDecayFactor
288-
else if (progress < .9f) particleDecayFactor + 1
289-
else if (progress < .98f) particleDecayFactor + 3
290-
else
291-
particleDecayFactor
292-
.coerceAtLeast(5) + 1
313+
val velocityX = -30f + (60f) * Random.nextFloat()
314+
val velocityY =
315+
-40f * Random.nextInt(100, 150) / 100f
293316

294-
if (animateSize) {
295-
val radius = particle.radius
296-
val newRadius =
297-
radius - progress * decayFactor * particle.initialRadius / 100f
298317

299-
particle.radius = newRadius.coerceAtLeast(0f)
318+
val newX =
319+
posX + velocityX * sectionBasedProgress
320+
val newY = posY + velocityY * sectionBasedProgress
321+
322+
particle.center =
323+
Offset(newX, newY)
324+
325+
val particleDecayFactor = particle.decayFactor
326+
327+
val decayFactor =
328+
if (progress < .80f) particleDecayFactor
329+
else if (progress < .85f) particleDecayFactor + 1
330+
else if (progress < .9f) particleDecayFactor + 4
331+
else
332+
particleDecayFactor
333+
.coerceAtLeast(5) + 1
334+
335+
if (animateSize) {
336+
val radius = particle.radius
337+
val newRadius =
338+
radius - progress * decayFactor * particle.initialRadius / 100f
339+
340+
particle.radius = newRadius.coerceAtLeast(0f)
300341

301342
// println(
302343
// "Time passed: $timePassed, " +
303344
// "timeFraction: $timeFraction, " +
304345
// "newRadius: $newRadius, " +
305346
// "center: ${particle.center}"
306347
// )
307-
}
348+
}
308349
//
309-
// if (animateAlpha) {
310-
// particle.alpha -= (1 - progress) * Random.nextFloat()
311-
// .coerceAtMost(.2f)
312-
// }
350+
if (animateAlpha) {
351+
particle.alpha -= (progress) * Random.nextFloat() / 20f
352+
}
353+
354+
if (progress == 1f) {
355+
particle.alpha = 0f
356+
}
357+
}
313358
}
314359
}
315360

@@ -422,10 +467,11 @@ fun createParticles(imageBitmap: ImageBitmap, particleSize: Int): List<Particle>
422467
for (posY in 0 until height step particleSize) {
423468

424469
// TODO Assign these params
425-
val scale = Random.nextInt(90, 250) / 100f
470+
val scale = Random.nextInt(95, 110) / 100f
426471
// val scale = 1f
427472
val decayFactor = Random.nextInt(10)
428-
val alpha = Random.nextFloat().coerceAtLeast(.5f)
473+
// val alpha = Random.nextFloat().coerceAtLeast(.5f)
474+
val alpha = 1f
429475

430476
// Get pixel at center of this pixel rectangle
431477
// If last pixel is out of image get it from end of the width or height

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /