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 6da89c9

Browse files
add particle boundaries for customization
1 parent d2f5cd7 commit 6da89c9

File tree

1 file changed

+55
-29
lines changed
  • Tutorial1-1Basics/src/main/java/com/smarttoolfactory/tutorial1_1basics/chapter9_animation

1 file changed

+55
-29
lines changed

‎Tutorial1-1Basics/src/main/java/com/smarttoolfactory/tutorial1_1basics/chapter9_animation/ParticleAnimations.kt

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

33
import android.graphics.Bitmap
4+
import android.util.Log
45
import android.widget.Toast
56
import androidx.compose.animation.core.Animatable
67
import androidx.compose.animation.core.FastOutSlowInEasing
@@ -234,13 +235,12 @@ fun ParticleAnimationSample() {
234235
particleState.startAnimation()
235236
}
236237
.disintegrate(
237-
progress = progress,
238+
// progress = progress,
238239
particleState = particleState,
239240
onStart = {
240241
Toast.makeText(context, "Animation started...", Toast.LENGTH_SHORT).show()
241242
},
242243
onEnd = {
243-
// particleState.animationStatus = AnimationStatus.Idle
244244
Toast.makeText(context, "Animation ended...", Toast.LENGTH_SHORT).show()
245245
}
246246
),
@@ -258,6 +258,7 @@ fun ParticleAnimationSample() {
258258
.border(2.dp, Color.Red)
259259
.size(widthDp)
260260
.clickable {
261+
particleState2.changeProgressManually = true
261262
particleState2.startAnimation()
262263
}
263264
.disintegrate(
@@ -267,7 +268,6 @@ fun ParticleAnimationSample() {
267268
Toast.makeText(context, "Animation started...", Toast.LENGTH_SHORT).show()
268269
},
269270
onEnd = {
270-
// particleState2.animationStatus = AnimationStatus.Idle
271271
Toast.makeText(context, "Animation ended...", Toast.LENGTH_SHORT).show()
272272
}
273273
),
@@ -306,19 +306,23 @@ fun Modifier.disintegrate(
306306
if (animationStatus != AnimationStatus.Idle) {
307307

308308
withContext(Dispatchers.Default) {
309+
310+
val currentBitmap = particleState.bitmap?.asAndroidBitmap()
311+
309312
val bitmap =
310-
if (particleState.bitmap == null || particleState.bitmap?.isRecycled==true) {
313+
if (currentBitmap == null || currentBitmap.isRecycled) {
311314
graphicsLayer
312315
.toImageBitmap()
313316
.asAndroidBitmap()
314317
.copy(Bitmap.Config.ARGB_8888, false)
315318
.apply {
316319
this.prepareToDraw()
317320
}
318-
} else particleState.bitmap
321+
322+
} else particleState.bitmap?.asAndroidBitmap()
319323

320324
bitmap?.let {
321-
particleState.bitmap = bitmap
325+
particleState.bitmap = bitmap.asImageBitmap()
322326
particleState.createParticles(
323327
particleList = particleState.particleList,
324328
particleSize = particleSizePx,
@@ -371,7 +375,9 @@ fun Modifier.disintegrate(
371375
)
372376

373377
@Composable
374-
fun rememberParticleState(particleSize: Dp = 2.dp): ParticleState {
378+
fun rememberParticleState(
379+
particleSize: Dp = 2.dp,
380+
): ParticleState {
375381
return remember {
376382
ParticleState(particleSize)
377383
}
@@ -391,15 +397,17 @@ class ParticleState internal constructor(particleSize: Dp) {
391397
val progress: Float
392398
get() = animatable.value
393399

394-
var bitmap: Bitmap? = null
400+
var bitmap: ImageBitmap? = null
395401
internal set
396402

397403
var animationSpec = tween<Float>(
398404
durationMillis = 2000,
399405
easing = FastOutSlowInEasing
400406
)
401407

402-
private val strategy = DisintegrateStrategy()
408+
var changeProgressManually: Boolean = false
409+
410+
private val strategy: ParticleStrategy = DisintegrateStrategy()
403411

404412
fun addParticle(particle: Particle) {
405413
particleList.add(particle)
@@ -408,7 +416,7 @@ class ParticleState internal constructor(particleSize: Dp) {
408416
fun updateAndDrawParticles(
409417
drawScope: DrawScope,
410418
particleList: SnapshotStateList<Particle>,
411-
bitmap: Bitmap?,
419+
bitmap: ImageBitmap?,
412420
progress: Float
413421
) {
414422
if (animationStatus != AnimationStatus.Idle) {
@@ -441,19 +449,24 @@ class ParticleState internal constructor(particleSize: Dp) {
441449
try {
442450
onStart()
443451
animatable.snapTo(0f)
444-
animatable.animateTo(
445-
targetValue = 1f,
446-
animationSpec = animationSpec
447-
)
452+
if (changeProgressManually.not()) {
453+
animatable.animateTo(
454+
targetValue = 1f,
455+
animationSpec = animationSpec
456+
)
457+
}
448458
} catch (e: CancellationException) {
449-
println("FAILED: ${e.message}")
459+
Log.e("Particle", "${e.message}")
450460
} finally {
451-
onEnd()
461+
if (changeProgressManually.not()) {
462+
animationStatus = AnimationStatus.Idle
463+
onEnd()
464+
}
452465
}
453466
}
454467

455468
fun dispose() {
456-
bitmap?.recycle()
469+
bitmap?.asAndroidBitmap()?.recycle()
457470
}
458471
}
459472

@@ -561,7 +574,7 @@ open class DisintegrateStrategy : ParticleStrategy {
561574
override fun updateAndDrawParticles(
562575
drawScope: DrawScope,
563576
particleList: SnapshotStateList<Particle>,
564-
bitmap:Bitmap,
577+
imageBitmap:ImageBitmap,
565578
progress: Float,
566579
) {
567580
with(drawScope) {
@@ -587,13 +600,10 @@ open class DisintegrateStrategy : ParticleStrategy {
587600
clipRect(
588601
left = progress * size.width * 2f
589602
) {
590-
bitmap?.asImageBitmap()?.let {
591-
// Source
592-
drawImage(
593-
image = it,
594-
blendMode = BlendMode.SrcOut
595-
)
596-
}
603+
drawImage(
604+
image = imageBitmap,
605+
blendMode = BlendMode.SrcOut
606+
)
597607
}
598608

599609
// For debugging
@@ -622,9 +632,9 @@ open class DisintegrateStrategy : ParticleStrategy {
622632

623633
// Set size
624634
val width =
625-
initialSize.width + (endSize.width - initialSize.width) * currentTime* .5f
635+
initialSize.width + (endSize.width - initialSize.width) * currentTime
626636
val height =
627-
initialSize.height + (endSize.height - initialSize.height) * currentTime* .5f
637+
initialSize.height + (endSize.height - initialSize.height) * currentTime
628638
currentSize = Size(width, height)
629639

630640
// Set alpha
@@ -647,7 +657,23 @@ open class DisintegrateStrategy : ParticleStrategy {
647657
}
648658
}
649659

660+
data class ParticleBoundaries(
661+
val velocityHorizontalLowerBound: ClosedRange<Float>? = null,
662+
val velocityHorizontalUpperBound: ClosedRange<Float>? = null,
663+
val velocityVerticalLowerBound: ClosedRange<Float>? = null,
664+
val velocityVerticalUpperBound: ClosedRange<Float>? = null,
665+
val accelerationLowerBound: ClosedRange<Float>? = null,
666+
val accelerationLowerUpperBound: ClosedRange<Float>? = null,
667+
val startSizeLowerBound: Size? = null,
668+
val startSizeUpperBound: Size? = null,
669+
val endSizeLowerBound: Size? = null,
670+
val endSizeUpperBound: Size? = null,
671+
val alphaLowerBound: ClosedRange<Float>? = null,
672+
val alphaUpperbound: ClosedRange<Float>? = null
673+
)
674+
650675
interface ParticleStrategy {
676+
651677
fun createParticles(
652678
particleList: SnapshotStateList<Particle>,
653679
particleSize: Int,
@@ -657,7 +683,7 @@ interface ParticleStrategy {
657683
fun updateAndDrawParticles(
658684
drawScope: DrawScope,
659685
particleList: SnapshotStateList<Particle>,
660-
bitmap:Bitmap,
686+
imageBitmap:ImageBitmap,
661687
progress: Float
662688
)
663689

@@ -729,7 +755,7 @@ data class Particle(
729755
}
730756

731757
enum class AnimationStatus {
732-
Idle, Initializing, Playing
758+
Idle, Initializing, Playing, Finished
733759
}
734760

735761
private fun DrawScope.drawWithLayer(block: DrawScope.() -> Unit) {

0 commit comments

Comments
(0)

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