|
1 | 1 | package com.smarttoolfactory.cropper.state |
2 | 2 |
|
3 | 3 | import androidx.compose.animation.core.Animatable |
| 4 | +import androidx.compose.animation.core.AnimationSpec |
4 | 5 | import androidx.compose.animation.core.exponentialDecay |
| 6 | +import androidx.compose.animation.core.tween |
5 | 7 | import androidx.compose.runtime.Stable |
6 | 8 | import androidx.compose.runtime.getValue |
7 | 9 | import androidx.compose.runtime.mutableStateOf |
@@ -60,7 +62,7 @@ open class TransformState( |
60 | 62 |
|
61 | 63 | internal val animatablePanX = Animatable(0f) |
62 | 64 | internal val animatablePanY = Animatable(0f) |
63 | | - internal val animatableZoom = Animatable(zoomInitial) |
| 65 | + internal val animatableZoom = Animatable(0f) |
64 | 66 | internal val animatableRotation = Animatable(rotationInitial) |
65 | 67 |
|
66 | 68 | private val velocityTracker = VelocityTracker() |
@@ -129,36 +131,49 @@ open class TransformState( |
129 | 131 | internal suspend fun resetWithAnimation( |
130 | 132 | pan: Offset = Offset.Zero, |
131 | 133 | zoom: Float = 1f, |
132 | | - rotation: Float = 0f |
| 134 | + rotation: Float = 0f, |
| 135 | + animationSpec: AnimationSpec<Float> = tween(400) |
133 | 136 | ) = 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) } |
138 | 141 | } |
139 | 142 |
|
140 | | - internal suspend fun animatePanXto(panX: Float) { |
| 143 | + internal suspend fun animatePanXto( |
| 144 | + panX: Float, |
| 145 | + animationSpec: AnimationSpec<Float> = tween(400) |
| 146 | + ) { |
141 | 147 | if (pannable && pan.x != panX) { |
142 | | - animatablePanX.animateTo(panX) |
| 148 | + animatablePanX.animateTo(panX, animationSpec) |
143 | 149 | } |
144 | 150 | } |
145 | 151 |
|
146 | | - internal suspend fun animatePanYto(panY: Float) { |
| 152 | + internal suspend fun animatePanYto( |
| 153 | + panY: Float, |
| 154 | + animationSpec: AnimationSpec<Float> = tween(400) |
| 155 | + ) { |
147 | 156 | if (pannable && pan.y != panY) { |
148 | | - animatablePanY.animateTo(panY) |
| 157 | + animatablePanY.animateTo(panY, animationSpec) |
149 | 158 | } |
150 | 159 | } |
151 | 160 |
|
152 | | - internal suspend fun animateZoomTo(zoom: Float) { |
| 161 | + internal suspend fun animateZoomTo( |
| 162 | + zoom: Float, |
| 163 | + animationSpec: AnimationSpec<Float> = tween(400) |
| 164 | + ) { |
153 | 165 | if (zoomable && this.zoom != zoom) { |
154 | 166 | val newZoom = zoom.coerceIn(zoomMin, zoomMax) |
155 | | - animatableZoom.animateTo(newZoom) |
| 167 | + animatableZoom.animateTo(newZoom, animationSpec) |
156 | 168 | } |
157 | 169 | } |
158 | 170 |
|
159 | | - internal suspend fun animateRotationTo(rotation: Float) { |
| 171 | + internal suspend fun animateRotationTo( |
| 172 | + rotation: Float, |
| 173 | + animationSpec: AnimationSpec<Float> = tween(400) |
| 174 | + ) { |
160 | 175 | if (rotatable && this.rotation != rotation) { |
161 | | - animatableRotation.animateTo(rotation) |
| 176 | + animatableRotation.animateTo(rotation, animationSpec) |
162 | 177 | } |
163 | 178 | } |
164 | 179 |
|
|
0 commit comments