如何通过坐标绘制画布并让它们与图片一起缩放? 1.在图像方面,彩色多边形应与背景图像一致,但不是。 (它显示在正确的位置) 2。 polygonList包含坐标,应该在缩放时更改值,因此如果点击位置在pologon中,则可以找到onSigleTabEvent。但现在,它不起作用,因为坐标总是变为无穷大。 (已解决!)
PS:TouchImageView嵌入在XML文件中。 TouchImageView .java中的onDraw方法
@覆盖 protected void onDraw(Canvas canvas){ super.onDraw(帆布);
if (polygonList != null) { for (Polygon polygon : polygonList) { canvas.drawPath(polygon.path, polygon.paint); } } onDrawReady = true; imageRenderedAtLeastOnce = true; if (delayedZoomVariables != null) { setZoom(delayedZoomVariables.scale, delayedZoomVariables.focusX, delayedZoomVariables.focusY, delayedZoomVariables.scaleType); delayedZoomVariables = null; } } private void fixTrans() { matrix.getValues(m); // update all polygon coordinate for (Polygon polygon : polygonList) { for (Coordinate coordinate : polygon.coordinates) { coordinate.x = coordinate.x * m[0] + m[2]; coordinate.y = coordinate.y * m[4] + m[5]; } } float transX = m[Matrix.MTRANS_X]; float transY = m[Matrix.MTRANS_Y]; float fixTransX = getFixTrans(transX, viewWidth, getImageWidth()); float fixTransY = getFixTrans(transY, viewHeight, getImageHeight()); if (fixTransX != 0 || fixTransY != 0) { matrix.postTranslate(fixTransX, fixTransY); } }
MainActivity.java
列出位置= floorPlan.getLocation(); Log.i(" FloorPlanAct"," SetupClickArea(),Location.count():" + locations.size()); 列表polygonList = new ArrayList<>();
for (Location location : locations) { List<Coordinate> coordinateList = location.getCoordinate(); // Test data, has location, but no coordinate // Paint as a pen Paint paint = new Paint(); paint.setARGB(location.areaColorA, location.areaColorR, location.areaColorG, location.areaColorB); paint.setAlpha(128); paint.setStyle(Paint.Style.FILL); // Group pen and path as a Polygon~ Polygon polygon = new Polygon(coordinateList, paint, location); polygonList.add(polygon); } // 1. Prepare originalImagePosition preparePolygon(polygonList, obj.orignalMatrix); // Create a click area TouchImageView imgClickArea = holder.imgClickArea; imgClickArea.init(act, polygonList, obj.scaledBitmap, obj.orignalMatrix); public void preparePolygon(List<Polygon> polygonList, Matrix orignalMatrix){ float[] m = new float[9]; orignalMatrix.getValues(m); for (Polygon polygon : polygonList) { List<Coordinate> coordinates = polygon.coordinates; // Path walk follow the coordinate Path path = new Path(); path.reset(); // only needed when reusing this path for a new build int coordinateCount = coordinates.size(); Log.i("TouchImageView", "***** initPolygon(), Polygon ******"); for (int i = 0; i < coordinateCount; i++) { // Calculate the point of screen Coordinate coordinate = coordinates.get(i); Log.i("TouchImageView", "**** Coordinate ****"); Log.i("TouchImageView", "Original :" + coordinate.x + "," + coordinate.y); coordinate.x = coordinate.x * m[0] + m[2]; coordinate.y = coordinate.y * m[4] + m[5]; Log.i("TouchImageView", "On the screen:" + coordinate.x + "," + coordinate.y); if (i == 0) { path.moveTo(coordinate.x, coordinate.y); } else { path.lineTo(coordinate.x, coordinate.y); } } path.close(); polygon.path = path; } }