android - 画布旋转后更改 RectF 的坐标
问题描述
我使用此代码垂直绘制文本。
RectF rectF2 = new RectF();
matrix.mapRect(rectF2, bounds);
canvas.save();
canvas.rotate(90, rectF2.right, rectF2.top);
canvas.drawText(text, rectF2.left, rectF2.bottom, mTextPaint);
canvas.restore();
这很好用,但我也想更改坐标。因为后来我点击对象并进行拖放。
现在的问题是,如下图所示,坐标被绘制为矩形。所以当我点击那个矩形区域时,只能在画布上移动文本。
所以我想在旋转画布时也旋转原始坐标。我试过matrix.setRotate
但我无法实现我想要的。
解决方案
在 onDraw() 中,您可以使用矩阵变换画布。在 onTouch() 中,您可以使用逆矩阵转换回屏幕坐标。
private static class MyView extends View {
private final Paint texPaint = new Paint(){{
setTextSize(60);
setColor(Color.BLACK);
}};
private final Paint rectPaint = new Paint(){{
setStrokeWidth(20);
setColor(Color.RED);
setStyle(Paint.Style.STROKE);
}};
private final Matrix matrix = new Matrix();
private final RectF rect = new RectF(0, 0, 400, 150);
public MyView(Context context) {
super(context);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
matrix.reset();
matrix.postRotate(45);
matrix.postScale(1.5f, 1.5f, 0, 0);
matrix.postTranslate(w/2, h/2);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.concat(matrix);
canvas.drawRect(rect, rectPaint);
//Bottom line of the drawn text is at y, if you want the text to inside the rect
// increase the y by the text height, textHeight is the textSize
canvas.drawText("SomeText", 0, texPaint.getTextSize(), texPaint);
canvas.restore();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getActionMasked() == MotionEvent.ACTION_UP) {
float x = event.getX();
float y = event.getY();
Toast.makeText(getContext(), isInsideRegion(x, y) ? "Inside" : "Outside", Toast.LENGTH_SHORT).show();
}
return true;
}
private boolean isInsideRegion(float x, float y) {
Matrix invertedMatrix = new Matrix();
if (!matrix.invert(invertedMatrix)) {
throw new RuntimeException("Matrix can't be inverted");
}
float[] point = {x, y};
invertedMatrix.mapPoints(point);
return rect.contains(point[0], point[1]);
}
}
推荐阅读
- python - SyntaxError:默认“除外:”必须是最后一个
- azure-data-factory - 数据工厂 - Kusto - 摄取集群
- sql - 存储过程的 MS-SQL 权限
- package - 如何为前端和后端使用相同的 ANTLR 语法文件?
- performance - 求解具有大量数字的模方程的有效方法
- vue.js - Nuxt.js 路由器 - 更改路由参数而不重新安装新页面?
- r - 将 RasterLayer 添加到传单地图时的空间数据维度问题
- powershell - 如何设置多种类型的返回值?
- amazon-web-services - Aws acm - 由于证书颁发机构身份验证 (CAA) 错误,一个或多个域名验证失败
- java - Spring Data JPA 更新查询(不支持 DML 操作)