Skip to content

Commit 2c4b3ce

Browse files
committed
feat: Cat Editor draw logic compatible
feat: Update Cat Editor guidline Signed-off-by: Hu Shenghao <[email protected]>
1 parent b2b6f8b commit 2c4b3ce

File tree

7 files changed

+87
-47
lines changed

7 files changed

+87
-47
lines changed

feature/cat-editor/src/main/java/com/dede/android_eggs/cat_editor/CatEditor.kt

+26-19
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,12 @@ import androidx.compose.ui.Modifier
3232
import androidx.compose.ui.geometry.Offset
3333
import androidx.compose.ui.geometry.Size
3434
import androidx.compose.ui.graphics.Matrix
35-
import androidx.compose.ui.graphics.drawscope.withTransform
3635
import androidx.compose.ui.graphics.graphicsLayer
3736
import androidx.compose.ui.input.pointer.pointerInput
37+
import androidx.compose.ui.layout.onSizeChanged
3838
import androidx.compose.ui.res.stringResource
3939
import androidx.compose.ui.tooling.preview.Preview
40+
import androidx.compose.ui.unit.toSize
4041
import com.dede.android_eggs.cat_editor.CaptureControllerDelegate.Companion.rememberCaptureControllerDelegate
4142
import com.dede.android_eggs.cat_editor.CatParts.VIEW_PORT_SIZE
4243
import com.dede.android_eggs.cat_editor.Utilities.asAndroidMatrix
@@ -84,8 +85,7 @@ internal fun CatEditor(
8485
captureController.onPerCapture = {
8586
// set normal state
8687
selectedPart = -1
87-
scale = 1f
88-
offset = Offset.Zero
88+
controllerImpl.resetGraphicsLayer()
8989
}
9090
}
9191

@@ -102,10 +102,15 @@ internal fun CatEditor(
102102
scale = nextScaleLevel(scale, S_MAX, S_MIN)
103103
}
104104

