2
2
3
3
package com.dede.android_eggs.cat_editor
4
4
5
+ import android.graphics.Bitmap
5
6
import androidx.compose.animation.AnimatedVisibility
6
7
import androidx.compose.animation.core.LinearEasing
7
8
import androidx.compose.animation.core.RepeatMode
@@ -22,21 +23,23 @@ import androidx.compose.foundation.layout.fillMaxSize
22
23
import androidx.compose.runtime.Composable
23
24
import androidx.compose.runtime.LaunchedEffect
24
25
import androidx.compose.runtime.getValue
26
+ import androidx.compose.runtime.mutableStateOf
25
27
import androidx.compose.runtime.remember
26
28
import androidx.compose.runtime.setValue
27
29
import androidx.compose.ui.Alignment
28
30
import androidx.compose.ui.ExperimentalComposeUiApi
29
31
import androidx.compose.ui.Modifier
30
32
import androidx.compose.ui.geometry.Offset
33
+ import androidx.compose.ui.geometry.Size
31
34
import androidx.compose.ui.graphics.Matrix
32
35
import androidx.compose.ui.graphics.drawscope.withTransform
33
36
import androidx.compose.ui.graphics.graphicsLayer
34
37
import androidx.compose.ui.input.pointer.pointerInput
35
- import androidx.compose.ui.layout.onSizeChanged
36
38
import androidx.compose.ui.res.stringResource
37
39
import androidx.compose.ui.tooling.preview.Preview
38
40
import com.dede.android_eggs.cat_editor.CaptureControllerDelegate.Companion.rememberCaptureControllerDelegate
39
41
import com.dede.android_eggs.cat_editor.CatParts.VIEW_PORT_SIZE
42
+ import com.dede.android_eggs.cat_editor.Utilities.asAndroidMatrix
40
43
import com.dede.android_eggs.cat_editor.Utilities.toInvert
41
44
import dev.shreyaspatil.capturable.capturable
42
45
import kotlin.math.max
@@ -45,8 +48,8 @@ import com.dede.android_eggs.resources.R as StringR
45
48
46
49
private const val TAG = " CatEditor"
47
50
48
- private const val S_MIN = 0.5f
49
- private const val S_MAX = 3f
51
+ private const val S_MIN = 0.3f
52
+ private const val S_MAX = 5f
50
53
51
54
private fun range (float : Float , max : Float , min : Float ): Float {
52
55
return min(max, max(float, min))
@@ -132,27 +135,26 @@ internal fun CatEditor(
132
135
val infiniteTransition = rememberInfiniteTransition(label = " CatEditor_SelectedPart" )
133
136
val blendRatio by infiniteTransition.animateFloat(
134
137
initialValue = 0f ,
135
- targetValue = 0.5f ,
138
+ targetValue = if (selectedPart != - 1 ) 0.5f else 0f ,
136
139
animationSpec = infiniteRepeatable(
137
140
animation = tween(700 , easing = LinearEasing ),
138
141
repeatMode = RepeatMode .Reverse ,
139
142
),
140
143
label = " HighlightColorBlend"
141
144
)
142
145
143
- val canvasMatrix = remember { Matrix () }
146
+ var canvasSize by remember { mutableStateOf(Size .Zero ) }
147
+ val canvasMatrix = remember(canvasSize) { createCanvasMatrix(canvasSize) }
148
+
149
+ val bitmap: Bitmap ? = remember(canvasSize) {
150
+ if (needAndroidCanvasDraw) createAndroidBitmap(canvasSize) else null
151
+ }
144
152
Canvas (
145
153
contentDescription = stringResource(StringR .string.cat_editor),
146
154
modifier = Modifier
147
155
.fillMaxSize()
148
156
.aspectRatio(1f )
149
157
.capturable(captureController.getDelegate())
150
- .onSizeChanged {
151
- val size = min(it.width, it.height)
152
- canvasMatrix.reset()
153
- canvasMatrix.translate((it.width - size) / 2f , (it.height - size) / 2f )
154
- canvasMatrix.scale(size / VIEW_PORT_SIZE , size / VIEW_PORT_SIZE )
155
- }
156
158
.pointerInput(controllerImpl.isSelectEnabled) {
157
159
if (! controllerImpl.isSelectEnabled) {
158
160
return @pointerInput
@@ -180,6 +182,25 @@ internal fun CatEditor(
180
182
}
181
183
},
182
184
onDraw = {
185
+ if (size != canvasSize) {
186
+ // canvas size changed
187
+ canvasSize = size
188
+ return @Canvas
189
+ }
190
+
191
+ // native canvas draw
192
+ if (needAndroidCanvasDraw && bitmap != null ) {
193
+ androidCanvasDraw(
194
+ canvasMatrix.asAndroidMatrix(),
195
+ bitmap,
196
+ controllerImpl.colorList,
197
+ selectedPart,
198
+ blendRatio
199
+ )
200
+ return @Canvas
201
+ }
202
+
203
+ // compose canvas draw
183
204
withTransform({ transform(canvasMatrix) }) {
184
205
CatParts .drawOrders.forEachIndexed { index, pathDraw ->
185
206
var color = controllerImpl.colorList[index]
@@ -195,3 +216,11 @@ internal fun CatEditor(
195
216
}
196
217
}
197
218
}
219
+
220
+ private fun createCanvasMatrix (size : Size ): Matrix {
221
+ val matrix = Matrix ()
222
+ val minDimension = size.minDimension
223
+ matrix.translate((size.width - minDimension) / 2f , (size.height - minDimension) / 2f )
224
+ matrix.scale(minDimension / VIEW_PORT_SIZE , minDimension / VIEW_PORT_SIZE )
225
+ return matrix
226
+ }
0 commit comments