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);
Related
I have a custom view with a rectangle:
mCanvas.drawRect(0, 0, 50, 50, paint);
I want to change the fill of the rectangle based on touch input.
How do I change the paint after the rectangle has been drawn?
You can't change the paint like that. You need to redraw the rectangle.
I'm developing my own custom control. It's rectangle with a text inside. Text could be longer then rectangle so I need to cut it. Please help me style clipped text to make it understandable that there is more text. Last characters should have opacity.
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
// draw button
paint.setColor(Color.parseColor("#b33232"));
canvas.drawRect(0, 0, getWidth(), getHeight(), paint);
paint.reset();
// draw text
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.WHITE);
paint.setTextSize(16);
canvas.drawText("My very very long text", 5, 24, paint);
paint.reset();
First image is what I have
Second is what I need to get
I think TextUtils.ellipsize is what u want.
Check this..
TextUtils
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);
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);
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));