I'd like to draw an oval, but I'd like to be able to rotate it. I know that I can use canvas.drawOval(...) and canvas.rotate(...). However, I want to just rotate my oval and not the whole canvas; that is, I want to rotate my oval before draw it to the canvas.
I have successfully rotated a rect by manipulating the coordinates before drawing it, but that approach isn't working for me with the oval.
There's canvas.save() and canvas.restore() to serve that purpose - i.e.
canvas.save();
canvas.rotate(90);
canvas.drawOval(....);
canvas.restore();
....//do other drawing
Related
I drawn a circle like this
canvas.drawCircle(40, 40, 50, mPaint);
I want to rotate this circle, how can I do this?
Rather than rotating the circle, you should rotate the canvas by method of Canvas.rotate() . You could look docs for this method.
In my Android application I have drawn rectangle in canvas, how can I rotate rectangle and resize the rectangle on touch events ?
canvas.drawRect(300,300,500,500,paint);
This code is used to draw the rectangle.
You can do using below
canvas.save(); // saving current canvas
resize
rect.set(x,y,newWidth, newHeight);
rotate
canvas.rotate(45);
Apply changes
canvas.drawRect(rect, paint);
canvas.restore();
I am using canvas on mapview for custom marker I have draw multiple components in Canvas but I want to rotate only one component rotate not all the components rotate same time. canvas have his own builtin function canvas.rotate(degree); but when we sub components like canvas.drawRect, canvas.drawText or canvas.drawBitmap then how we use rotate drawRect.
sorry for bad english...
thanks in advance.
You can save Canas state and then restore its state to remove all modifications:
canvas.save();
canvas.rotate(degree);
canvas.drawText(...);
canvas.restore();
In this answer you can read more about point of managing cavas state.
I want to say that maybe (probably) the word 'layer' is not the correct one, but I believe it gives the correct idea of what I want to do.
I am using a SurfaceView which implements SurfaceHolder.Callback.
I am working on a canvas. I am drawing a quite complex set of points.
I have the canvas translated, rotated and scaled (translateX and translateY are variable that changes when the user interacts with the canvas):
canvas.translate(0, 0);
canvas.rotate(-90);
canvas.translate(translateX, translateY);
canvas.scale(scaleX, scaleY);
My next step is to add text and images on top of this canvas.
Both the text and the images should not be scaled nor rotated, only translated (they should follow the canvas).
For this reason I was thinking about having some sort of 'layer' or something similar transparent which only follows the canvas movements and does not change on zoom in or zoom out or on rotation [think them as some sort of UI which stay over the whole canvas].
EDIT:
Here is a snippet:
#Override
public void onDraw(Canvas canvas) {
try{
pt.setColor(Color.BLACK);
canvas.drawColor(Color.WHITE);
canvas.drawText("HELLOOOOOOOOOOOOOOOO", 100, 10, pt);
pt.setAntiAlias(true);
canvas.save();
canvas.rotate(-90, 0, 0);
canvas.drawText("HELLOOOOOOOOOOOOOOOO", getHeight(), 10, pt);
canvas.restore();
}
catch(Exception e)
{}
}
I want the two 'HELLOOOOOO' to appear close to each other. I can't figure out how to.
Why not use save() and restore() so that after all your canvas is "normal" again. Than you can draw on it easily?
Save and restore working like a SceneGraph, if you have heard of it.
I managed to fix this. I simply rotated the canvas using:
rotate(+90, X, Y);
and then rotated back
rotate(-90, X, Y);
Doing this, the (X,Y) coordinates remain the same.
I have two arrows drawn on my canvas using the canvas.DrawPath(). I'm using canvas.rotate() to rotate, but it is obviously rotating both arrows by the same amount.
Id like to be able to rotate one arrow one way, and rotate the other arrow in a different direction.
Is this possible?
When you use canvas.rotate() you are altering the transformation matrix associated to the canvas, so all what you paint after that will be affected by the current state of the matrix, you have to do the following:
canvas.save(); //Save current canvas matrix state
canvas.rotate(angle);
canvas.DrawPath(); //Draw first arrow
canvas.restore(); //Restore canvas matrix to saved state
canvas.DrawPath(); //Draw second arrow without the rotation