Saeed-rz Saeed-rz - 1 month ago 20
Android Question

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.

Answer

finally i create a rectangle inside of my custom speech bubble

Rect rcText;
rcText = new Rect(rcBounds);//rcBounds is y speech bubble rect
canvas.save();

then use

canvas.cliprect(rect);

to limit the draw area and use

canvas.drawtext(mytext,rect.left,rect.top,paint);
canvas.restore();

to draw my text inside of custom speech bubble
thankx mc_fish for help