Android Canvas rectangle change color - android

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.

Related

Rounded Rectangle with Android Canvas

I'm attempting to draw a rectangle around a custom view in Android. I mostly have it working except for one detail.
Here is my code...
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setStrokeWidth(14.5f);
paint.setStyle(Paint.Style.STROKE);
canvas.drawRoundRect(0, 0, getWidth(), getHeight(), 20.0f, 20.0f, paint);
And here is the resulting rectangle...
As you can see, the inside of the rectangle does have rounded corners, but the outside still draws pointed corners. How can I make it so that the outside corners are rounded as well?
You don't see corners rounded on the outside because part of the stroke is outside of the bounds of the Canvas. You can check this easily by adding a certain margin to the coordinates of the round rectangle to make sure it is drawn inside the Canvas.
In fact your best option is trying to optimize this margin depending on the selected stroke width.

How to draw on bitmap using another bitmap as mask?

I want to draw on a bitmap using another bitmap as mask. The mask is a black bitmap with a transparent object in it. I want this transparent part to be filled with an arbitrary color and added to my original image. How can this be done?
+
+ blue color =
I had to change the masks as described by #Christian. Then, the wanted result could easily be produced:
canvas.drawRect(rect, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
canvas.drawBitmap(mask, 0.0f, 0.0f, paint);
If you're setting this to an ImageView, a quick way is to set the background to the gradient drawable, and a blue 'T' with a transparent background as the ImageView's source. Not fancy, but fast and simple!

How to nicely cut long text using Android canvas.drawText?

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

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);

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);

Categories

Resources