android - 我有一个位图,并在其上绘制了一个形状。现在我旋转位图并希望形状也被旋转。如何获得形状的点?
问题描述
我想为 Android 制作一个支持旋转的自定义图像裁剪器。Cropper 工作正常。我在旋转图像时面临的问题(使用旋转矩阵的位图)裁剪覆盖区域也应该相应地旋转。为此,我通过应用另一个旋转矩阵来旋转裁剪形状的点。它映射作物形状的点(特别是图像中不同颜色圆圈指向的边界框)。旋转矩阵应用于作物形状旋转以作物形状的中心为原点应用。但是在旋转裁剪形状后,形状的点应该被平移以保持相同的位置和纵横比。任何人都可以帮助我实现这一目标吗?[我正在分享一些代码片段来旋转位图和裁剪形状点
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();
}
解决方案
推荐阅读
- python - Python 多条件 if 语句,每个条件都有操作
- vue.js - 如何在vue js路由器中传递api获取数据传递另一个页面
- bash - bash中列中的模式匹配
- visual-studio - Visual Studio 在调试模式下运行启用了 Docker 的项目而不使用 docker
- java - lombok 为默认构建器调用子类函数
- php - 在 PHP 中打开 Ajax 调用时如何从一个页面转到另一个页面?
- biopython - 使用 bioentrez 从 Pubmed ID 中提取存放在 pubmed 中的文章的标题
- angular - 如何在 Angular 7 中从我的 Cosmos DB 中查询数据?
- javascript - Jest Mock React 组件
- java - 列出 Amazon s3 客户端的第一个深度目录