i create a rectangle in a specific size, and now i want rotate it to 45 degree, i used canvas.rotate, matrix, but not working. how is the proper way to rotate canvas in android? and i'm curios about Path.Direction.CW, is it used for rotation? but i don't see any rotation function in Path()
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setColor(Color.BLUE);
paint.setAlpha(75);
Path path = new Path();
path.addRect(166, 748, 314, 890, Path.Direction.CW);
canvas.rotate(45);
canvas.drawPath(path, paint);
To draw a rotated rectangle you need to rotate the canvas before drawing, (then rotate it back to right-side up if you're drawing anything else). Canvas.rotate() just alters the canvas's transformation matrix, which transforms shapes drawn after the call.
canvas.save();
canvas.rotate(45);
canvas.drawRect(166, 748, 314, 890, paint);
canvas.restore();
Path.Direction has nothing to do with rotation transforms. From the docs:
Specifies how closed shapes (e.g. rects, ovals) are oriented when they
are added to a path.
If you want to draw something from (x,y) point, you have to rotate the canvas around (x,y) point. For doing this you should use
canvas.rotate(45,x,y);
so,
canvas.save();
canvas.rotate(45,x,y);
//all drawing from (x,y) point
canvas.restore();
Proper way should be something like this:
Path path = new Path();
path.addRect(166, 748, 314, 890, Path.Direction.CW);
canvas.save(); // first save the state of the canvas
canvas.rotate(45); // rotate it
canvas.drawPath(path, paint); // draw on it
canvas.restore(); // restore previous state (rotate it back)
Related
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 need to create a clipping mask with a shape of a circular sector.
I am able to draw one using the following:
paint.setColor(0x88FF0000);
paint.setStyle(Style.FILL);
canvas.drawArc(oval, 0, 30, true, paint);
I want to use it as a clipping path, so I've tried:
Path path = new Path();
path.addArc(oval, 0, 30);
canvas.clipPath(path, Op.REPLACE);
However addArc doesn't have the useCenter parameter so what I get is not a sector but a segment.
Ok, it seems there is no proper way doing this using a clipping mask.
However there is an alternate way using PorterDuffXfermode. See Xfermodes in ApiDemos.
I simply draw a sector using drawArc over my image with the DST_OUT operator. This makes the part of the image covered by the sector invisible (not drawn).
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(0xFFFFFFFF);
paint.setStyle(Style.FILL);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
drawable.draw(canvas);
canvas.drawArc(oval, 30, 90, true, paint);
I needed to tweak the AChartEngine library for Android to turn the pie chart into a donut chart. I needed to replace the following:
canvas.drawArc(oval, startAngle, sweepAngle, useCenter, paint);
by using the Path class in order to be able to use clipping. The following code did the job for me:
Path path = new Path();
path.moveTo(oval.centerX(), oval.centerY());
path.addArc(oval, startAngle, sweepAngle);
path.lineTo(oval.centerX(), oval.centerY());
canvas.drawPath(path, paint);
I hope this saves some headaches to people not familiar with the Canvas API as myself.
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
How can I use two canvas in my application so that I am able to rotate two bitmap images with different rotation speed
Why don't you just use the same canvas?
c.save();
c.rotate(rotationAngleForBitmap1);
c.drawBitmap(bitmap, matrix, paint);
c.restore();
c.rotate(rotationAngleForBitmap2);
c.drawBitmap(bitmap2, matrix, paint)
c.restore();
I'm trying to draw a transparent circle, but it just doesn't work.
when I'm drawing a bitmap it works, but a circle doesn't become transparent.
Here's my code in short:
Paint paint = new Paint();
paint.setAlpha(125);
canvas.drawBitmap(bitmap, sourceRect, destRect, paint); // this works fine
canvas.drawCircle(x, y, radius, paint); // the circle is drawn but not transparent
I found it.
paint.setAlpha must come after paint.setColor