首页 > 解决方案 > 我有一个位图,并在其上绘制了一个形状。现在我旋转位图并希望形状也被旋转。如何获得形状的点?

问题描述

我想为 Android 制作一个支持旋转的自定义图像裁剪器。Cropper 工作正常。我在旋转图像时面临的问题(使用旋转矩阵的位图)裁剪覆盖区域也应该相应地旋转。为此,我通过应用另一个旋转矩阵来旋转裁剪形状的点。它映射作物形状的点(特别是图像中不同颜色圆圈指向的边界框)。旋转矩阵应用于作物形状旋转以作物形状的中心为原点应用。但是在旋转裁剪形状后,形状的点应该被平移以保持相同的位置和纵横比。任何人都可以帮助我实现这一目标吗?带有裁剪形状的原始图像[我得到的旋转图像......看裁剪形状不在预期位置,尽管它相对于它的中心点旋转2我期望的作物区域的实际位置。 请注意,随着图像位图宽度和高度的改变,裁剪形状的宽度高度也应该改变。 我想它只需要翻译点的位置。 所以我需要一种方法来计算点的更新位置我正在分享一些代码片段来旋转位图和裁剪形状点

private Bitmap rotateBitmap(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    Bitmap rotatedBitmap = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
    rotateShapePoints(angle);
    return rotatedBitmap;
}

旋转形状点

 private void rotateShapePoints(float angle)
{
    PointF centerPoint = getCropShapeView().getCenterPoint(); // return the center of the shape
    Matrix matrix = new Matrix();
    matrix.postRotate(angle,centerPoint.x,centerPoint.y);
    HashMap<Integer,PointF> points = (HashMap<Integer, PointF>) getCropShapeView().getPoints(); // map is used to track the point at different position
    for(Map.Entry<Integer,PointF> entry : points.entrySet())
    {
        int key = entry.getKey();
        PointF value = entry.getValue();
        float[] pointArray = new float[] { value.x,value.y};
        matrix.mapPoints(pointArray);
        PointF rotatedPoint = new PointF(Math.abs(pointArray[0]),Math.abs(pointArray[1]));
        points.put(key,rotatedPoint);
    }
    getCropShapeView().setPoints(points);
    getCropShapeView().invalidate();
}

标签: androidimagebitmaprotation

解决方案


推荐阅读