0

如何通过坐标绘制画布并让它们与图片一起缩放?1. 在图像侧,彩色多边形应与背景图像一起缩放,但事实并非如此。(它显示在正确的位置) 2. polygonList 包含坐标,缩放时应该改变值,所以如果点击位置在 pologon 中,onSigleTabEvent 可以找到。但是现在,它不起作用,因为坐标总是变成无穷大。 (解决了!)

PS:TouchImageView 嵌入在 XML 文件中。TouchImageView .java 中的 onDraw 方法

@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas);

    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()); List 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;
    }
}
4

0 回答 0