Skip to content

Commit b74c9ef

Browse files
add animation spec param to animations for TransformState
1 parent 15a0cba commit b74c9ef

1 file changed

Lines changed: 29 additions & 14 deletions

File tree

cropper/src/main/java/com/smarttoolfactory/cropper/state/TransformState.kt

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
package com.smarttoolfactory.cropper.state
22

33
import androidx.compose.animation.core.Animatable
4+
import androidx.compose.animation.core.AnimationSpec
45
import androidx.compose.animation.core.exponentialDecay
6+
import androidx.compose.animation.core.tween
57
import androidx.compose.runtime.Stable
68
import androidx.compose.runtime.getValue
79
import androidx.compose.runtime.mutableStateOf
@@ -60,7 +62,7 @@ open class TransformState(
6062

6163
internal val animatablePanX = Animatable(0f)
6264
internal val animatablePanY = Animatable(0f)
63-
internal val animatableZoom = Animatable(zoomInitial)
65+
internal val animatableZoom = Animatable(0f)
6466
internal val animatableRotation = Animatable(rotationInitial)
6567

6668
private val velocityTracker = VelocityTracker()
@@ -129,36 +131,49 @@ open class TransformState(
129131
internal suspend fun resetWithAnimation(
130132
pan: Offset = Offset.Zero,
131133
zoom: Float = 1f,
132-
rotation: Float = 0f
134+
rotation: Float = 0f,
135+
animationSpec: AnimationSpec<Float> = tween(400)
133136
) = coroutineScope {
134-
launch { animatePanXto(pan.x) }
135-
launch { animatePanYto(pan.y) }
136-
launch { animateZoomTo(zoom) }
137-
launch { animateRotationTo(rotation) }
137+
launch { animatePanXto(pan.x, animationSpec) }
138+
launch { animatePanYto(pan.y, animationSpec) }
139+
launch { animateZoomTo(zoom, animationSpec) }
140+
launch { animateRotationTo(rotation, animationSpec) }
138141
}
139142

140-
internal suspend fun animatePanXto(panX: Float) {
143+
internal suspend fun animatePanXto(
144+
panX: Float,
145+
animationSpec: AnimationSpec<Float> = tween(400)
146+
) {
141147
if (pannable && pan.x != panX) {
142-
animatablePanX.animateTo(panX)
148+
animatablePanX.animateTo(panX, animationSpec)
143149
}
144150
}
145151

146-
internal suspend fun animatePanYto(panY: Float) {
152+
internal suspend fun animatePanYto(
153+
panY: Float,
154+
animationSpec: AnimationSpec<Float> = tween(400)
155+
) {
147156
if (pannable && pan.y != panY) {
148-
animatablePanY.animateTo(panY)
157+
animatablePanY.animateTo(panY, animationSpec)
149158
}
150159
}
151160

152-
internal suspend fun animateZoomTo(zoom: Float) {
161+
internal suspend fun animateZoomTo(
162+
zoom: Float,
163+
animationSpec: AnimationSpec<Float> = tween(400)
164+
) {
153165
if (zoomable && this.zoom != zoom) {
154166
val newZoom = zoom.coerceIn(zoomMin, zoomMax)
155-
animatableZoom.animateTo(newZoom)
167+
animatableZoom.animateTo(newZoom, animationSpec)
156168
}
157169
}
158170

159-
internal suspend fun animateRotationTo(rotation: Float) {
171+
internal suspend fun animateRotationTo(
172+
rotation: Float,
173+
animationSpec: AnimationSpec<Float> = tween(400)
174+
) {
160175
if (rotatable && this.rotation != rotation) {
161-
animatableRotation.animateTo(rotation)
176+
animatableRotation.animateTo(rotation, animationSpec)
162177
}
163178
}
164179

0 commit comments

Comments
 (0)