画布旋转后更改RectF的坐标

时间:2020-01-11 07:41:28

标签: android android-canvas

我使用此代码垂直绘制文本。

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,但无法实现自己想要的目标。

enter image description here

2 个答案:

答案 0 :(得分:1)

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

}

答案 1 :(得分:0)

好吧,如果要在此视图中拖动对象,则可能需要创建ViewGroup并将对象放置为Views。因为所有对象都绘制在画布上,所以您无法在其上点击和拖动。

您可以旋转,平移和调整视图的大小,还可以拦截视图上的触摸事件以执行拖动

您的解决方案只能像带有文字的图片一样使用,如果我理解正确,您将无法对其进行所需的操作

相关问题