Android canvas drawing semi transparent figures, so they overlay each other - android

On a canvas I draw figures covering each-other to get the result I want. Their transparent is 128 and on cover areas color multiple. I need whole figure in one color. How to fix it?
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.GREEN);
Path path = new Path();
path.moveTo(margin,0);
path.lineTo(middleWidth+pieceWidth,0);
path.lineTo(middleWidth-pieceWidth,height);
path.lineTo(margin, height);
path.lineTo(margin, 0);
Path path1 = new Path();
path1.moveTo(middleWidth+pieceWidth+interval,0);
path1.lineTo(middleWidth-pieceWidth+interval,height);
path1.lineTo(width-margin,height);
path1.lineTo(width-margin,0);
path1.lineTo(middleWidth+pieceWidth+interval,0);
RectF rect = new RectF();
rect.set(0,0,middleWidth-pieceWidth,height);
RectF rect1 = new RectF();
rect1.set(middleWidth+pieceWidth,0,width,height);
paint.setStyle(Paint.Style.FILL);
paint.setColor(leftColor);
paint.setAlpha(128);
canvas.drawPath(path, paint);
paint.setColor(rightColor);
paint.setAlpha(128);
canvas.drawPath(path1, paint);
paint.setAlpha(128);
paint.setStyle(Paint.Style.FILL);
canvas.drawRoundRect(rect, corners, corners, paint);
canvas.drawRoundRect(rect1, corners, corners, paint);
}
The result is:
Result picture

Related

Drawing double colored square on Android canvas transparent rectangle

I have several lines in my application. If somebody touches a line I have to highlight the line touched. I think if I could draw a transparent rectangle with light color, other than the clicked line color, then it will be highlighted properly.. So can anybody tell me how can I draw a transparent rectangle on Android canvas?
My line color is black. Please see the pic.
This will draw green 50% transparent rectangle on canvas:
Paint myPaint = new Paint();
myPaint.setStyle(Paint.Style.FILL);
myPaint.setColor(Color.rgba(0, 256, 0, 128)); // green color with 50% transparency
// c is your canvas
c.drawRect(100, 100, 200, 200, myPaint);
try this way
private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(3);
mPaint.setPathEffect(null);
canvas.drawRect(x, y, x + width, y + height, mPaint);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(3);
mPaint.setPathEffect(new DashPathEffect(new float[] { 5, 5 }, 5));
canvas.drawRect(x, y, x + width, y + height, mPaint);
you can use this:
Paint paint = new Paint();
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.MAGENTA);
DashPathEffect dashPath =new DashPathEffect(new float[ ]{70,2}, 30);
paint.setPathEffect(dashPath);
paint.setStrokeWidth(80);
canvas.drawRect(30, 300 , 320, 300, paint);

How to draw text inside custom view?

I have created custom view to draw a custom speech bubble inside
and I want put some text on custom view. I use drawTextOnPath but it doesn't work right, I want the text to appear line by line.
Custom View - Speech Bubble
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setAntiAlias(true);
paint.setStrokeWidth(2);
paint.setStrokeJoin(Paint.Join.ROUND);
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setPathEffect(new CornerPathEffect(15) );
paint.setStyle(Paint.Style.FILL_AND_STROKE);
paint.setDither(true);
paint.setShader(new LinearGradient(0, 0, 0, getHeight(), Color.BLACK, Color.WHITE,
Shader.TileMode.MIRROR));
Path path = new Path();
paint.setShadowLayer(4, 2, 2, 0x80000000);
path.moveTo(myPath[0].x, myPath[0].y);
for (int i = 1; i < myPath.length; i++){
path.lineTo(myPath[i].x, myPath[i].y);
}
path.close();
canvas.clipPath(path);
canvas.drawPath(path, paint);
canvas.save();
paint = new Paint();
paint.setColor(Color.WHITE);
canvas.drawTextOnPath(MessageBody, path, 10, 10, paint);
Thanks.
something like this?
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setStyle(Style.FILL);
canvas.drawPaint(paint);
...
//tell the paint of the new color
paint.setColor(android.R.color.black);
paint.setTextSize(20);
canvas.drawText("Some Text", 10, 25, paint);
edit:
then why not something like this
Path path = new Path();
path.addCircle(width/2, height/2, radius, Path.Direction.CW);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setTextSize(20);
canvas.drawTextOnPath("Some Text", path, 0, 0, paint);
edit nr2:
why not add a rect?
path.addRect(left, top, right, bottom, Direction.CW);
or
path.addRect(rect, Direction.CW);
I created a rectangle in the speech bubble like so
Rect rcText;
rcText = new Rect(rcBounds);//rcBounds is y speech bubble rect
canvas.save();
Limited the draw area
canvas.cliprect(rect);
And draw the text inside the speech bubble like this
canvas.drawtext(mytext,rect.left,rect.top,paint);
canvas.restore();
Thanks mc_fish.

Drawing a filled rectangle with a border in android

