How to draw a transparent circle? - android

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

Related

Setting a radial gradient as the background color of a circle on canvas

I draw some circles on a canvas using .drawCircle.
I would like the fill color of that circle to be a radial gradient (or linear gradient if that's not possible).
Basically I want the circles to have some depth to them.
I'm not quite sure how to accomplish this.
I know I can define a gradient as a drawable, but is there a way to set that as the background for the drawn circle?
For example in WPF I can simply define a LinearGradientBrush and define it's stop points.
Some code for context:
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setStrokeWidth(4);
canvas.drawCircle((float) temp_dot.get_x(),
(float) temp_dot.get_y(),
(float) temp_dot.get_radius(),
paint);
Thanks in advance for your time.
before drawing the circle, use the following code :
paint.setShader(new RadialGradient(
centerX,
centerY,
radius,
centerColor,
externalColor,
Shader.TileMode.CLAMP
));
canvas.drawCircle(..., paint);
See the RadialGradient if you want to have a more custom radial gradient.

Android canvas hidden shape

I am making a small app that requires drawing some rectangle and using its contains() method. My problem is to draw a hidden rectangle. I'm trying to use Paint's setStyle(Paint.Style.STROKE), then setStrokeWidth(0). But the stroke is still visible.
Set your paint color to transparent color. try the below
Paint paint = new Paint();
paint.setColor(0xFF);
OR
paint.setColor(Color.TRANSPARENT);
canvas.drawRect(30, 30, 80, 80, paint);

android how to rotate canvas rect

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)

Canvas keep ignoring Paint in drawBitmap method

I would like to draw bitmap(with specified color) on canvas.
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.RED);
// create bitmap
canvas.drawBitmap(bitmap, 0, 0, paint);
Well, the bitmap is visible on the canvas, but color of drawable didn't change.
Where is the problem?
I would like to draw bitmap(with specified color) on canvas.
A bitmap contains an image and drawing an image in single color doesn't make any sense. What do you expect it to do? Draw a red rectangle? Shapes can be drawn with color, not images...
The Color attribute of your Paint will be ignored. That Paint parameter is used to pass other settings such as anti-aliasing.
I hope this clarifies.
paint.setColor(Color.RED) is irrelevant. If your image comes with an alpha channel and you want it to be drawn in a single color, use ColorFilter instead:
paint.setColorFilter(new PorterDuffColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);

Circular sector shaped clipping mask with Path.addArc?

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.

Categories

Resources