Is it possible to draw unfilled figures on Android? By default circles and rectangles are filled.
You need to change the Paint style to stroke if you just want an outline with no fill:
Paint p = new Paint();
p.setStyle(Paint.Style.STROKE);
Related
There are many posts how to draw a dotted/dashed line or draw a border around circle. but I cannot find any information how to fill it. For example with DashPathEffect one can draw a stroke.
Is there any simple tool android provides similar to JS createPattern() ?
There is BitmapShader, which will allow you to create patterns. You can use it like this:
Paint paint = new Paint();
Shader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
paint.setShader(shader);
Then you use paint to draw on the canvas, and it will be filled with the specified pattern from the bitmap.
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.
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);
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);
How to draw a bitmap with a given color set as transparent?
For example I want all white pixels to be transparent.
You need to set the Alpha value for the paint you're passing to the Bitmap.
http://developer.android.com/reference/android/graphics/Paint.html#setAlpha%28int%29
Values vary from 0-255
EDIT:
Paint p = new Paint();
//Set Blue Color
p.setColor(Color.WHITE);
//Set transparency roughly at 50%
p.setAlpha(125);
you need to check every pixel of image and change its color.
you will get your answer in this Post
Another approach is drawing in trasparent color on your canvas (drawing holes).
The bitmapu needs alpha channel.
//Set transparent paint - you need all of these three
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
paint.setColor(Color.TRANSPARENT);
// Do you wanna soften?
// Set paint transparency:
// 0 = transparent ink, no visible effect
// 255 = full ink, hole in the bitmap
p.setAlpha(192);
// Do you want some blur?
// Set blur radius in pixel
paint.setMaskFilter(new BlurMaskFilter(10, Blur.NORMAL));