Android 中的 Canvas.rotate() 问题.它究竟是如何工作的? [英] Issue with Canvas.rotate() in Android .How does it exactly work?

查看:15
本文介绍了Android 中的 Canvas.rotate() 问题.它究竟是如何工作的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请在下面找到我的 onDraw 方法的代码(.我正在尝试在绘制圆弧后将画布(//Rotate call -b)旋转 25 度.但是我发现圆弧仍然是从 0 到50 度.我原以为它会再移动 25 度.

Please find the code of my onDraw method below(.I'm trying to rotate the canvas(//Rotate call -b) by 25 degrees after drawing the arc .But i find that the arc is still drawn from 0 to 50 degrees.I was expecting it to move by another 25 degrees.

public class CustomView extends View {

    public CustomView(Context context) {
        super(context);

    }

    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);

    }
    protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);

            Paint paint = new Paint();
            paint.setColor(Color.RED);
            int px = getMeasuredWidth() / 2;
            int py = getMeasuredHeight() / 2;

            // radius - min 
            int radius = 130;

            // Defining bounds for the oval for the arc to be drawn
            int left = px - radius;
            int top = py - radius;
            int right = left + (radius * 2);
            int bottom = top + (radius * 2);

            RectF rectF = new RectF(left, top, right, bottom);
            paint.setColor(Color.RED);
            paint.setStyle(Style.FILL);

                //canvas.rotate(25,px,py);//Rotate call -a

        canvas.drawArc(rectF, 0, 50, true, paint);

            canvas.rotate(25,px,py);//Rotate call  -b

        }
}

但是如果我在绘制圆弧之前放置旋转调用(//Rotate call -a),我会看到绘制的圆弧偏移了 25 度.这里到底发生了什么?谁能给我解释一下?

But if i place the rotate call(//Rotate call -a) before drawing the arc i see that the arc drawn is shifted by 25 degrees more .What exactly is happening here? Can someone explain to me?

谢谢

推荐答案

Canvas 维护一个 Matrix 负责其上的所有转换.即使是轮换.正如您在 中所见文档rotate 方法说:

The Canvas maintains a Matrix that is responsible for all transformations on it. Even for rotation. As you can see in the documentation, the rotate method says:

使用指定的旋转预先连接当前矩阵.

所有转换都在 Canvas Matrix 上完成,因此,在 Canvas 上完成.您绘制的弧线不会旋转.您首先旋转 Canvas,然后在其上绘图.

All transformations are done on the Canvas Matrix,hence, on the Canvas. The arc you draw isn't rotated. You first rotate the Canvas and then draw on it.

因此,在您的代码中,call -a 有效,而不是 call -b.

Hence, in your code, call -a works, not call -b.

对于 postrotate 和 prerotate 等问题,请检查 Matrix 类(postRotatepreRotate 方法).

For issues like postrotate and prerotate check the Matrix class(postRotate and preRotate methods).

几个例子:这个这个.

还有一些您可能想阅读的内容:this这个.

And few things you might want to read : this and this.

这篇关于Android 中的 Canvas.rotate() 问题.它究竟是如何工作的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