问题Canvas.rotate()Android中。如何它究竟工作? [英] Issue with Canvas.rotate() in Android .How does it exactly work?
问题描述
请找我下面的OnDraw方法的code(.I'm试图通过25度拉弧后,旋转画布(//旋转调用-b)。但我发现,电弧仍然绘制从0到50 degrees.I期待它由另一个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
}
}
但是,如果我画弧我看到拉弧是25度多。什么究竟是怎么回事移入前放置旋转调用(//旋转调用-a)?有人可以给我解释一下?
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?
感谢
推荐答案
在画布
维护矩阵
这是负责就可以了所有的转换。即使对于旋转。正如你可以在<一见href="http://developer.android.com/reference/android/graphics/Canvas.html#rotate%28float,%20float,%20float%29"相对=nofollow>文档,在旋转
法说:
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:
preconcat当前矩阵与指定的旋转。
所有的转换是在画布
矩阵
,因此,在画布<完成/ code>。你画圆弧不旋转。你先旋转
画布
再画就可以了。
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.
因此,在code,调用-a
的作品,而不是调用-b
。
Hence, in your code, call -a
works, not call -b
.
编辑:
对于像postrotate和prerotate查看矩阵类(问题 postRotate
和 preRotate
方法)。
For issues like postrotate and prerotate check the Matrix class(postRotate
and preRotate
methods).
和几件事情,你可能需要阅读:这个并<一href="http://stackoverflow.com/questions/3855578/android-matrix-what-is-the-different-between-$p$pconcat-and-postconcat">this.
And few things you might want to read : this and this.
这篇关于问题Canvas.rotate()Android中。如何它究竟工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!