Browse Source

1. 可以选择编辑轮廓、中线、宽线。
2. 移除调试界面。

Ulric 5 days ago
parent
commit
ac44a4df61

+ 1 - 0
app/src/main/java/com/ys/imageProcess/ui/work/AnchorLayer.kt

@@ -29,6 +29,7 @@ fun AnchorLayer(
     BoxWithConstraints(modifier) {
         Spacer(modifier = Modifier.width(maxWidth))
 
+
         val p = PointF(200f, 200f)
         val offsetX = p.x - maxWidth.value / 2
         val offsetY = p.y - maxHeight.value / 2

+ 17 - 17
app/src/main/java/com/ys/imageProcess/ui/work/CentralImageView.kt

@@ -51,22 +51,22 @@ fun CentralImageView(
         //        val width by viewModel.width.collectAsStateWithLifecycle()
         //        val length by viewModel.length.collectAsStateWithLifecycle()
 
-        val w = mutableListOf<Offset>()
-        val center = canvasSize.center
-
-        for (origin in width) {
-            val originToCenter = origin - center
-            val finalToCenter = originToCenter * scale + offset
-            val final = finalToCenter + center
-
-            val rect = Rect(center * 0.5f, center * 1.5f)
-            if (rect.contains(final)) {
-                val np = Offset(origin.x + 10, origin.y)
-                w.add(np)
-            } else {
-                w.add(origin)
-            }
-        }
+//        val w = mutableListOf<Offset>()
+//        val center = canvasSize.center
+//
+//        for (origin in width) {
+//            val originToCenter = origin - center
+//            val finalToCenter = originToCenter * scale + offset
+//            val final = finalToCenter + center
+//
+//            val rect = Rect(center * 0.5f, center * 1.5f)
+//            if (rect.contains(final)) {
+//                val np = Offset(origin.x + 10, origin.y)
+//                w.add(np)
+//            } else {
+//                w.add(origin)
+//            }
+//        }
 
 
         bitmap?.let {
@@ -81,7 +81,7 @@ fun CentralImageView(
 
             CanvasView(
                 contour = contour,
-                width = w,
+                width = width,
                 length = length,
                 state = imageState,
                 scale = scale

+ 0 - 15
app/src/main/java/com/ys/imageProcess/ui/work/WorkScreen.kt

@@ -90,7 +90,6 @@ fun WorkScreen(
                                 viewModel.onDrag(change.position / density)
                             }
                         }
-                        .background(Color.Red)
                 ) {
                     val canvasSize = Size(maxWidth.value, maxHeight.value)
                     LaunchedEffect(canvasSize) {
@@ -127,26 +126,12 @@ fun WorkScreen(
                         offset
                     )
 
-                    AnchorLayer(
-                        Modifier
-                            .fillMaxSize(),
-                        imgOffset = offset,
-                        imgScale = scale
-                    )
-
                     Text(
                         text = viewModel.zoomString,
                         Modifier
                             .align(Alignment.TopEnd)
                             .padding(20.dp)
                     )
-
-                    Spacer(
-                        modifier = Modifier
-                            .fillMaxSize(0.5f)
-                            .align(Alignment.Center)
-                            .background(Color(0x33000000))
-                    )
                 }
                 ViewToolBar(
                     Modifier

+ 41 - 31
app/src/main/java/com/ys/imageProcess/viewModel/WorkViewModel.kt

@@ -219,19 +219,20 @@ class WorkViewModel : ViewModel() {
     }
 
     /**
-     * 给定屏幕上一个位置,查找目标身上处于这个位置的点在数组中的索引
-     * 本函数会依次遍历轮廓、长、宽各个数组,找到数组中处于该位置的点
-     * @param position 屏幕上点的坐标,这里是相对于画布左上角的位移
-     * @return Pair<Int, Int>,第一个 Int 是数组的标记,0:轮廓,1:长,2:宽;第二个 Int 表示点在数组中的索引。
-     * @return Pair(-1, -1),表示没有找到目标点
+     * 给定屏幕上一个位置, 查找目标身上处于这个位置的点在数组中的索引
+     * 本函数会依次遍历 (轮廓/长/宽) 各个数组, 找到数组中处于该位置的点
+     * @param position 屏幕上点的坐标, 这里是相对于画布左上角的位移
+     * @return Pair<pathIndex,pointIndex>, 默认值 (-1,-1), 表示没有找到目标点
+     * pathIndex: 数组的标记, 0:轮廓, 1:长, 2:宽
+     * pointIndex: 表示点在数组中的索引
      */
-    fun findVertexAt(position: Offset): Pair<Int, Int> {
+    private fun findVertexAt(position: Offset): Pair<Int, Int> {
 
         /**
-         * 给定屏幕上一个位置在数组中查找此坐标附近的点
-         * 由于画布经过了缩放、平移等操作,所以需要把数组中的坐标经过相同的变换映射到新的坐标
-         * @param list 画布上点的数组,这里指轮廓,长或宽的数组
-         * @return 点在画布上的位置-1 表示查无此点
+         * 给定屏幕上一个位置, 在数组中查找此坐标附近的点
+         * 由于画布经过了缩放, 平移等操作, 所以需要把数组中的坐标经过相同的变换映射到新的坐标
+         * @param list 画布上点的数组, 这里指 (轮廓/长/宽) 的数组
+         * @return 点在画布上的位置, -1 表示查无此点
          */
         fun findVertex(list: List<Offset>): Int {
             val center = canvasSize.center
@@ -251,20 +252,28 @@ class WorkViewModel : ViewModel() {
             return -1
         }
 
+        /**
+         * 用途: 查找当前正在编辑的点
+         * 逻辑: 在可编辑状态下, 按照优先级 (轮廓>长>宽) 的顺序查找
+         */
         var pathIndex = -1
         var pointIndex = -1
-
-        val paths: List<List<Offset>> = listOf(
-            contourPath.value,
-            lengthPath.value,
-            widthPath.value
-        )
-
-        for (i in paths.indices) {
-            pointIndex = findVertex(paths[i])
-            if (pointIndex >= 0) {
-                pathIndex = i
-                break
+        if (imageState.value.editMode) {
+            if (imageState.value.showContour) {
+                pointIndex = findVertex(contourPath.value)
+                if (pointIndex >= 0) {
+                    pathIndex = 0
+                }
+            } else if (imageState.value.showLength) {
+                pointIndex = findVertex(lengthPath.value)
+                if (pointIndex >= 0) {
+                    pathIndex = 1
+                }
+            } else if (imageState.value.showWidth) {
+                pointIndex = findVertex(widthPath.value)
+                if (pointIndex >= 0) {
+                    pathIndex = 2
+                }
             }
         }
 
@@ -302,18 +311,19 @@ class WorkViewModel : ViewModel() {
         if (dragStartPos == null) {
             dragStartPos = position
 
-            // 编辑模式下,开始拖动时,检查有没有目标上的点被
-            if (imageState.value.editMode) {
-                val (pathIndex, pointIndex) = findVertexAt(position)
-                isEditingVertex = pathIndex >= 0 && pointIndex >= 0
-                if (isEditingVertex) {
-                    editingPath = pathIndex
-                    editingPoint = pointIndex
-                }
+            // 获取目标上被拖动的点
+            val (pathIndex, pointIndex) = findVertexAt(position)
+            isEditingVertex = pathIndex >= 0 && pointIndex >= 0
+            if (isEditingVertex) {
+                editingPath = pathIndex
+                editingPoint = pointIndex
             }
 
             Log.i(
-                TAG, "editing:$isEditingVertex, path: $editingPath, " +
+                TAG,
+                "drag started, " +
+                        "editing:$isEditingVertex, " +
+                        "path:$editingPath, " +
                         "point:$editingPoint"
             )
         }