Is there any way in Android to draw a filled rectangle with say a black border. My problem is that the canvas.draw() takes one paint object, and to my knowledge the paint object can't have a different color for the fill and the stroke. Is there a way around this?
Try paint.setStyle(Paint.Style.FILL) and paint.setStyle(Paint.Style.STROKE).
Paint paint = new Paint();
Rect r = new Rect(10, 10, 200, 100);
#Override
public void onDraw(Canvas canvas) {
// fill
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.MAGENTA);
canvas.drawRect(r, paint);
// border
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
canvas.drawRect(r, paint);
}
If you are drawing multiple views then you could also use two paints, one for the stroke and one for the fill. That way you don't have to keep resetting them.
Paint fillPaint = new Paint();
Paint strokePaint = new Paint();
RectF r = new RectF(30, 30, 1000, 500);
void initPaints() {
// fill
fillPaint.setStyle(Paint.Style.FILL);
fillPaint.setColor(Color.YELLOW);
// stroke
strokePaint.setStyle(Paint.Style.STROKE);
strokePaint.setColor(Color.BLACK);
strokePaint.setStrokeWidth(10);
}
#Override
protected void onDraw(Canvas canvas) {
// First rectangle
canvas.drawRect(r, fillPaint); // fill
canvas.drawRect(r, strokePaint); // stroke
canvas.translate(0, 600);
// Second rectangle
int cornerRadius = 50;
canvas.drawRoundRect(r, cornerRadius, cornerRadius, fillPaint); // fill
canvas.drawRoundRect(r, cornerRadius, cornerRadius, strokePaint); // stroke
}
You draw a rectangle with the color of the border and the size of the rectangle plus the border, you change the color of the paint and draw again the rectangle with the normal size.

How do I erase (make transparent) one custom region in a Android's canvas?

I'm overriding Android's ImageView in order to make the corners of my image transparent. I can accomplish that clipping the canvas in my onDraw(Canvas canvas):
#Override
protected void onDraw(Canvas canvas) {
Path clipPath = new Path();
int w = this.getWidth();
int h = this.getHeight();
clipPath.addRoundRect(new RectF(0,0,w,h), 10.0f, 10.0f, Path.Direction.CW);
canvas.clipPath(clipPath);
super.onDraw(canvas);
}
Unfortunately, it's not possible to antialias this round rectangle, and the result are ugly corners like this:
I know I can clear parts of my canvas with antialiasing using Paint and PorterDuff.Mode.CLEAR, what I don't know is to specify the round corners as the region to be erased. I'm looking for something like this:
#Override
protected void onDraw(Canvas canvas) {
//superclass will draw the bitmap accordingly
super.onDraw(canvas);
final Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
//this will erase a round rectangle, I need the exact inverse
canvas.drawRoundRect(rect, rx, ry, paint);
}
Is there any way to "erase" not the round rectangle, but it's inverse, ie, the round corners? And what if I just want to erase one of the corners?
Draw using a BitmapShader with a transparent color for your Paint object.
If you just want to erase one of the corners, try drawing it as a Path instead of a RoundRect.
protected void onDraw(Canvas canvas) {
BitmapShader bitmapShader = new BitmapShader(<original drawable>, TileMode.CLAMP, TileMode.CLAMP);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(0xFF000000);
paint.setShader(bitmapShader);
canvas.drawRoundRect(rect, rx, ry, paint);
}

draw an oval shape around the text on canvas

I want to draw an oval shape around the text on Canvas, I am displaying the 3 texts on Canvas using drawwText() method.
Now when I click on a particular text, I need to draw an oval around that text and again when we click on another text, the oval shape should appear on the clicked text. For this give me some code suggestions.Thanks in advance
use drawOval method().. here is the signature of the method..
public void drawOval (RectF oval, Paint paint)
RectF is class for drawing rectangle...whose constructor is defined as following...
RectF(x,y,x+width,y+height);
you can make its object as follows
RectF rect = new RectF(x,y,x+width,y+height);...
now pass this object in drawOval method....
canvas.drawOval(rect,paint);
for resolution (480 x 800)
in onCreate()
setContentView(new SampleView(this));
create class
private static class SampleView extends View {
// CONSTRUCTOR
public SampleView(Context context) {
super(context);
setFocusable(true);
}
#SuppressLint("DrawAllocation")
#Override
protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
//1
Paint paint = new Paint();
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.GRAY);
RectF oval1 = new RectF(0, 0, 250,250);
Paint p1 = new Paint();
p1.setColor(Color.BLACK);
canvas.drawText("Parent", 30, 50, p1);
canvas.drawOval(oval1, paint);
//2
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLUE);
RectF oval2 = new RectF(50, 50, 150, 150);
Paint p2 = new Paint();
p2.setColor(Color.GREEN);
canvas.drawText("Child", 75, 75, p2);
canvas.drawOval(oval2, paint);
}
}

Categories

Resources