105+
var editorSize by remember { mutableStateOf(Size.Zero) }
106+
105107
Box(
106108
contentAlignment = Alignment.Center,
107109
modifier = Modifier
108110
.fillMaxSize()
111+
.onSizeChanged {
112+
editorSize = it.toSize()
113+
}
109114
.pointerInput(controllerImpl.isGesturesEnabled) {
110115
if (!controllerImpl.isGesturesEnabled) {
111116
return@pointerInput
@@ -146,8 +151,15 @@ internal fun CatEditor(
146151
var canvasSize by remember { mutableStateOf(Size.Zero) }
147152
val canvasMatrix = remember(canvasSize) { createCanvasMatrix(canvasSize) }
148153

154+
LaunchedEffect(canvasSize, editorSize) {
155+
if (canvasSize != Size.Zero && editorSize != Size.Zero) {
156+
controllerImpl.defaultGraphicsLayerScale =
157+
editorSize.minDimension / canvasSize.maxDimension
158+
}
159+
}
160+
149161
val bitmap: Bitmap? = remember(canvasSize) {
150-
if (needAndroidCanvasDraw) createAndroidBitmap(canvasSize) else null
162+
if (useAndroidCanvasDraw) createAndroidBitmap(canvasSize) else null
151163
}
152164
Canvas(
153165
contentDescription = stringResource(StringR.string.cat_editor),
@@ -188,28 +200,23 @@ internal fun CatEditor(
188200
return@Canvas
189201
}
190202

191-
// native canvas draw
192-
if (needAndroidCanvasDraw && bitmap != null) {
203+
if (useAndroidCanvasDraw && bitmap != null) {
204+
// android canvas draw
193205
androidCanvasDraw(
194206
canvasMatrix.asAndroidMatrix(),
195207
bitmap,
196208
controllerImpl.colorList,
197209
selectedPart,
198210
blendRatio
199211
)
200-
return@Canvas
201-
}
202-
203-
// compose canvas draw
204-
withTransform({ transform(canvasMatrix) }) {
205-
CatParts.drawOrders.forEachIndexed { index, pathDraw ->
206-
var color = controllerImpl.colorList[index]
207-
if (selectedPart == index) {
208-
val blend = Utilities.getHighlightColor(color)
209-
color = Utilities.blendColor(color, blend, blendRatio)
210-
}
211-
pathDraw.drawLambda.invoke(this, color)
212-
}
212+
} else {
213+
// compose canvas draw
214+
composeCanvasDraw(
215+
canvasMatrix,
216+
controllerImpl.colorList,
217+
selectedPart,
218+
blendRatio
219+
)
213220
}
214221
}
215222
)

feature/cat-editor/src/main/java/com/dede/android_eggs/cat_editor/CatEditorAndroidCanvasDraw.kt

+3-9
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ internal fun createAndroidBitmap(size: Size): Bitmap {
2727
}
2828

2929
// fix Android N canvas scale
30-
internal val needAndroidCanvasDraw =
31-
Build.VERSION.SDK_INT in Build.VERSION_CODES.N..Build.VERSION_CODES.N_MR1
30+
internal val useAndroidCanvasDraw = Build.VERSION.SDK_INT <= Build.VERSION_CODES.N_MR1
3231

3332
private val androidPaint = Paint(Paint.ANTI_ALIAS_FLAG)
3433

@@ -46,13 +45,8 @@ internal fun DrawScope.androidCanvasDraw(
4645
// draw bitmap
4746
bitmap.applyCanvas {
4847
withMatrix(matrix) {
49-
CatParts.drawOrders.forEachIndexed { index, pathDraw ->
50-
var color = colorList[index]
51-
if (selectedPart == index) {
52-
val blend = Utilities.getHighlightColor(color)
53-
color = Utilities.blendColor(color, blend, blendRatio)
54-
}
55-
pathDraw.drawLambda2.invoke(this, color, androidPaint)
48+
forEachCatDrawPart(colorList, selectedPart, blendRatio) { part, color ->
49+
part.androidDrawLambda(this, color, androidPaint)
5650
}
5751
}
5852
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.dede.android_eggs.cat_editor
2+
3+
import androidx.compose.ui.graphics.Color
4+
import androidx.compose.ui.graphics.Matrix
5+
import androidx.compose.ui.graphics.drawscope.DrawScope
6+
import androidx.compose.ui.graphics.drawscope.withTransform
7+
8+
9+
internal fun forEachCatDrawPart(
10+
colorList: List<Color>,
11+
selectedPart: Int,
12+
blendRatio: Float,
13+
block: (part: CatParts.PathDraw, color: Color) -> Unit
14+
) {
15+
CatParts.drawOrders.forEachIndexed { index, pathDraw ->
16+
var color = colorList[index]
17+
if (selectedPart == index) {
18+
val blend = Utilities.getHighlightColor(color)
19+
color = Utilities.blendColor(color, blend, blendRatio)
20+
}
21+
block(pathDraw, color)
22+
}
23+
}
24+
25+
internal fun DrawScope.composeCanvasDraw(
26+
matrix: Matrix,
27+
colorList: List<Color>,
28+
selectedPart: Int,
29+
blendRatio: Float
30+
) {
31+
withTransform({ transform(matrix) }) {
32+
forEachCatDrawPart(colorList, selectedPart, blendRatio) { part, color ->
33+
part.drawLambda(this, color)
34+
}
35+
}
36+
}

feature/cat-editor/src/main/java/com/dede/android_eggs/cat_editor/CatEditorController.kt

+9-1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ internal class CatEditorControllerImpl(private val speed: Long) : CatEditorContr
8383

8484
private val colorStateList = mutableStateListOf(*CatPartColors.colors(speed))
8585

86+
override var defaultGraphicsLayerScale: Float = 1f
87+
set(value) {
88+
field = value
89+
resetGraphicsLayer()
90+
}
91+
8692
override val colorList: List<Color> = colorStateList
8793

8894
override var selectPart: Int by selectedPartState
@@ -107,13 +113,15 @@ internal class CatEditorControllerImpl(private val speed: Long) : CatEditorContr
107113
}
108114

109115
override fun resetGraphicsLayer() {
110-
scaleState.floatValue = 1f
116+
scaleState.floatValue = defaultGraphicsLayerScale
111117
offsetState.value = Offset.Zero
112118
}
113119
}
114120

115121
internal interface CatEditorController {
116122

123+
var defaultGraphicsLayerScale: Float
124+
117125
var isSelectEnabled: Boolean
118126

119127
var isGesturesEnabled: Boolean

feature/cat-editor/src/main/java/com/dede/android_eggs/cat_editor/CatEditorGridLine.kt

+9-9
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import androidx.compose.ui.platform.LocalDensity
1212
import androidx.compose.ui.tooling.preview.Preview
1313
import androidx.compose.ui.unit.Dp
1414
import androidx.compose.ui.unit.dp
15-
import kotlin.math.roundToInt
1615

1716
/**
1817
* A grid line for the cat editor.
@@ -22,11 +21,10 @@ import kotlin.math.roundToInt
2221
internal fun CatEditorGridLine(
2322
modifier: Modifier = Modifier,
2423
color: Color = MaterialTheme.colorScheme.tertiary,
25-
step: Dp = 20.dp,
24+
gridLineCount: Int = 28,
2625
strokeWidth: Dp = 1.dp,
2726
) {
2827

29-
val stepPx = with(LocalDensity.current) { step.toPx() }
3028
val strokeWidthPx = with(LocalDensity.current) { strokeWidth.toPx() }
3129

3230
Canvas(
@@ -35,10 +33,12 @@ internal fun CatEditorGridLine(
3533
.then(modifier),
3634
onDraw = {
3735
clipRect {
38-
val columns = (size.width / stepPx).roundToInt()
39-
val offsetX = (size.width - columns * stepPx) / 2f
36+
val step = size.maxDimension / gridLineCount
37+
38+
val columns = (size.width / step).toInt() + 1
39+
val offsetX = (size.width - columns * step) / 2f
4040
for (c in 0 until columns) {
41-
val x = offsetX + c * stepPx
41+
val x = offsetX + c * step
4242
drawLine(
4343
color,
4444
start = Offset(x, 0f),
@@ -47,10 +47,10 @@ internal fun CatEditorGridLine(
4747
)
4848
}
4949

50-
val rows = (size.height / stepPx).roundToInt()
51-
val offsetY = (size.height - rows * stepPx) / 2f
50+
val rows = (size.height / step).toInt() + 1
51+
val offsetY = (size.height - rows * step) / 2f
5252
for (r in 0 until rows) {
53-
val y = offsetY + r * stepPx
53+
val y = offsetY + r * step
5454
drawLine(
5555
color,
5656
start = Offset(0f, y),

feature/cat-editor/src/main/java/com/dede/android_eggs/cat_editor/CatEditorScreen.kt

+3-8
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,9 @@
55

66
package com.dede.android_eggs.cat_editor
77

8+
import android.util.Log
89
import androidx.compose.animation.Crossfade
910
import androidx.compose.foundation.layout.Box
10-
import androidx.compose.foundation.layout.calculateEndPadding
11-
import androidx.compose.foundation.layout.calculateStartPadding
1211
import androidx.compose.foundation.layout.fillMaxSize
1312
import androidx.compose.foundation.layout.padding
1413
import androidx.compose.material.icons.Icons
@@ -43,7 +42,6 @@ import androidx.compose.ui.Modifier
4342
import androidx.compose.ui.graphics.Color
4443
import androidx.compose.ui.graphics.asAndroidBitmap
4544
import androidx.compose.ui.platform.LocalContext
46-
import androidx.compose.ui.platform.LocalLayoutDirection
4745
import androidx.compose.ui.res.stringResource
4846
import androidx.compose.ui.text.font.FontWeight
4947
import androidx.compose.ui.text.style.TextOverflow
@@ -119,6 +117,7 @@ fun CatEditorScreen() {
119117
val deferred = captureController.captureAsync()
120118
scope.launch {
121119
val bitmap = deferred.await().asAndroidBitmap()
120+
Log.i("CatEditorScreen", "saveCatToAlbum, w * h: ${bitmap.width} * ${bitmap.height}")
122121
val uri = ShareCatUtils.saveCat(context, bitmap, catName)
123122
if (uri != null) {
124123
context.toast("🐱")
@@ -240,14 +239,10 @@ fun CatEditorScreen() {
240239
)
241240
}
242241
) { contentPadding ->
243-
val layoutDirection = LocalLayoutDirection.current
244242
Box(
245243
modifier = Modifier
246244
.fillMaxSize()
247-
.padding(
248-
start = contentPadding.calculateStartPadding(layoutDirection),
249-
end = contentPadding.calculateEndPadding(layoutDirection)
250-
),
245+
.padding(contentPadding),
251246
contentAlignment = Alignment.Center
252247
) {
253248
CatEditor(

feature/cat-editor/src/main/java/com/dede/android_eggs/cat_editor/CatParts.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ internal object CatParts {
368368

369369
val drawLambda: DrawScope.(color: Color) -> Unit = { draw(it) }
370370

371-
val drawLambda2: AndroidCanvas.(color: Color, paint: Paint) -> Unit = { c, p ->
371+
val androidDrawLambda: AndroidCanvas.(color: Color, paint: Paint) -> Unit = { c, p ->
372372
p.setColor(c.toArgb())
373373
androidDraw(p)
374374
}

0 commit comments

Comments
 (0)