首页 > 解决方案 > 画布旋转后更改 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但我无法实现我想要的。

在此处输入图像描述

标签: androidandroid-canvas

解决方案


在 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]);
    }

}

推荐阅读